0

我想从犯罪数据集中进行综合控制。我已经使用相同的数据对另一个结果执行相同的方法,并且我能够毫无问题地做到这一点。

在第一种情况下,我的数据集有 1888 个观测值,结果是一个名为“armas”的变量。对于第二种和有问题的情况,我同时使用了 left_join() 和 select() 来合并两个数据集,因此我的新结果变量是“Total”,同时保持相同的 1888 个观察值。这是数据集头:

  Fecha               Entidad                id Cortas Largas Total
  <dttm>              <chr>               <int>  <dbl>  <dbl> <dbl>
1 2017-01-01 00:00:00 Aguascalientes          1      0      0     0
2 2017-01-01 00:00:00 Baja California         2     26     36    62
3 2017-01-01 00:00:00 Baja California Sur     3      0      0     0
4 2017-01-01 00:00:00 Campeche                4      2      0     2
5 2017-01-01 00:00:00 Chiapas                 5      0      1     1
6 2017-01-01 00:00:00 Chihuahua               6      0      1     1

有问题的代码如下:


CScanjes <-
  
  canjesCS %>%
  
  # initial the synthetic control object
  synthetic_control(outcome = Total, # outcome
                    unit = id, # unit index in the panel data
                    time = Fecha, # time index in the panel data
                    i_unit = 7, # unit where the intervention occurred
                    i_time = as.Date("2019-01-01"), # time period when the intervention occurred
                    generate_placebos=T # generate placebo synthetic controls (for inference)
  ) %>%
  # predictores
  generate_predictor(time_window = as.Date("2017-01-01"):as.Date("2018-11-01"),
                     cortas = mean(`Cortas`, na.rm = T),
                     largas = mean(`Largas`, na.rm = T)) %>%
  ###########
generate_predictor(time_window = as.Date("2017-01-01"),
                   canjes1 = Total) %>%
  generate_predictor(time_window = as.Date("2017-03-01"),
                     canjes3 = Total) %>%
  generate_predictor(time_window = as.Date("2017-05-01"),
                     canjes5 = Total) %>%
  generate_predictor(time_window = as.Date("2017-07-01"),
                     canjes7 = Total) %>%
  generate_predictor(time_window = as.Date("2017-09-01"),
                     canjes9 = Total) %>%
  generate_predictor(time_window = as.Date("2017-11-01"),
                     canjes11 = Total) %>%
  generate_predictor(time_window = as.Date("2018-01-01"),
                     canjes13 = Total) %>%
  generate_predictor(time_window = as.Date("2018-03-01"),
                     canjes15 = Total) %>%
  generate_predictor(time_window = as.Date("2018-05-01"),
                     canjes17 = Total) %>%
  generate_predictor(time_window = as.Date("2018-07-01"),
                     canjes19 = Total) %>%
  generate_predictor(time_window = as.Date("2018-09-01"),
                     canjes21 = Total) %>%
  generate_predictor(time_window = as.Date("2018-11-01"),
                     canjes23 = Total) %>%
  
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window = as.Date("2017-01-01"):as.Date("2018-12-01"), # time to use in the optimization task
                   margin_ipop = 0.02,sigf_ipop = 7,bound_ipop = 5 # optimizer options
  ) %>%
  
  # Generate the synthetic control
  generate_control()

这与我用于第一个合成控制的那个非常相似,但是这个向我显示了以下错误:

Error: Problem with `mutate()` column `.predictors`.
i `.predictors = list(pred_trt, pred_cnt)`.
i `.predictors` must be size 118 or 1, not 2.
4

0 回答 0