下方的数据
analysis<-tibble(off_race = c("hispanic", "hispanic", "white","white", "hispanic", "white", "hispanic", "white", "white", "white","hispanic"), any_black_uof = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), any_black_arrest = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), prop_white_scale = c(0.866619646027524, -1.14647499712298, 1.33793539994219, 0.593565300512359, -0.712819809606193, 0.3473585867755, -1.37025501425243, 1.16596624239715, 0.104521426674564, 0.104521426674564, -1.53728347122581), prop_hisp_scale=c(-0.347382203637802, 1.54966785579018,
-0.833021026477168, -0.211470492567308, 1.48353691981021,
0.421968013870802, 2.63739845069911, -0.61002505397242, 0.66674880256898,0.66674880256898, 2.93190487813111))
我想运行一系列回归,迭代这些向量
officer_race = c("black", "white", "hispanic")
primary_ind<-c("prop_white_scale","prop_hisp_scale","prop_black_scale")
outcome<-c("any_black_uof","any_white_uof","any_hisp_uof","any_black_arrest","any_white_arrest","any_hisp_arrest","any_black_stop","any_white_stop","any_hisp_stop")
同样值得注意的是,我想使用最优惠的套餐回归看起来像这个
feols(any_black_uof~ prop_white_scale,data=analysis[analysis$off_race =="black"])
feols(any_black_uof~ prop_white_scale,data=analysis[analysis$off_race =="white"])
feols(any_black_uof~ prop_white_scale,data=analysis[analysis$off_race=="hispanic"])
feols(any_black_uof~ prop_hisp_scale,data=analysis[analysis$off_race =="black"])
feols(any_black_uof~ prop_hisp_scale,data=analysis[analysis$off_race =="white"])
等等。遍历所有可能的组合并创建lm对象列表。
这可能吗?
由于您没有提供示例数据,我使用mtcars
数据集作为示例数据集。
在您的示例中,我使用的cyl
变量等效于race
。
primary_ind <- c("mpg", "gear", "disp")
outcome <- c("hp", "wt")
result <- lapply(split(mtcars, mtcars$cyl), function(x) {
sapply(primary_ind, function(y) {
sapply(outcome, function(z) {
lm(paste(y, z, sep = "~"), x)
}, simplify = FALSE)
}, simplify = FALSE)
})
result
首先,我们通过cyl
值来split
数据,这样我们就有了每个唯一值(4、6和8(的(3(列表。然后,对于每个单独的数据集,在primary_ind
和outcome
值上循环,并对每个组合应用lm
。
sapply
和simplify = FALSE
有助于识别每个模型的primary_ind
和outcome
值,因为它们保存在列表的名称中。
您可以使用内置的多种估计工具:请参阅专用的渐晕图。您还需要了解此处介绍的公式扩展工具。
您似乎想要迭代不同解释变量和因变量的数据子集。使用split
作为子集,sw
作为解释变量,c()
作为因变量。
这里有一个可重复的例子:
library(fixest)
base = setNames(iris, c("y1", "y2", "x1", "x2", "species"))
lhs = c("y1", "y2")
rhs = c("x1", "x2")
mult_est = feols(.[lhs] ~ sw(.[, rhs]), base, split = ~ species)
etable(mult_est)
#> mult_est.1 mult_est.2 mult_est.3 mult_est.4
#> Sample (species) setosa setosa setosa setosa
#> Dependent Var.: y1 y1 y2 y2
#>
#> Constant 4.213*** (0.4156) 4.777*** (0.1239) 2.861*** (0.4564) 3.222*** (0.1349)
#> x1 0.5423. (0.2823) 0.3879 (0.3100)
#> x2 0.9302. (0.4637) 0.8372 (0.5049)
#> ________________ _________________ _________________ _________________ _________________
#> S.E. type IID IID IID IID
#> Observations 50 50 50 50
#> R2 0.07138 0.07734 0.03158 0.05417
#> Adj. R2 0.05204 0.05812 0.01140 0.03447
#>
#> mult_est.5 mult_est.6 mult_est.7 mult_est.8
#> Sample (species) versicolor versicolor versicolor versicolor
#> Dependent Var.: y1 y1 y2 y2
#>
#> Constant 2.408*** (0.4463) 4.045*** (0.4229) 1.175** (0.3421) 1.373*** (0.2296)
#> x1 0.8283*** (0.1041) 0.3743*** (0.0798)
#> x2 1.426*** (0.3155) 1.054*** (0.1713)
#> ________________ __________________ _________________ __________________ _________________
#> S.E. type IID IID IID IID
#> Observations 50 50 50 50
#> R2 0.56859 0.29862 0.31419 0.44089
#> Adj. R2 0.55960 0.28401 0.29990 0.42925
#>
#> mult_est.9 mult_est.10 mult_est.11 mult_est.12
#> Sample (species) virginica virginica virginica virginica
#> Dependent Var.: y1 y1 y2 y2
#>
#> Constant 1.060* (0.4668) 5.269*** (0.6556) 1.673*** (0.4310) 1.695*** (0.2921)
#> x1 0.9957*** (0.0837) 0.2343** (0.0773)
#> x2 0.6508* (0.3207) 0.6314*** (0.1429)
#> ________________ __________________ _________________ _________________ __________________
#> S.E. type IID IID IID IID
#> Observations 50 50 50 50
#> R2 0.74688 0.07902 0.16084 0.28915
#> Adj. R2 0.74161 0.05983 0.14335 0.27434
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1