简体   繁体   English

如何在 R 中使用样本权重

[英]How to use sample weights in R

I am planning to fit a Multi-Group Confirmatory Factor Analysis about views on ethical matters.我计划对关于道德问题的观点进行多组确认因素分析。 I will compare people from the regions of Wallonia and Flanders in Belgium.我将比较来自比利时瓦隆区和佛兰德斯地区的人。 My two samples need to be weighted, in order to be representative of their populations in terms of in terms of age, gender, education and party choice.我的两个样本需要加权,以便在年龄、性别、教育和政党选择方面代表他们的人口。

Sampling weights where already provided in my dataset.我的数据集中已经提供的采样权重。 I then created a variable wreg , combining weights for respondents from Wallonia and Flanders.然后我创建了一个变量wreg ,将来自瓦隆和佛兰德斯的受访者的权重结合起来。

I am new to R, and read documentation about lavaan.survey and svydesign to learn about the code.我是 R 新手,阅读了有关lavaan.surveysvydesign文档以了解代码。 However, I haven't yet succeeded in writing something correct.但是,我还没有成功地写出正确的东西。 I always get error messages about the part concerning weights.我总是收到有关重量部分的错误消息。 Apparently the programme cannot read the sampling weights variable right.显然程序无法正确读取采样权重变量。

Here is the code I used:这是我使用的代码:

library(lavaan.survey)

f <- "C:/.../bges07_small.csv"
s <- read.csv(f,sep=";")

r <- s[is.na(s$flawal),]

rDesign <- svydesign(ids=~1, data=r, weights=~wreg)

model.1 <- 'ethic =~ q96_1+ q96_2 +q96_3'

fit <- cfa(model.1, data=r,ordered=c("q96_1","q96_2","q96_3"))

summary(fit, fit.measures=TRUE, modindices=FALSE,standardized=FALSE)

And this is the error message I had:这是我收到的错误消息:

Erreur dans 1/as.matrix(weights) : 
  argument non numérique pour un opérateur binaire

Any suggestion on how I should write my model with R?关于我应该如何用 R 编写模型的任何建议? Thanks a lot!非常感谢!

From the results of summary(r$wreg) , it looks like your weights column is a factor, and not a numeric vector.summary(r$wreg)的结果看来,您的权重列是一个因素,而不是数字向量。 Make sure you've read your data in correctly and that column doesn't contain any character-like values.确保您已正确读取数据,并且该列不包含任何类似字符的值。 You can manually convert it with您可以手动转换它

r$wreg <- as.numeric(r$wreg)

before running your model.在运行模型之前。 Also, those look like very large weight values.此外,那些看起来像非常大的权重值。 Are you sure they are correct?你确定他们是正确的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM