【问题标题】:Baffling error using dataprep function in R Synth package在 R Synth 包中使用 dataprep 函数时出现莫名其妙的错误
【发布时间】:2015-08-29 13:40:08
【问题描述】:

我正在尝试使用 R 中的“Synth”包来探索某些政变对发生政变的国家/地区的经济增长的影响,但我被一个我无法理解的错误挂断了。当我尝试运行dataprep() 时,我得到以下信息:

Error in dataprep(foo = World, predictors = c("rgdpe.pc", "population.ln",  : 

 unit.variable not found as numeric variable in foo.

这令人费解,因为我的数据框 World 确实包含一个名为“idno”的数字 id,在对 dataprep() 的调用中指定。

这是我正在使用的脚本。它从 GitHub 提取包含必要数据的 .csv。最后一步 --- 调用 dataprep() --- 是出现错误的地方。我希望能帮助我弄清楚为什么会出现这个错误以及如何避免它,以便我可以继续阅读 synth() 部分。

library(dplyr)
library(Synth)

# DATA INGESTION AND TRANSFORMATION

World <- read.csv("https://raw.githubusercontent.com/ulfelder/coups-and-growth/master/data.raw.csv", stringsAsFactors=FALSE)

World$rgdpe.pc = World$rgdpe/World$pop # create per capita version of GDP (PPP)
World$idno = as.numeric(as.factor(World$country))  # create numeric country id
World$population.ln = log(World$population/1000)  # population size in 1000s, logged
World$trade.ln = log(World$trade)  # trade as % of GDP, logged
World$civtot.ln = log1p(World$civtot)  # civil conflict scale, +1 and logged
World$durable.ln = log1p(World$durable)  # political stability, +1 and logged
World$polscore = with(World, ifelse(polity >= -10, polity, NA)) # create version of Polity score that's missing for -66, -77, and -88
World <- World %>%  # create clocks counting years since last coup (attempt) or 1950, whichever is most recent
    arrange(countrycode, year) %>%
    mutate(cpt.succ.d = ifelse(cpt.succ.n > 0, 1, 0),
           cpt.any.d = ifelse(cpt.succ.n > 0 | cpt.fail.n > 0, 1, 0)) %>%
    group_by(countrycode, idx = cumsum(cpt.succ.d == 1L)) %>%
    mutate(cpt.succ.clock = row_number()) %>%
    ungroup() %>%
    select(-idx) %>%
    group_by(countrycode, idx = cumsum(cpt.any.d == 1L)) %>%
    mutate(cpt.any.clock = row_number()) %>%
    ungroup() %>%
    select(-idx) %>%
    mutate(cpt.succ.clock.ln = log1p(cpt.succ.clock), # include +1 log versions
           cpt.any.clock.ln = log1p(cpt.any.clock))

# THAILAND 2006

THI.coup.year = 2006

THI.years = seq(THI.coup.year - 5, THI.coup.year + 5)
# Get names of countries that had no coup attempts during window analysis will cover. If you wanted to restrict the comparison to a
# specific region or in any other categorical way, this would be the place to do that as well.
THI.controls <- World %>%
    filter(year >= min(THI.years) & year <= max(THI.years)) %>% # filter to desired years
    group_by(idno) %>%  # organize by country
    summarise(coup.ever = sum(cpt.any.d)) %>%  # get counts by country of years with coup attempts during that period
    filter(coup.ever==0) %>%  # keep only the ones with 0 counts
    select(idno)  # cut down to country names
THI.controls = unlist(THI.controls)  # convert that data frame to a vector
names(THI.controls) = NULL  # strip the vector of names

THI.synth.dat <- dataprep(

    foo = World,

    predictors = c("rgdpe.pc", "population.ln", "trade.ln", "fcf", "govfce", "energy.gni", "polscore", "durable.ln", "cpt.any.clock.ln", "civtot.ln"),
    predictors.op = "mean",
    time.predictors.prior = seq(from = min(THI.years), to = THI.coup.year - 1),

    dependent = "rgdpe.pc",

    unit.variable = "idno",
    unit.names.variable = "country",
    time.variable = "year",

    treatment.identifier = unique(World$idno[World$country=="Thailand"]),
    controls.identifier = THI.controls,

    time.optimize.ssr = seq(from = THI.coup.year, to = max(THI.years)),
    time.plot = THI.years

)

【问题讨论】:

  • 嗨@ulfelder,你成功调整你的代码了吗?您是否介意将问题中的最后一个共享为 EDIT 或至少是您将绘制差异的部分?我想出了dataprep,但不知道如何绘制它。非常感谢!
  • @Googme,有效的代码位于公共 GitHub 存储库中:github.com/ulfelder/coups-and-growth

标签: r


【解决方案1】:

评论太长了。

您的dplyr 声明:

World <- World %>% ...

Worlddata.frame 转换为tbl_df 对象(阅读dplyr 上的文档)。不幸的是,这会导致 mode(World[,"idno"]) 返回 list,而不是 numeric,并且对数字 unit.variable 的测试失败。

您可以使用

解决此问题
`World <- as.data.frame(World)`

就在致电dataprep(...) 之前。

不幸的是(再次)你现在得到一个不同的错误,这可能是由于你的 dplyr 语句的逻辑。

【讨论】:

  • 知道了。我认为新错误与controls.identifier= 位中包含的所有国家/地区并非在THI.years 涵盖的所有年份中都观察到这一事实有关。我将在产生THI.controls 的过程中对该标准进行一些过滤,看看是否能解决问题。无论如何,非常感谢您的帮助。
  • 我现在已经让dataprep() 运行了。它需要对数据进行大量预处理,以确保传递给foo 的数据框仅包括具有足够非 NA 值的平衡面板,用于治疗前和治疗后年份的所有病例,以生成必要的统计数据.这不一定是坏事,但从有关“合成器”的文档和论文中并不清楚。
猜你喜欢
  • 2021-11-30
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
相关资源
最近更新 更多