在microsynth函数“if(dum < = dum 1 + 1){:缺少需要TRUE/FALSE的值”

smtd7mpg  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(80)

有谁能帮我弄清楚为什么microsynth函数会出现错误代码?
我 * 认为 * 我已经完全按照教程描述的那样设置了数据,给出了ID变量,时间变量(数值)和二进制治疗变量。
当我运行以下代码时:

cov.var <- c("age", "sex", "ethgr2", "hh_size", "country_4", 
             "inc_imp", "nssec4", "ten1", "num_adult")
match.out <- c("ov_1", "ov_2", "ov_3", "ov_4")

synth1 <- microsynth(DS, 
                   idvar="seriali", timevar="year_2", intvar="treated", 
                   start.pre=1, end.pre=6, end.post=9, 
                   match.out=match.out, match.covar=cov.var, 
                   result.var=match.out, omnibus.var=match.out,
                   test="lower",
                   n.cores = min(parallel::detectCores(), 2))

我得到这个错误:

Error in if (dum <= dum1 + 1) { : missing value where TRUE/FALSE needed

R不提供错误发生的追溯。此外,当查看函数的源代码时,我不知道在哪里可以提供TRUE/FALSE。
我试着按照microsynth教程学习,看看是否能重现错误。但是,我得到了一个不同的错误。

data(seattledmi)
set.seed(99199)
cov.var <- c("TotalPop", "BLACK", "HISPANIC", "Males_1521", "HOUSEHOLDS", 
             "FAMILYHOUS", "FEMALE_HOU", "RENTER_HOU", "VACANT_HOU")
match.out <- c("i_felony", "i_misdemea", "i_drugs", "any_crime")
sea1 <- microsynth(seattledmi, 
                   idvar="ID", timevar="time", intvar="Intervention", 
                   start.pre=1, end.pre=12, end.post=16, 
                   match.out=match.out, match.covar=cov.var, 
                   result.var=match.out, omnibus.var=match.out,
                   test="lower",
                   n.cores = min(parallel::detectCores(), 2))
sea1
Error in colnames(newdat) : object 'newdat' not found

任何建议将不胜感激!

wa7juj8i

wa7juj8i1#

调试代码后,似乎microsynth在处理tibble对象时存在一些问题;至少这是第二个错误的来源:函数newreshape尝试运行time.tmp <- data[,timevar],当应用到tibble时,它不会提取向量(这会导致进一步向下的问题)--它应该是例如。time.tmp <- dplyr::pull(data, timevar)
即使修复了这个问题,仍然有更多的错误,所以我会简单地尝试在运行代码之前将数据转换为data.frame,这对我很有效:

sea1 <- microsynth(as.data.frame(seattledmi), 
               idvar="ID", timevar="time", intvar="Intervention", 
               start.pre=1, end.pre=12, end.post=16, 
               match.out=match.out, match.covar=cov.var, 
               result.var=match.out, omnibus.var=match.out,
               test="lower",
               n.cores = min(parallel::detectCores(), 2))

这样有用吗

rdrgkggo

rdrgkggo2#

当数据中缺少观测值时,会发生此错误。下面的最小代码示例使用seattledmi数据集生成错误:

library(microsynth)

# Delete the fifth row of the seattledmi data set
mySeattledmi <- seattledmi[-c(5),]

# Define variables as in the microsynth tutorial
cov.var <- c("TotalPop", "BLACK", "HISPANIC", "Males_1521", "HOUSEHOLDS", 
             "FAMILYHOUS", "FEMALE_HOU", "RENTER_HOU", "VACANT_HOU")
match.out <- c("i_felony", "i_misdemea", "i_drugs", "any_crime")

# Run microsynth using the mySeattledmi data and all other parameters set equal to
# ... the values used in Example 1 of the microsynth tutorial
sea5 <- microsynth(mySeattledmi, 
                   idvar="ID", timevar="time", intvar="Intervention", 
                   start.pre=1, end.pre=12, end.post=16, 
                   match.out=match.out, match.covar=cov.var, 
                   result.var=match.out, omnibus.var=match.out,
                   test="lower",
                   n.cores = min(parallel::detectCores(), 2))

现在发生的事情是,microsynth的newresform函数正在创建一个矩阵,它以数据中的intvar列为值。该矩阵的维数为n乘t,其中n等于id的数量,t等于数据中的时间段的数量,例如:在Seattledmi数据中n = 9,642和t = 16。如果任何观测结果缺失,则该矩阵将包含NA。这导致if (dum <= dum1 + 1)dum的值评估为NA和错误消息。
解决该问题的一种方法是消除任何包含缺失值的治疗组或对照组。继续前面的示例,下面的代码删除mySeattledmi数据中与小于16的ID(即end.post - start.pre + 1)观察结果。

library(dplyr)

nobsById <- mySeattledmi %>% group_by(ID) %>% summarize(nobs = n())
mySeattledmi2 <- mySeattledmi %>% subset(!(ID %in% nobsById[nobsById$nobs < 16, 1][[1]]))

# Call microsynth again this time with mySeattledmi2
sea6 <- microsynth(mySeattledmi2, 
                   idvar="ID", timevar="time", intvar="Intervention", 
                   start.pre=1, end.pre=12, end.post=16, 
                   match.out=match.out, match.covar=cov.var, 
                   result.var=match.out, omnibus.var=match.out,
                   test="lower",
                   n.cores = min(parallel::detectCores(), 2))

相关问题