gpt4 book ai didi

r - 在 R Synth 包中使用 dataprep 函数时出现莫名其妙的错误

转载 作者:行者123 更新时间:2023-12-05 00:21:05 28 4
gpt4 key购买 nike

我正在尝试使用 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

)

最佳答案

评论太长了。

您的 dplyr陈述:

World <- World %>% ...

转换 World来自 data.frametbl_df对象(阅读 dplyr 上的文档)。不幸的是,这会导致 mode(World[,"idno"])返回 list ,而不是 numeric和数字 unit.variable 的测试失败。

您可以通过使用解决此问题
`World <- as.data.frame(World)`

就在调用 dataprep(...) 之前.

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

关于r - 在 R Synth 包中使用 dataprep 函数时出现莫名其妙的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32286680/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com