lag()
library(plm)
data <- data.frame(country = rep(c("A", "B", "C"), each = 3),
year = rep(c(1, 2, 3), 3),
population = c(100, 150, 200, 1000, 1200, 1250, 10, 20, 45)) |>
pdata.frame(index = c('country', 'year'))
data$lag_population <- plm::lag(data$population, k = 1)
lm_model <- lm(population ~ lag_population, data)
plm_model <- plm(population ~ plm::lag(population, k = 1), data, model = 'pooling')
plm_model2 <- plm(population ~ lag_population, data, model = 'pooling')
shyet,他们对相同数据的预测是不同的:
> print(lm_model)
Call:
lm(formula = population ~ lag_population, data = data)
Coefficients:
(Intercept) lag_population
31.633 1.079
+ print(plm_model)
Model Formula: population ~ plm::lag(population, k = 1)
Coefficients:
(Intercept) plm::lag(population, k = 1)
31.6332 1.0787
+ print(plm_model2)
Model Formula: population ~ lag_population
Coefficients:
(Intercept) lag_population
31.6332 1.0787
基于预期的预测,每个横截面的第一阶段(b/c第一期都没有滞后观测值)
lm()
使用生成的变量而不是
NA
函数的预测与
plm()
lag_population
预测使用使用
lag()
lm()
?)
NA
有人遇到类似问题吗? 如何使PLM预测看起来与基于
plm()
?的预测相同
将开发版本构建,例如:
lag()
以您的例子,它产生:
fill.na = FALSE
predict_plm <- predict(plm_model, newdata = data, na.fill = FALSE)
predict_plm2 <- predict(plm_model2, newdata = data, na.fill = FALSE)
predict_lm <- predict(lm_model, newdata = data)
> print(predict_lm)
A-1 A-2 A-3 B-1 B-2 B-3 C-1 C-2 C-3
NA 139.50424 193.43973 NA 1110.34313 1326.08511 NA 42.42035 53.20745
+ print(predict_plm2)
A-2 A-3 B-2 B-3 C-2 C-3
139.50424 193.43973 1110.34313 1326.08511 42.42035 53.20745
+ print(predict_plm)
A-1 A-2 A-3 B-1 B-2 B-3 C-1 C-2 C-3
139.50424 193.43973 247.37522 1110.34313 1326.08511 1380.02060 42.42035 53.20745 80.17519