0

I have the following data and the survey design:

province <-
  read.table( text =
                "id str clu wt ue91 meanz hou85 diffhou85 smplrat
        1 1 1 4 4123 2867 26881 -24014 .25
        2 1 4 4 760 2867 4896 -2029 .25
        3 1 5 4 721 2867 3730 -863 .25
        4 1 15 4 142 2867 556 2311 .25
        5 1 18 4 187 2867 1463 1404 .25
        6 1 26 4 331 2867 1946 921 .25
        7 1 30 4 127 2867 834 2033 .25
        8 1 31 4 219 2867 932 1935 .25" ,
              header = TRUE
  )
require(survey)
province.design <-
  svydesign(
    id = ~clu ,
    strata = ~str ,
    data = province ,
    weights = ~wt ,
    
  )

> head(province.design[[7]])
  id str clu wt ue91 meanz hou85 diffhou85 smplrat
1  1   1   1  4 4123  2867 26881    -24014    0.25
2  2   1   4  4  760  2867  4896     -2029    0.25
3  3   1   5  4  721  2867  3730      -863    0.25

Now I want to extract the univariate regression coefficients without actually fitting multiple models. I tried the following code.

coef_reg=sapply(c(6:(ncol(province.design[[7]]))), function(i) 
  
{
  svyglm(ue91 ~ province.design[[7]][i], province.design)$coefficients[2];
  
  
  
}
)

But it is not working. Can anybody help me to figure this out?

This is the error I am getting:

Error in svyglm.survey.design(ue91 ~ province.design[[7]][i], province.design) : 
  all variables must be in design= argument 

Thank you for your help.

student_R123
  • 962
  • 11
  • 30
  • What exactly does "not working" mean in this case? Are you getting an error? If so, what does it say? – MrFlick Feb 11 '21 at 18:02
  • @MrFlick Thank you for the reply. I updated the question with the error – student_R123 Feb 11 '21 at 18:04
  • It sounds like the variables you are accessing through the function(i) are not sufficient to compute the coefficients... right? Are you missing a parameter in `svydesign` ? – Ben Feb 11 '21 at 20:13
  • @Ben I am not sure about that. It seems like looping over a svydesign cannot be done very easily. – student_R123 Feb 12 '21 at 01:31
  • 1
    something like `sapply( names( province )[seq( 6 , ncol( province ) ) ] , function( i ) svyglm( as.formula( paste( 'ue91 ~' , i ) ) , province.design )$coefficients[2] )` ? – Anthony Damico Feb 18 '21 at 22:03

0 Answers0