在调查数据(SPSS数据集)上运行逻辑回归(svyglm)时出现两个月前我没有得到的子集错误 [英] Getting a subset error I did not get two months ago when running logistic regression (svyglm) on survey data (SPSS dataset)

查看:128
本文介绍了在调查数据(SPSS数据集)上运行逻辑回归(svyglm)时出现两个月前我没有得到的子集错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新运行了大约两个月前没有任何错误的脚本.

I re-run script that previously worked with no errors about two months ago.

我使用haven包上传了一个(非公开的和专有的)SPSS数据集和用于分析复杂调查数据的调查包.

I used the haven package to upload an (non-public and proprietary) SPSS dataset and the survey package to analyze complex survey data.

但是,现在,即使我运行了一个简单的逻辑回归,其中两个变量都是虚拟变量(编码为0代表否,编码为1代表是)...类似这样的东西...

Now, however, when I run even a simple logistic regression where both variables are dummies (coded 0 for no and 1 for yes)...something like this...

f <- read_sav("~/data.sav")
fsd <- svydesign(ids=~1, data=f, weights=~f$weight)
model <- svyglm(exclhlth~male,design=fsd,family=quasibinomial())

...我收到以下错误:

...I get the following errors:

Error: Must subset elements with a valid subscript vector.
x Subscript has the wrong type `omit`.
ℹ It must be logical, numeric, or character.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/vctrs_error_subscript_type>
Must subset elements with a valid subscript vector.
x Subscript has the wrong type `omit`.
ℹ It must be logical, numeric, or character.
Backtrace:
 1. survey::svyglm(exclhlth ~ male, design = fsd, family = quasibinomial())
 2. survey:::svyglm.survey.design(...)
 4. survey:::`[.survey.design2`(design, -nas, )
 5. base::`[.data.frame`(x$variables, i, ..1, drop = FALSE)
 7. vctrs:::`[.vctrs_vctr`(xj, i)
 8. vctrs:::vec_index(x, i, ...)
 9. vctrs::vec_slice(x, i)
Run `rlang::last_trace()` to see the full context.

我尝试在将男性设置为因数并将两个都设置为因数的情况下运行它.我遇到同样的错误.

I've tried running it where I set male as a factor, and where both are set as factors. I get the same errors.

自两个月前以来,我已经更新了 R Rstudio 以及避风港和调查配套.因此,我猜测情况有所变化,但是我不确定该怎么办.

Since two months ago, I have updated R, Rstudio and both the haven and survey packages. So, I'm guessing that something changed but I am not sure what to do.

我只是从去年年底开始从 SPSS 过渡到 R ,所以在此先感谢您的指导,对于新手的错误也深表歉意!

I only started transitioning from SPSS to R late last year, so I thank you in advance for any guidance and apologize in advance for newbie mistakes!

推荐答案

好吧,您的问题似乎是RStudio数据导入功能正在创建劫持下标( [)操作的类.这是以前发生的,当RStudio从创建 data.frame 切换到 tbl 对象时,但是使用 as.data.frame()就足够了调用 svydesign()之前.

Ok, your problem seems to be that the RStudio data import functions are creating classes that hijack the subscript ([) operation. This has happened before, when RStudio switched from creating data.frame to tbl objects, but then it was sufficient to use as.data.frame() before calling svydesign().

在提供新版本的 survey 软件包之前,您可以尝试使用 foreign :: read.spss 而不是 haven :: read_sav ?

Until a new version of the survey package is available, can you try using foreign::read.spss instead of haven::read_sav?

(此外,如果您能提出一个不太机密的示例并将其发送给维护者,我很确定他会很感激.)

(Also, if you could come up with a less-confidential example and send it to the maintainer, I'm fairly sure he'd be grateful.)

更新:问题是 na.omit 的输出具有类 omit ,而某些变量具有类haven_labelled ,而 haven_labelled 的子集运算符对于其参数的类非常挑剔:它必须是纯整数或逻辑,没有类.

Update: the issue is that the output of na.omit has class omit, and some of the variables have class haven_labelled, and the subsetting operator for haven_labelled is very fussy about the class of its arguments: it has to be plain integer or logical, without a class.

标记为类的帮助建议使用 haven :: as_factor haven :: zap_labels 将这些标记矢量强制为标准R课.

The help for the labelled class suggests using haven::as_factor or haven::zap_labels to coerce these labelled vectors to a standard R class.

进一步更新:我为 haven 包提交了一个github问题,该问题已移至 vctrs 包中,因此这种行为很可能进行更改.

Further update: I filed a github issue for the haven package, which was moved to the vctrs package, so this behaviour is likely to be changed.

进一步的更新:此已修复 vctrs

Further further update: This has been fixed in the development version of vctrs

这篇关于在调查数据(SPSS数据集)上运行逻辑回归(svyglm)时出现两个月前我没有得到的子集错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆