2
I’m using the function svymean() package Survey but when the data have missings ("Als"), the mean is not calculated.
Please, someone knows how to fix this?
2
I’m using the function svymean() package Survey but when the data have missings ("Als"), the mean is not calculated.
Please, someone knows how to fix this?
2
The function svymean has an argument na.rmwhich by default is FALSE, ie if you do not quote the argument in the function call, it will use the value FALSE. If you do
svymean(..., na.rm = T)
It should do the calculation excluding the observations with Missing, which should solve your problem.
1
Daniel’s answer is correct: just add the parameter na.rm = TRUE. I just add that care should be taken when using this parameter when we analyze more than 1 variable at the same time (I do not know if this is the case), because the survey removes the observation you have NA in at least 1 of the variables analyzed, even if in another variable it is not Missing. For example:
require(survey)
exemplo <- data.frame(ID = 1:10, var = rnorm(10), var2 = rnorm(10, 5), peso = rchisq(10, 5))
exemplo[10, 2] <- NA # Acrescento um missing na variável var
amostra <- svydesign(ids = ~ ID, data = exemplo, weights = ~ peso)
svymean(~ var + var2, design = amostra, na.rm = TRUE)
svymean(~ var2, design = amostra, na.rm = TRUE)
In the first svymean we get:
mean SE
var 0.56224 0.3166
var2 5.07549 0.4867
Already in the second (only for var2):
mean SE
var2 5.0833 0.4393
Browser other questions tagged r
You are not signed in. Login or sign up in order to post.