Skip to contents

Complex-survey GLM constructor (single-shot wrapper that builds a design and fits a svyglm in one call). Cluster-robust SEs via the design.

Usage

morie_survey_complex_glm(
  df,
  formula,
  weight_col,
  family = "gaussian",
  cluster_col = NULL,
  strata_col = NULL
)

Arguments

df

A data.frame holding the (unit-level) sample data plus any weight/strata/cluster/domain columns referenced by name.

formula

A formula (e.g. y ~ x1 + x2) for survey-weighted regression / GLM.

weight_col

Character; column name of the design weight variable in df.

family

A family object (e.g. stats::binomial()) passed to the survey-weighted GLM.

cluster_col

Character; column name of the cluster identifier in df.

strata_col

Character; column name of the stratum identifier in df.

Value

A svyglm object (survey-weighted GLM fit).

Examples

set.seed(1)
df <- data.frame(y = rnorm(40), x = rnorm(40), w = runif(40, 0.5, 2))
str(morie_survey_complex_glm(df, y ~ x, "w"), max.level = 1)
#> List of 33
#>  $ coefficients     : Named num [1:2] 0.11 0.123
#>   ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
#>  $ residuals        : Named num [1:40] -0.716 0.105 -1.031 1.417 0.305 ...
#>   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
#>  $ fitted.values    : Named num [1:40] 0.0893 0.0783 0.1955 0.1782 0.0246 ...
#>   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
#>  $ effects          : Named num [1:40] -0.76 0.713 -0.857 1.679 0.417 ...
#>   ..- attr(*, "names")= chr [1:40] "(Intercept)" "x" "" "" ...
#>  $ R                : num [1:2, 1:2] -6.325 0 -0.542 5.778
#>   ..- attr(*, "dimnames")=List of 2
#>  $ rank             : int 2
#>  $ qr               :List of 5
#>   ..- attr(*, "class")= chr "qr"
#>  $ family           :List of 12
#>   ..- attr(*, "class")= chr "family"
#>  $ linear.predictors: Named num [1:40] 0.0893 0.0783 0.1955 0.1782 0.0246 ...
#>   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
#>  $ deviance         : num 28.3
#>  $ aic              : logi NA
#>  $ null.deviance    : num 28.8
#>  $ iter             : int 2
#>  $ weights          : Named num [1:40] 0.68 1.352 0.858 1.225 1.336 ...
#>   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
#>  $ prior.weights    : Named num [1:40] 0.68 1.352 0.858 1.225 1.336 ...
#>   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
#>  $ df.residual      : num 38
#>  $ df.null          : int 39
#>  $ y                : Named num [1:40] -0.626 0.184 -0.836 1.595 0.33 ...
#>   ..- attr(*, "names")= chr [1:40] "1" "2" "3" "4" ...
#>  $ converged        : logi TRUE
#>  $ boundary         : logi FALSE
#>  $ model            :'data.frame':	40 obs. of  3 variables:
#>   ..- attr(*, "terms")=Classes 'terms', 'formula'  language y ~ x
#>   .. .. ..- attr(*, "variables")= language list(y, x)
#>   .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#>   .. .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. ..- attr(*, "term.labels")= chr "x"
#>   .. .. ..- attr(*, "order")= int 1
#>   .. .. ..- attr(*, "intercept")= int 1
#>   .. .. ..- attr(*, "response")= int 1
#>   .. .. ..- attr(*, ".Environment")=<environment: 0x555822617c90> 
#>   .. .. ..- attr(*, "predvars")= language list(y, x)
#>   .. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
#>   .. .. .. ..- attr(*, "names")= chr [1:3] "y" "x" "(weights)"
#>  $ call             : language svyglm(formula = fml, design = design, family = fam)
#>  $ formula          :Class 'formula'  language y ~ x
#>   .. ..- attr(*, ".Environment")=<environment: 0x555822617c90> 
#>  $ terms            :Classes 'terms', 'formula'  language y ~ x
#>   .. ..- attr(*, "variables")= language list(y, x)
#>   .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#>   .. .. ..- attr(*, "dimnames")=List of 2
#>   .. ..- attr(*, "term.labels")= chr "x"
#>   .. ..- attr(*, "order")= int 1
#>   .. ..- attr(*, "intercept")= int 1
#>   .. ..- attr(*, "response")= int 1
#>   .. ..- attr(*, ".Environment")=<environment: 0x555822617c90> 
#>   .. ..- attr(*, "predvars")= language list(y, x)
#>   .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
#>   .. .. ..- attr(*, "names")= chr [1:3] "y" "x" "(weights)"
#>  $ data             :'data.frame':	40 obs. of  4 variables:
#>  $ offset           : NULL
#>  $ control          :List of 3
#>  $ method           : chr "glm.fit"
#>  $ contrasts        : NULL
#>  $ xlevels          : Named list()
#>  $ naive.cov        : num [1:2, 1:2] 0.02522 -0.00257 -0.00257 0.02995
#>   ..- attr(*, "dimnames")=List of 2
#>  $ cov.unscaled     : num [1:2, 1:2] 0.018142 0.000503 0.000503 0.019629
#>   ..- attr(*, "dimnames")=List of 2
#>  $ survey.design    :List of 9
#>   ..- attr(*, "class")= chr [1:2] "survey.design2" "survey.design"
#>  - attr(*, "class")= chr [1:3] "svyglm" "glm" "lm"