r - How do I write a loop to run and save outputs from a glm with multiple predictors -


i trying create function in r run glm on number of different columns of data in 1 matrix using predictors second matrix , write results matrix csv file. have managed single predictor glm using:

x<- matrix(sample(10,100,t),10) colnames(x)<- c("f5", "f10", "f15","f20","f25","f30", "f35", "f40", "f45","f50") # matrix dependant variables y<- matrix(runif(10, min=1, max= 50)) #the matrix predictor  m<-matrix(nrow=dim(x)[2],ncol=9) #the matrix store output in colnames(m)< c("species","intercept","slope","pval.i","pval.b","disp","dev.null","dev.res","dev.expl") #the headings of output file names  species ns<-colnames(x)  (k in 1:dim(x)[2]){   glm1<-glm(x[,k]~y,family=poisson) #calculate glm    print(g1<-summary(glm1)) #print output in file # filling rows in results table m[k,1]<-ns[k] #store name m[k,c(2:3)]<-coef(glm1) # store coefficients m[k,c(4:5)]<-t(g1$coefficients[c(1:2),4]   ) m[k,6]<-g1$dispersion # store dispersion m[k,7]<-g1$null.deviance #store null deviance m[k,8]<-g1$deviance #store residual deviance m[k,9]<-round((g1$null.deviance-g1$deviance)/g1$null.deviance,4) #caluclate deviance explained }  write.csv(m,paste("test.csv",sep=""),row.names=false) #write results matrix m csv file called test 

this great single predictor model, run analysis using multiple predictors that:

x<- matrix(sample(10,100,t),10) colnames(x)<- c("f5", "f10", "f15","f20","f25","f30", "f35", "f40", "f45","f50") #dependent y<- matrix(runif(50, min=1, max= 50), 10) #matrix multiple predictors colnames(y)<- c("h1","h2","h3","h4","h5") 

if attempt run same code error message receive

error in m[k, c(2:3)] <- coef(glm1) :                  number of items replace not multiple of replacement length 

which think telling me m matrix have set results not correct, having difficulty trying work out how m matrix needs defined in order correct this.

any appreciated

charlie


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -