2013-03-26 106 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