我正在尝试在r中创建一个函数,该函数将使用来自第二个矩阵的预测变量在一个矩阵中的多个不同数据列上运行glm,然后将结果矩阵写入csv文件。 我已经设法使用以下方法为单个预测器glm执行此操作:
X<- matrix(sample(10,100,T),10)
colnames(X)<- c("f5", "f10", "f15","f20","f25","f30", "f35", "f40", "f45","f50") # The matrix with the dependant variables
Y<- matrix(runif(10, min=1, max= 50)) #The matrix with the predictor
M<-matrix(nrow=dim(X)[2],ncol=9) #the matrix to store the output in
colnames(M)< c("Species","intercept","slope","pval.I","pval.B","Disp","Dev.Null","Dev.res","Dev.expl") #the headings of the output file names for species
ns<-colnames(X)
for (k in 1:dim(X)[2]){
glm1<-glm(X[,k]~Y,family=poisson) #calculate the glm
print(g1<-summary(glm1)) #print the output in the file # filling the rows in the results table
M[k,1]<-ns[k] #store the name
M[k,c(2:3)]<-coef(glm1) # store the coefficients
M[k,c(4:5)]<-t(g1$coefficients[c(1:2),4] )
M[k,6]<-g1$dispersion # store the dispersion
M[k,7]<-g1$null.deviance #store the null deviance
M[k,8]<-g1$deviance #store the residual deviance
M[k,9]<-round((g1$null.deviance-g1$deviance)/g1$null.deviance,4) #caluclate the deviance explained
}
write.csv(M,paste("test.csv",sep=""),row.names=FALSE) #write the results matrix M to a csv file called test
这对单个预测变量模型很有用,但是我想使用多个预测变量来运行分析,以便:
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 with multiple predictors
colnames(Y)<- c("h1","h2","h3","h4","h5")
如果我尝试运行相同的代码,我收到的错误消息是
Error in M[k, c(2:3)] <- coef(glm1) :
number of items to replace is not a multiple of replacement length
我认为我告诉我,我为结果设置的M矩阵不正确,但是我很难弄清楚如何定义M矩阵以纠正这个问题。
非常感谢任何帮助
查理