R Variable Length Differ when build linear model for residuals

╄→尐↘猪︶ㄣ 提交于 2019-12-08 08:15:12

问题


I am working on a problem where I want to build a linear model using residuals of two other linear models. I have used UN3 data set to show my problem since its easy put the problem here than using my actual data set.

Here is my R code:

head(UN3)
m1.lgFert.purban <- lm(log(Fertility) ~ Purban, data=UN3)
m2.lgPPgdp.purban <- lm(log(PPgdp) ~ Purban,  data=UN3)
m3 <- lm(residuals(m1.lgFert.purban) ~ residuals(m2.lgPPgdp.purban))

Here is the error I am getting:

> m3 <- lm(residuals(m1.lgFert.purban) ~ residuals(m2.lgPPgdp.purban))
Error in model.frame.default(formula = residuals(m1.lgFert.purban) ~ residuals(m2.lgPPgdp.purban),  : 
  variable lengths differ (found for 'residuals(m2.lgPPgdp.purban)')

I am not really understanding the why this error actually take place. If it was log related issue then I should have gotten the error when I am building first two models.


回答1:


Your default na.action is most likely na.omit (check with options("na.action")). This means that NA values get removed silently, resulting in different lengths of the residuals vectors. You probably want to use na.action="na.exclude", which pads the residuals with NAs.

library(alr3)
options("na.action")
#$na.action
#[1] "na.omit"

m1.lgFert.purban <- lm(log(Fertility) ~ Purban, data=UN3,na.action="na.exclude")
m2.lgPPgdp.purban <- lm(log(PPgdp) ~ Purban,  data=UN3,na.action="na.exclude")

m3 <- lm(residuals(m1.lgFert.purban) ~ residuals(m2.lgPPgdp.purban))
#Coefficients:
#                 (Intercept)  residuals(m2.lgPPgdp.purban)  
#                    -0.01245                      -0.18127  


来源:https://stackoverflow.com/questions/14924541/r-variable-length-differ-when-build-linear-model-for-residuals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!