2013-02-21 35 views
4

我有一個擁有較舊和較新數據的大型數據集。我創建了兩個數據框,EarlyYears包含舊數據,LaterYears包含新數據,因此它們具有相同的列。R - 分割數據,迴歸並將公式應用於新的分割數據集

我想要做的是迴歸早年的數據來確定一個方程,並將其應用於後幾年以測試方程的強度 - A和B是常數,Input是我正在測試的 - 我將它改爲代碼的不同運行 - 虛擬爲1是沒有數據輸入。然而,我想將EarlyYears和LaterYears數據按照其中一個變量的五分位數進行分解,並將EarlyYears的五分位數1中找到的方程應用於分位數爲1的LaterYears中的數據。我在R中相當新,迄今爲止有:

Model<-data.frame(Date = rep(c("3/31/09","3/31/11"),each = 20), 
InputRating = rep(c(1:5), 8), Dummy = rep(c(rep(0,9),1),4), 
Y = rep(1,3,5,7,11,13,17,19), A = 1:40,B = 1:40*3+7) 
newer<-as.numeric(grep("/11",Model$Date)) 
later<-as.numeric(grep("/11",Model$Date,invert = TRUE)) 

LaterYears<-Model[newer,] 
EarlyYears<-Model[later,] 
newModel<-EarlyYears 

DataSet.Input<-data.frame(Date = newModel$Date, InputRating = newModel$InputRating, 
Dummy = newModel$Dummy, Y = newModel$Y, A = newModel$A,B = newModel$B) 
quintiles<-quantile(DataSet.Input$A,probs=c(0.2,0.4,0.6, 0.8, 1.0)) 
VarQuint<-findInterval(DataSet.Input$A,quintiles,rightmost.closed=TRUE)+1L 

regressionData<-do.call(rbind,lapply(split(DataSet.Input,VarQuint), 
FUN = function(SplitData) { 
SplitRegression<-lm(Y ~ A + B + InputRating + Dummy, data = SplitData, na.action = na.omit) 
c(coef.Intercept = coef(summary(SplitRegression))[1], 
coef.A = coef(summary(SplitRegression))[2], 
coef.B = coef(summary(SplitRegression))[3], 
coef.Input = coef(summary(SplitRegression))[4], 
coef.Dummy= coef(summary(SplitRegression))[5]) 
})) 

i = 0 
quintiles.LY<-quantile(LaterYears$A,probs=c(0.2,0.4,0.6, 0.8, 1.0)) 
Quint.LY<-findInterval(LaterYears$A,quintiles,rightmost.closed=TRUE)+1L 

LaterYears$ExpectedValue <-apply(split(LaterYears,Quint.LY),1, 
FUN = function(SplitData) { 
    i=i+1 
    regressionData[i,1]+regressionData[i,2]*SplitData$A + 
    regressionData[i,3]*SplitData$B + regressionData[i,4]*SplitData$Input + 
    regressionData[i,5]*SplitData$Dummy  
}) 

第一部分工作很好,以獲取regressionData中的數據。我想將公式中的LaterYears數據集內列舉行的這個結果,但我得到一個錯誤 -

Error in apply(split(LaterYears, Quint.LY), 1, FUN = function(SplitData) { : 
dim(X) must have a positive length 

與lapply其運行時,運行此與申請時,空白是我最初試圖。

任何有關如何解決這個問題的幫助將不勝感激! 謝謝!

+1

我們沒有DataSet.Quint和VarQuint。你可以讓你的問題重現嗎? – 2013-02-21 18:22:47

+0

這是更好嗎?我把數據框和VarQuint的構造放進去,但newModel只是一個大的數據集。 – user1775563 2013-02-21 19:18:31

+0

我不這麼認爲,我仍然無法重現您的問題。嘗試http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – 2013-02-21 21:18:37

回答

4

也許像這樣,使用predict會更好。它不適用於您的示例數據,但它可能適用於真實數據。

# by, splits a dataset by a factor 
regressionData <- by(DataSet.Input,VarQuint, 
        function(d) { 
         lm1 <- lm(Y ~ A + B + InputRating + Dummy, d) 
        }) 

quintiles.LY<-quantile(LaterYears$A,probs=seq(0,1,0.2)) 
Quint.LY<-findInterval(LaterYears$A,quintiles,rightmost.closed=TRUE)+1L 

LaterYearsPredict <- split(LaterYears,Quint.LY) 

# lapply's arguments can be anything that is a sequence 
LaterYears$ExpectedValue <- unlist(lapply(1:length(LaterYearsPredict), 
     function(x) 
     predict(regressionData[[x]],LaterYearsPredict[[x]]) 
     )) 
+0

完美處理真實數據 - 感謝布蘭登!現在,我可以添加預測到R可以做的事情列表,我不會想到它可以讓我的生活變得如此簡單! – user1775563 2013-02-22 15:24:10

+0

這個解決方案存在一個潛在的問題,如果你的數據沒有被分割向量排序,當你沒有列出並追加期望值時,某些數字可能會失序。我相信你必須首先對data.frames進行排序。 (除非他們已經以某種方式訂購) – 2013-02-22 20:10:17