### lm function
tic <- Sys.time()
Data<-matrix(nrow=10,ncol=20,rnorm(1:200))
rownames(Data)<-paste("Gene", 1:10, sep = "")
test<-matrix(nrow=nrow(Data),ncol=ncol(Data)+1)
test[1,]<-rep(0,21)
for(i in 2:10){
out<-lm(Data[1,]~Data[i,])
test[i,1]<-out$coefficients[2]
test[i,-1]<-out$residuals
out<-c()
}
toc <- Sys.time()
comp.time <- toc - tic
### matrix calculate
tic <- Sys.time()
Data<-matrix(nrow=10,ncol=20,rnorm(1:200))
rownames(Data)<-paste("Gene", 1:10, sep = "")
test<-matrix(nrow=nrow(Data),ncol=ncol(Data)+1)
test[1,]<-rep(0,21)
y<-matrix(ncol=1,Data[1,])
for(i in 2:10){
x<-c()
out<-c()
x<-cbind(rep(1,20),Data[i,])
#求bo,b1
out<-solve(t(x)%*%x) %*% (t(x)%*%y)
test[i,1]<-out[2,]
#求residual
res<-c(y - (x%*%out))
sse<-sum(res^2)
}
toc <- Sys.time()
comp.time <- toc - tic
沒有留言:
張貼留言