2013-10-18 55 views
-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,因此循環無法繼續。任何人都可以給我一些建議,以保持該計劃在這種情況下進行?我希望得到的結果是每個迴歸模型的常數。您的意見將非常感謝。

謝謝。

回答

0

包裝紙的循環體中try將允許出現錯誤後繼續。另外,還可以像這樣一次分配的results一整行:

for(i in 1:8)try({fit<-lm(as.vector(funds[,i])~TBY,data=factors,na.action=na.omit) 
      results[i,]<-coef(summary(fit))[1,] 
     }) 
## Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
## 0 (non-NA) cases 
results 
##  estimates residual   t   p 
## fund1 0.1977773 0.1949221 1.0146478 0.4953715 
## fund2 0.7192174 0.2862573 2.5124861 0.2411462 
## fund3 2.9271787  NaN  NaN  NaN 
## fund4 0.8757588 2.1261925 0.4118906 0.7512633 
## fund5 -0.3371507 0.5472105 -0.6161262 0.6005921 
## fund6 0.2844758 0.3068079 0.9272114 0.4222080 
## fund7   NA  NA   NA  NA 
## fund8 -0.2380825 0.2613918 -0.9108262 0.4295420 

順便說一句,你避免了循環完全與sapplytryCatch

sapply(funds,function(x) 
     tryCatch(coef(summary(lm(x ~ TBY,data=factors,na.action=na.omit)))[1,], 
       error= function(x)rep(NA,4))) 

##    fund1  fund2 fund3  fund4  fund5  fund6 fund7 
## Estimate 0.1977773 0.7192174 2.927179 0.8757588 -0.3371507 0.2844758 NA 
## Std. Error 0.1949221 0.2862573  NaN 2.1261925 0.5472105 0.3068079 NA 
## t value 1.0146478 2.5124861  NaN 0.4118906 -0.6161262 0.9272114 NA 
## Pr(>|t|) 0.4953715 0.2411462  NaN 0.7512633 0.6005921 0.4222080 NA 
##     fund8 
## Estimate -0.2380825 
## Std. Error 0.2613918 
## t value -0.9108262 
## Pr(>|t|) 0.4295420 
+0

太感謝你了! – user2893255