[英]linear regression in r using differenced response variable
I am trying to run this code 我正在尝试运行此代码
r2<-lm(diff(Y)~.,data=credit.train)
Now, I get the error: 现在,我得到了错误:
Error in model.frame.default(formula = diff(Y) ~ ., data = credit.train, : variable lengths differ (found for 't') model.frame.default(formula = diff(Y)〜。,data = credit.train,:中的错误:可变长度不同(找到't')
I understand that on differencing, number of rows of Y is decreasing by 1 which is not happening for the X variables. 我知道,在相异的情况下,Y的行数减少1,而X变量却没有发生。 Any idea how to tackle this? 任何想法如何解决这个问题?
You can extend at one end or the other (with loss of one set of predictors) using the I()
-function 您可以使用I()
函数在一端或另一端进行扩展(损失一组预测变量)
From the ?lm
page: 从?lm
页面:
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(I(c(diff(weight),NA)) ~ group) # no error
Also no error just with: 同样也没有错误:
lm.D9 <- lm( c(diff(weight),NA) ~ group)
我想您想针对某个变量x(n)
或t(n)
等对值Y
的增加进行回归,即Y(n+1)-Y(n)
,所以只需从数据帧中过滤掉最后一行:
r2 <- lm(diff(Y) ~ ., data = credit.train[1:(length(credit.train)-1), ])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.