简体   繁体   中英

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.

I am new to R, and read documentation about lavaan.survey and svydesign to learn about the code. 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? Thanks a lot!

From the results of summary(r$wreg) , it looks like your weights column is a factor, and not a numeric vector. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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