R-在用户定义模型上的度量列表上循环
我目前在差异模型方面存在差异,并且希望以有效的方式度量同一模型的不同度量 例如,我有一个数据框,其中的列表示行驶里程、工作小时数、状态、组和时间 目前,我在其中复制和粘贴每个度量的模型的代码:R-在用户定义模型上的度量列表上循环,r,function,statistics,lapply,R,Function,Statistics,Lapply,我目前在差异模型方面存在差异,并且希望以有效的方式度量同一模型的不同度量 例如,我有一个数据框,其中的列表示行驶里程、工作小时数、状态、组和时间 目前,我在其中复制和粘贴每个度量的模型的代码: # Create DID models model_miles <- lm(df$miles_driven ~ df$Group + df$Time + df$Group * df$Time, dat
# Create DID models
model_miles <- lm(df$miles_driven ~ df$Group
+ df$Time
+ df$Group * df$Time, data = df)
model_hours <- lm(df$hours_worked ~ df$Group
+ df$Time
+ df$Group * df$Time, data = df)
# Select results using clustered standard errors. The purpose of this is to
# avoid making distributional assumptions about the errors in the models.
results_miles <- clubSandwich::coef_test(model_miles,
vcov = "CR2",
cluster = df$state,
test = "Satterthwaite")
results_hours <- clubSandwich::coef_test(model_hours,
vcov = "CR2",
cluster = df$state,
test = "Satterthwaite")
results <- data.table::rbindlist(list(results_miles, results_hours))
View(results)
#创建DID模型
model_miles这听起来像是purrr的map()
以及tidyr的unnest()
的工作
下一次,构建一个应用程序可能会有所帮助,但我们可以从这里开始
库(tidyverse)
列车%
变异(模型=映射(公式,~lm(.x,数据=mtcars)))
火车
#>#tibble:2 x 3
#>度量公式模型
#>
#>每加仑1英里
#>2显示
请注意,我在这里设置了一个列,其中包含我们将用作模型中预测数量的度量的名称,然后是包含建模公式的列。你可以在那里使用paste()
和mutate()
甚至更喜欢,而不必重新键入预测数量的名称。然后使用purrr的map()
为每个预测量拟合模型。您现在有了一个包含模型的列
接下来,是测试所有回归系数的时候了。请注意map()
是如何获取其参数的:首先是映射到的数量(模型),然后是应用到每个模型的函数(来自clubSandwich的函数),最后是需要传递到该函数的其他参数
测试%
变异(coeffs=map(模型,clubSandwich::coef_测试,“CR2”,“Satterthwaite”,“All”,mtcars$cyl))
测验
#>#A tibble:2 x 4
#>度量公式模型系数
#>
#>每加仑1英里
#>2显示
我们做到了!最后一步是使用tidyr的unnest()
,这样我们就可以很容易地从回归系数测试中得到所有这些量
测试%>%
不耐烦(系数)
#>#tibble:22 x 5
#>米制贝塔东南测向p_Satt
#>
#>1 mpg 12.3 16.3 1.52 0.550
#>每加仑2英里-0.111 1.44 2.00 0.945英里
#>3 mpg 0.0133 0.0159 1.34 0.526
#>4 mpg-0.0215 0.0330 1.82 0.588
#>5英里每加仑0.787 0.493 2.05 0.248
#>每加仑6英里-3.72 2.05 1.33 0.270英里
#>7英里每加仑0.821 0.388 1.34 0.228
#>8英里每加仑0.3181.55 1.74 0.859
#>9 mpg 2.52 1.25 1.73 0.202
#>10英里每加仑0.6551.84 1.73 0.761
#> # ... 还有12排
由(v0.2.0)于2018年4月26日创建
#list of metrics
metrics <- c("miles_driven", "hours_worked")
udf <- function(metric, dataframe){
# Create DID model
model <- lm(dataframe$metric ~ df$Group
+ dataframe$Time
+ dataframe$Group * df$Time, data = dataframe)
# Select results using clustered standard errors. The purpose of this
is to
# avoid making distributional assumptions about the errors in the
models.
results_miles <- clubSandwich::coef_test(model_miles,
vcov = "CR2",
cluster = dataframe$state,
test = "Satterthwaite")[4,]
View(results)
}
lapply(metrics, udf)