augsynth:交错综合控制 - 请求数字变量

问题描述 投票:0回答:1

我正在尝试使用 augsynth 包运行综合控制以实现交错采用。我正在密切关注 Github 页面 (https://github.com/ebenmichael/augsynth/blob/master/vignettes/multisynth-vignette.md)。但是,我发现了一个我不知道如何修复的错误。所有应该是数字的变量都是数字。但错误显示有所不同。

也许我误解了错误。

head(t)
# A tibble: 6 × 6
# Groups:   state_abb [1]
  state_abb  year            y post_fts fts_year treated
  <fct>     <dbl>        <dbl>    <dbl>    <dbl>   <dbl>
1 AL         2013            0        0     2022       0
2 AL         2014            0        0     2022       0
3 AL         2015            0        0     2022       0
4 AL         2016           34        0     2022       0
5 AL         2017           26        0     2022       0
6 AL         2018           19        0     2022       0

> ppool_syn <- multisynth(y ~ treated, state_abb, year, nu = 0.5, t)
Adding missing grouping variables: `state_abb`
Adding missing grouping variables: `state_abb`
Error in colMeans(X[!is.finite(trt), , drop = F], na.rm = TRUE): 
  'x' must be numeric`

我尝试再次将它们转换为数字。但我遇到了同样的错误。

您可以使用以下命令重新创建数据框:

# Create the dataframe
df <- data.frame(
  state_abb = c(rep("AL", 10), rep("AR", 10), rep("AZ", 10), rep("CA", 10), 
                rep("CO", 10), rep("FL", 10), rep("GA", 10), rep("IA", 10), 
                rep("ID", 10), rep("IL", 10), rep("IN", 10), rep("KS", 10), 
                rep("KY", 10), rep("LA", 10), rep("MD", 10), rep("ME", 10)),
  year = rep(2013:2022, 16),
  y = c(
    0, 0, 0, 34, 26, 19, 28, 39, 45, 12, # AL
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        # AR
    0, 0, 0, 11, 31, 27, 24, 45, 35, 16, # AZ
    0, 0, 0, 0, 46, 83, 146, 247, 314, 213, # CA
    0, 0, 0, 0, 0, 0, 0, 0, 27, 14,      # CO
    0, 19, 77, 250, 309, 341, 382, 430, 218, 92, # FL
    0, 15, 32, 11, 41, 27, 40, 76, 55, 21, # GA
    0, 0, 0, 0, 0, 11, 0, 22, 0, 0,      # IA
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        # ID
    0, 0, 42, 298, 550, 651, 660, 839, 625, 435, # IL
    0, 0, 0, 0, 51, 75, 112, 82, 29, 11, # IN
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,        # KS
    0, 0, 36, 57, 76, 48, 66, 55, 22, 11, # KY
    0, 0, 0, 0, 17, 26, 80, 116, 79, 0,  # LA
    0, 10, 62, 312, 333, 257, 129, 67, 10, 0, # MD
    0, 0, 0, 0, 13, 26, 12, 23, 0, 0     # ME
  ),
  post_fts = c(
    rep(0, 9), 1,                       # AL
    rep(0, 10),                         # AR
    rep(0, 8), 1, 1,                    # AZ
    rep(0, 10),                         # CA
    rep(0, 6), 1, 1, 1, 1,              # CO
    rep(0, 10),                         # FL
    rep(0, 10),                         # GA
    rep(0, 10),                         # IA
    rep(0, 10),                         # ID
    rep(0, 10),                         # IL
    rep(0, 10),                         # IN
    rep(0, 10),                         # KS
    rep(0, 10),                         # KY
    rep(0, 9), 1,                       # LA
    rep(0, 5), 1, 1, 1, 1, 1,           # MD
    rep(0, 8), 1, 1                     # ME
  ),
  fts_year = c(
    rep(2022, 10),  # AL
    rep(Inf, 10),   # AR
    rep(2021, 10),  # AZ
    rep(Inf, 10),   # CA
    rep(2019, 10),  # CO
    rep(Inf, 10),   # FL
    rep(Inf, 10),   # GA
    rep(Inf, 10),   # IA
    rep(Inf, 10),   # ID
    rep(Inf, 10),   # IL
    rep(Inf, 10),   # IN
    rep(Inf, 10),   # KS
    rep(Inf, 10),   # KY
    rep(2022, 10),  # LA
    rep(2018, 10),  # MD
    rep(2021, 10)   # ME
  ),
  treated = c(
    rep(0, 9), 1,                       # AL
    rep(0, 10),                         # AR
    rep(0, 8), 1, 1,                    # AZ
    rep(0, 10),                         # CA
    rep(0, 6), 1, 1, 1, 1,              # CO
    rep(0, 10),                         # FL
    rep(0, 10),                         # GA
    rep(0, 10),                         # IA
    rep(0, 10),                         # ID
    rep(0, 10),                         # IL
    rep(0, 10),                         # IN
    rep(0, 10),                         # KS
    rep(0, 10),                         # KY
    rep(0, 9), 1,                       # LA
    rep(0, 5), 1, 1, 1, 1, 1,           # MD
    rep(0, 8), 1, 1                     # ME
  )
)

# Check the dataframe
head(df, 20)

r
1个回答
0
投票

您的问题是由于您的 t 对象是分组的 tibble 引起的。使用您的示例 df:

library(augsynth)
library(dplyr)

t <- tibble(df) |>
  group_by(state_abb)

ppool_syn <- multisynth(y ~ treated, state_abb, year, nu = 0.5, t)

添加缺失的分组变量:

state_abb

添加缺失的分组变量:
state_abb

colMeans(X[!is.finite(trt), , drop = F], na.rm = TRUE) 中的错误:
“x”必须是数字

t <- ungroup(t)

ppool_syn <- multisynth(y ~ treated, state_abb, year, nu = 0.5, t)

ppool_syn
# Call:
# multisynth(form = y ~ treated, unit = state_abb, time = year, 
#     data = t, nu = 0.5)
# 
# Average ATT Estimate: -32.464
© www.soinside.com 2019 - 2024. All rights reserved.