# NOT RUN {
n <- 1000
k <- 3
y <- rnorm(n)
x <- round(matrix(rnorm(n * k), n, k), digits = 3)
colnames(x) <- c("s1", "s2", "s3")
da <- data.frame(y, x)
do1 <- da[1:300,]
do2 <- da[301:700,]
do3 <- da[701:1000,]
m1 <- speedlm(y ~ s1 + s2 + s3, data = do1)
m1 <- update(m1, data = do2)
m1 <- update(m1, data = do3)
m2 <- lm(y ~ s1 + s2 + s3, data = da)
summary(m1)
summary(m2)
# }
# NOT RUN {
# }
# NOT RUN {
# as before but recursively
make.data <- function(filename, chunksize,...){
conn <- NULL
function(reset=FALSE, header=TRUE){
if(reset){
if(!is.null(conn)) close(conn)
conn<<-file(filename,open="r")
} else{
rval <- read.table(conn, nrows=chunksize,header=header,...)
if (nrow(rval)==0) {
close(conn)
conn<<-NULL
rval<-NULL
}
return(rval)
}
}
}
write.table(da,"da.txt",col.names=TRUE,row.names=FALSE,quote=FALSE)
x.names <- c("s1", "s2", "s3")
dat <- make.data("da.txt",chunksize=300,col.names=c("y",x.names))
dat(reset=TRUE)
da2 <- dat(reset=FALSE)
# the first model runs on the first 300 rows.
m3 <- speedlm(y ~ s1 + s2 + s3, data=da2)
# the last three models run on the subsequent 300, 300 and 100 rows, respectively
for (i in 1:3){
da2 <- dat(reset=FALSE, header=FALSE)
m3 <- update(m3, data=da2, add=TRUE)
}
all.equal(coef(m1),coef(m3))
file.remove("da.txt")
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab