Pearson correlation coefficient in packet survey R

Sorry if this is really obvious, but I don’t see how to perform a simple Pearson correlation between the two variables in the survey package. My data has striations, so this would be equivalent to finding r for api00 and api99 in apistrat.

library(survey)
data(api)

dstrat <- svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)

I'm sure there should be an easy way to do this using svyvar or svyglm or something, but I can’t see it?

+4
source share
3 answers

You can use svyvarto estimate the variance covariance matrix, and then scale it to correlation:

library(survey)
data(api)

dstrat <- svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
v <- svyvar(~api00+api99, dstrat)

as.matrix(v)
cov2cor(as.matrix(v))

This works for any number of correlations and any design.

+3
source
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
summary(svyglm(api00~ell+meals+mobility, design=dstrat),correlation=T)
0
source

, , - -, , svymean svyvar.

dstrat2 <- transform(dstrat, 
                     z_api99 = (api99 - svymean(~api99,    dstrat))/sqrt(svyvar(~api99, dstrat)), 
                     z_api00 = (api00 - svymean(~api00, dstrat))/sqrt(svyvar(~api00, dstrat))) 

svyglm(z_api99 ~ z_api00, dstrat2)$coefficients

9.759047e-01, , :

library(weights)
wtd.cor(apistrat$api99, apistrat$api00, weight = apistrat$pw)

, . -, . , , , .

- , , , .

0

Source: https://habr.com/ru/post/1621300/


All Articles