简体   繁体   English

R线性回归中roll_lm和lm的区别

[英]Difference between roll_lm and lm in R linear regression

I am looking to make a rolling linear regression, i found the function roll_lm, but it provide a different result from the function lm.我希望进行滚动线性回归,我找到了函数 roll_lm,但它提供了与函数 lm 不同的结果。 Are they not supposed to yield the same results ?他们不应该产生相同的结果吗?

# creating dataset
set.seed(123)
ABC <- sample(seq(from = 20, to = 50, by = 5), size = 50, replace = TRUE)
DCE <- sample(seq(from = 20, to = 50, by = 5), size = 50, replace = TRUE)

# Calculating rolling correlation and using last coeff
library(roll)
Rolling.Correl <- roll_lm(ABC , DCE, 50)
last(Rolling.Correl$coefficients[,2])
# [1] -0.233245

# Calculating basic regression using lm
Trad.Rolling.Correl <- lm(ABC ~ DCE)
Trad.Rolling.Correl

# Call:
# lm(formula = ABC ~ DCE)
#
# Coefficients:
# (Intercept)          DCE  
#     41.9204      -0.2112  

On this specific case, I get -0.233245 in one hand and -0.2112 in the other.在这种特定情况下,我一方面得到 -0.233245,另一方面得到 -0.2112。

?roll_lm says roll_lm(x, y,...) , so you need to compare it with lm(DCE ~ ABC) ?roll_lm表示roll_lm(x, y,...) ,因此您需要将其与lm(DCE ~ ABC)进行比较

lm(formula = DCE ~ ABC)

# Coefficients:
# (Intercept)          ABC  
#     43.6236      -0.2332  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM