-1
數據集我有遺漏的數字多元迴歸由1380只對衝基金的月度收益,但大部分資金存在缺失數據。我想將每個單一基金的月回報率迴歸到一些因素,例如國債券收益率(TBY)。我試圖用一個for循環倒退每個資金因素的月度回報,但收到以下錯誤信息:在每個因變量
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
# 0 (non-NA) cases
我也做了一些網絡上搜索,並相信該問題是由清單明智引起刪除。我已複製一個簡單的例子來說明:
#create a dataframe A with 8 funds and two factors
A<-data.frame(fund1=rnorm(5),fund2=rnorm(5),fund3=rnorm(5),fund4=rnorm(5),
fund5=rnorm(5),fund6=rnorm(5),fund7=rnorm(5),fund8=rnorm(5),
SP500=rnorm(5),TBY=rnorm(5))
#replace some vlaue with NA
A[1,3:5]<-NA
A[2,1:2]<-NA
A[3,3]<-NA
A[4,2:4]<-NA
A[5,1]<-NA
A[1:5,7]<-NA
A
# build two data frames to split funds and factors
funds<-as.data.frame(A[,1:8])
factors<-as.data.frame(A[,9:10])
# build empty data frame to store regression outputs
results<-data.frame(matrix(NA,ncol=4,nrow=8))
colnames(results)<-c("estimates", "residual", "t", "p")
rownames(results)<-as.vector(colnames(funds))
for(i in 1:8){
fit<-lm(as.vector(funds[,i])~TBY,data=factors,na.action=na.omit)
results[i,1]<-coef(summary(fit))[1,1]
results[i,2]<-coef(summary(fit))[1,2]
results[i,3]<-coef(summary(fit))[1,3]
results[i,4]<-coef(summary(fit))[1,4]
}
results
最後的結果是這樣的:
results
# estimates residual t p
# fund1 0.1039720 0.2486456 0.4181535 0.7478621
# fund2 -0.1040939 0.2464246 -0.4224168 0.7455554
# fund3 0.3869647 NaN NaN NaN
# fund4 0.1349445 0.2107588 0.6402796 0.6374377
# fund5 0.7470140 0.4066014 1.8372147 0.2075786
# fund6 0.8305238 0.3845686 2.1596245 0.1196180
# fund7 NA NA NA NA
# fund8 NA NA NA NA
程序停止在fund7循環。我認爲主要原因是基金7的專欄只包含NA
,因此循環無法繼續。任何人都可以給我一些建議,以保持該計劃在這種情況下進行?我希望得到的結果是每個迴歸模型的常數。您的意見將非常感謝。
謝謝。
太感謝你了! – user2893255