2013-03-26 107 views
0

我可以看到这已经差不多完成了,但我是R新手,无法弄清楚。非常多,我有一个回归循环(请不要批评数据挖掘),我需要从每个循环中报告一些事情到一个新的列表/数据框/任何最合适的。这里是我的代码:循环回归提取每个回归

#Required packages 
require(lattice) 
require(plyr) 


ACDn <- "ACDn.csv" 
x <- as.matrix(read.csv(ACDn, colClasses = "numeric")) 

#To find which columns are which, in order to split up the overall dataset. 
which(colnames(X)=="H95.H30") 
which(colnames(X)=="H99") 

#Here i split all the data into their different types, i.e. LHt = Lidar Heights. Please ignore 
#those that are unpopulated, as i am waiting on data to run. 

Yall <- x[,c(59:79)]       #All "True Variables" - BA, MTH, etc. 
Y <- Yall[,10]         #Specifies which columnn is the Y variable, BA = 10, 
               #TopHt = 11, SPH = 12, Vol_live = 13, RecovVol = 14 

X <- x[,c(1:58,80:95)]       #All Lidar metrics and combinations. 
LHt <- X[,c(28:41,59:74)] 
LCv <- X[,c()] 
LKu <- X[,c()] 
LSk <- X[,c()] 
L?? <- X[,c()] 

#Create List file. I 

Optmod1 <- 

#Loop Creation, need dataset sizes. The ?? are caused by not knowing the exact sizes 
#of the relative datasets yet. Somewhere in here i would like the an entry for EACH model to be 
#appended to a data.frame (or list, whatever is most appropriate), which would state the variables 
# i.e. 'y', 'i', 'j', 'k', 'l', 'm', and the Adj. R-squared value (which i guess can be extracted 
# through using 'summary(mod)$adj.r.squared). 

For(i in 1:30) { 
    For(j in 1:??) { 
    For(k in 1:??) { 
     For(l in 1:??){ 
     For(m in 1:??){ 
      mod <- lm(Y ~ LHt[i] + LCv[j] + LKu[k] + LSk[l] + L??[m]) 
     } 
     } 
    } 
    } 
} 

那么好看多了,以后“国防部”已经跑每一次,我只需要它扔“Y”,“我”,“J”,“K”,“L” ,'米',并调整.R平方(我想通过使用“摘要(mod)$ adj.r.squared”)到可抽取的表中。

对不起,如果有任何这是r-illiterate,我是新来的,并刚才给出了规定的代码,因此我的基本理解是稀疏的。

谢谢你的时间!

P.S.随时提出任何问题 - 我会努力回答他们!

回答

1

简短的回答你的问题是

Answers = list() 
For(i in 1:30) { 
    For(j in 1:??) { 
    For(k in 1:??) { 
     For(l in 1:??){ 
     For(m in 1:??){ 
      mod <- lm(Y ~ LHt[i] + LCv[j] + LKu[k] + LSk[l] + L??[m]) 
      Answers[[length(Answers)+1]] = list(i,j,k,l,m,summary(mod)$adj.r.squared) 
     } 
     } 
    } 
    } 
} 

将存储您在列表中所需的信息。它的工作原理是创建一个空白列表,然后每次在循环中运行回归模型时都会添加一个空白列表。但是,在循环中增加这样的列表是非常糟糕的R练习。

你可能会更好先写形式LHt[i] + LCv[j] + LKu[k] + LSk[l] + L??[m]的所有可能的公式到一个列表,然后使用lapply做回归...

首先使用expand.grid给5列的数据帧,与从每个类别

LHT_names = lapply(1:30,function(i) paste("LHt[",i,"]",sep="")) #a list of names of LHT type variables for use in formula 
LCv_names = lapply(1:?,function(i) paste("LCv[",i,"]",sep="")) #similar for LCv 
LKu_names = ... 
LSk_names = ... 
L??_names = ... 

temp = expand.grid(c(LHt_names, LCv_names, LKu_names, LSk_names, L??_names)) 

然后将含有一个变量名每一列,用浆糊和lapply得到公式列表:

list_of_formulas = lapply(seq_along(nrow(temp)), function(i) paste("Y~",paste(temp[i,],collapse="+"),sep = "")) 

然后,使用lapply获取回归模型列表

list_of_models = lapply(list_of_formulas, function(x) lm(x)) 
+0

非常感谢您的回复。 我的问题是:当我做第二件事你说(最好的做法)我收到一条错误消息: > LHT_names = lapply(1:30,paste(“LHt [”,i,“]”,sep =“”)) 错误get(as.character(FUN),mode =“function”,envir = envir): 找不到模式'function'的对象'LHt [1]' 我该怎么做? – Schmakk 2013-03-26 06:23:26

+0

对不起,这是一个错字...你需要在'paste'之前插入'function(i)'。看看'lapply',并且在它工作时看看那条线的输出。 – Alex 2013-03-26 06:28:06

+0

Hrmmmm,我到了最后一段代码,并得到一个错误,说'function(x)'后面的“=”是意外的。 – Schmakk 2013-03-26 06:34:36