如何跨多个回归模型 运行 coeftest()?
How to run coeftest() across multiple regression models?
我 运行 使用 purrr 包的两个不同模型的回归模型。第一个模型是 "sales + cpi"
,第二个模型是“sales + ndi"
。两个模型的因变量都是价格。下面的代码显示了我如何 运行 这两个模型在三个不同的模型中的回归地区。
我的问题是如何添加 coeftest()
作为循环中的第二步。这意味着我将为三个区域中的每个回归添加 coeftest()
。我在第二步展示了如何为一个模型做这件事。
我试图在 purrr package
中使用 map2()
包含 coeftest()
,但我无法将其集成到循环函数中。有人可以帮忙吗?
在下面的第一步中,我展示了如何 运行 跨区域的多元回归:
library(plm)
library(lmtest)
library(broom)
library(purrr)
library(dplyr)
data(Cigar)
Cigar$region = rbinom(n=1380, size=3, prob=0.5)
Cigar$region = as.factor(Cigar$region)
model_by_region= Cigar %>%
ungroup() %>%
nest_by(region) %>%
mutate(model = list(map(c
( "sales + cpi",
"sales+ ndi"), ~
cbind(model = .x, tidy(plm(formula(paste0("price ~ ", .x)),
index=c("state", "year"), model="within", data = data), conf.int=TRUE)))))
在第二步中,我展示了如何对一个模型进行 运行 coeftest():
model<- plm(price ~ sales + cpi, index=c("state", "year"), model = 'within',
data = Cigar)
#Extract the robust standard errors
plot_coeftest = tidy(coeftest(model))
这可以很简单:
model_by_country_with_coef_test = Cigar %>%
ungroup() %>%
nest_by(region) %>%
mutate(model = list(map(c
( "sales + cpi",
"sales+ ndi"), ~
cbind(model = .x, tidy(coeftest(plm(formula(paste0("price ~ ", .x)),
index=c("state", "year"), model="within", data = data)), conf.int=TRUE
)))))
model_by_country_with_coef_test[[3]][[1]]
[[1]]
model term estimate std.error statistic p.value conf.low conf.high
1 sales + cpi sales -0.4716673 0.04863656 -9.697793 6.831817e-17 -0.5679327 -0.3754019
2 sales + cpi cpi 1.0209728 0.02336462 43.697376 3.542571e-77 0.9747277 1.0672179
[[2]]
model term estimate std.error statistic p.value conf.low conf.high
1 sales+ ndi sales -0.267552595 0.0500205394 -5.348855 4.111431e-07 -0.366557254 -0.16854794
2 sales+ ndi ndi 0.008491882 0.0001944991 43.660261 3.911187e-77 0.008106914 0.00887685
我 运行 使用 purrr 包的两个不同模型的回归模型。第一个模型是 "sales + cpi"
,第二个模型是“sales + ndi"
。两个模型的因变量都是价格。下面的代码显示了我如何 运行 这两个模型在三个不同的模型中的回归地区。
我的问题是如何添加 coeftest()
作为循环中的第二步。这意味着我将为三个区域中的每个回归添加 coeftest()
。我在第二步展示了如何为一个模型做这件事。
我试图在 purrr package
中使用 map2()
包含 coeftest()
,但我无法将其集成到循环函数中。有人可以帮忙吗?
在下面的第一步中,我展示了如何 运行 跨区域的多元回归:
library(plm)
library(lmtest)
library(broom)
library(purrr)
library(dplyr)
data(Cigar)
Cigar$region = rbinom(n=1380, size=3, prob=0.5)
Cigar$region = as.factor(Cigar$region)
model_by_region= Cigar %>%
ungroup() %>%
nest_by(region) %>%
mutate(model = list(map(c
( "sales + cpi",
"sales+ ndi"), ~
cbind(model = .x, tidy(plm(formula(paste0("price ~ ", .x)),
index=c("state", "year"), model="within", data = data), conf.int=TRUE)))))
在第二步中,我展示了如何对一个模型进行 运行 coeftest():
model<- plm(price ~ sales + cpi, index=c("state", "year"), model = 'within',
data = Cigar)
#Extract the robust standard errors
plot_coeftest = tidy(coeftest(model))
这可以很简单:
model_by_country_with_coef_test = Cigar %>%
ungroup() %>%
nest_by(region) %>%
mutate(model = list(map(c
( "sales + cpi",
"sales+ ndi"), ~
cbind(model = .x, tidy(coeftest(plm(formula(paste0("price ~ ", .x)),
index=c("state", "year"), model="within", data = data)), conf.int=TRUE
)))))
model_by_country_with_coef_test[[3]][[1]]
[[1]]
model term estimate std.error statistic p.value conf.low conf.high
1 sales + cpi sales -0.4716673 0.04863656 -9.697793 6.831817e-17 -0.5679327 -0.3754019
2 sales + cpi cpi 1.0209728 0.02336462 43.697376 3.542571e-77 0.9747277 1.0672179
[[2]]
model term estimate std.error statistic p.value conf.low conf.high
1 sales+ ndi sales -0.267552595 0.0500205394 -5.348855 4.111431e-07 -0.366557254 -0.16854794
2 sales+ ndi ndi 0.008491882 0.0001944991 43.660261 3.911187e-77 0.008106914 0.00887685