ewenharrison / finalfit
1
#' Univariable survey-weighted generalised linear models
2
#'
3
#' Wrapper for \code{\link[survey]{svyglm}}. Fit a generalised linear model to
4
#' data from a complex survey design, with inverse-probability weighting and
5
#' design-based standard errors.
6
#'
7
#' @param design Survey design.
8
#' @param dependent Character vector of length 1: name of depdendent variable
9
#'   (must have 2 levels).
10
#' @param explanatory Character vector of any length: name(s) of explanatory
11
#'   variables.
12
#' @param ... Other arguments to be passed to \code{\link[survey]{svyglm}}.
13
#'
14
#' @return A list of univariable fitted model outputs. Output is of class
15
#'   \code{svyglmlist}.
16
#' @export
17
#' 
18
#' @seealso \code{\link{fit2df}, \link{finalfit_merge}}
19
#' @family finalfit model wrappers
20
#'
21
#' @examples
22
#' # Examples taken from survey::svyglm() help page. 
23
#' 
24
#' library(survey)
25
#' library(dplyr)
26
#' 
27
#' data(api)
28
#' dependent = "api00"
29
#' explanatory = c("ell", "meals", "mobility")
30
#' 
31
#' library(survey)
32
#' library(dplyr)
33
#'
34
#' data(api)
35
#' 
36
# Label data frame
37
#' apistrat = apistrat %>% 
38
#'   mutate(
39
#'   api00 = ff_label(api00, "API in 2000 (api00)"),
40
#'   ell = ff_label(ell, "English language learners (percent)(ell)"),
41
#'   meals = ff_label(meals, "Meals eligible (percent)(meals)"),
42
#'   mobility = ff_label(mobility, "First year at the school (percent)(mobility)"),
43
#'   sch.wide = ff_label(sch.wide, "School-wide target met (sch.wide)")
44
#'   )
45
#'
46
#' # Linear example
47
#' dependent = "api00"
48
#' explanatory = c("ell", "meals", "mobility")
49
#' 
50
#' # Stratified design
51
#' dstrat = svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
52
#' 
53
#' # Univariable fit
54
#' fit_uni = dstrat %>%
55
#'   svyglmuni(dependent, explanatory) %>%
56
#'   fit2df(estimate_suffix = " (univariable)")
57
#'
58
#' # Multivariable fit
59
#' fit_multi = dstrat %>%
60
#'   svyglmmulti(dependent, explanatory) %>%
61
#'   fit2df(estimate_suffix = " (multivariable)")
62
#'
63
#' # Pipe together
64
#' apistrat %>%
65
#'   summary_factorlist(dependent, explanatory, fit_id = TRUE) %>%
66
#'   ff_merge(fit_uni) %>% 
67
#'   ff_merge(fit_multi) %>% 
68
#'   select(-fit_id, -index) %>%
69
#'   dependent_label(apistrat, dependent)
70
#'
71
#' # Binomial example
72
#' ## Note model family needs specified and exponentiation if desired
73
#' 
74
#' dependent = "sch.wide"
75
#' explanatory = c("ell", "meals", "mobility")
76
#' 
77
#' # Univariable fit
78
#' fit_uni = dstrat %>%
79
#'   svyglmuni(dependent, explanatory, family = "quasibinomial") %>%
80
#'   fit2df(exp = TRUE, estimate_name = "OR", estimate_suffix = " (univariable)")
81
#'
82
#' # Multivariable fit
83
#' fit_multi = dstrat %>%
84
#'   svyglmmulti(dependent, explanatory, family = "quasibinomial") %>%
85
#'   fit2df(exp = TRUE, estimate_name = "OR", estimate_suffix = " (multivariable)")
86
#'
87
#' # Pipe together
88
#' apistrat %>%
89
#'   summary_factorlist(dependent, explanatory, fit_id = TRUE) %>%
90
#'   ff_merge(fit_uni) %>% 
91
#'   ff_merge(fit_multi) %>% 
92
#'   select(-fit_id, -index) %>%
93
#'   dependent_label(apistrat, dependent)
94
svyglmuni <- function(design, dependent, explanatory, ...){
95 1
	result <- list()
96 1
	for (i in 1:length(explanatory)){
97 1
		result[[i]] <- ff_eval(
98 1
			survey::svyglm(ff_formula(dependent, explanatory[i]), design = design, ...)
99
		)
100
	}
101 1
	class(result) = c("svyglmlist", "glmlist")
102 1
	return(result)
103
}
104

105

106
#' Multivariable survey-weighted generalised linear models
107
#'
108
#' Wrapper for \code{\link[survey]{svyglm}}. Fit a generalised linear model to
109
#' data from a complex survey design, with inverse-probability weighting and
110
#' design-based standard errors.
111
#'
112
#' @param design Survey design.
113
#' @param dependent Character vector of length 1: name of depdendent variable
114
#'   (must have 2 levels).
115
#' @param explanatory Character vector of any length: name(s) of explanatory
116
#'   variables.
117
#' @param ... Other arguments to be passed to \code{\link[survey]{svyglm}}.
118
#'
119
#' @return A list of univariable fitted model outputs. Output is of class
120
#'   \code{svyglmlist}.
121
#' @export
122
#'
123
#' @seealso \code{\link{fit2df}, \link{finalfit_merge}}
124
#' @family finalfit model wrappers
125
#' 
126
#' @examples
127
#' # Examples taken from survey::svyglm() help page. 
128
#' 
129
#' library(survey)
130
#' library(dplyr)
131
#' 
132
#' data(api)
133
#' dependent = "api00"
134
#' explanatory = c("ell", "meals", "mobility")
135
#' 
136
#' library(survey)
137
#' library(dplyr)
138
#'
139
#' data(api)
140
#' 
141
# Label data frame
142
#' apistrat = apistrat %>% 
143
#'   mutate(
144
#'   api00 = ff_label(api00, "API in 2000 (api00)"),
145
#'   ell = ff_label(ell, "English language learners (percent)(ell)"),
146
#'   meals = ff_label(meals, "Meals eligible (percent)(meals)"),
147
#'   mobility = ff_label(mobility, "First year at the school (percent)(mobility)"),
148
#'   sch.wide = ff_label(sch.wide, "School-wide target met (sch.wide)")
149
#'   )
150
#'
151
#' # Linear example
152
#' dependent = "api00"
153
#' explanatory = c("ell", "meals", "mobility")
154
#' 
155
#' # Stratified design
156
#' dstrat = svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
157
#' 
158
#' # Univariable fit
159
#' fit_uni = dstrat %>%
160
#'   svyglmuni(dependent, explanatory) %>%
161
#'   fit2df(estimate_suffix = " (univariable)")
162
#'
163
#' # Multivariable fit
164
#' fit_multi = dstrat %>%
165
#'   svyglmmulti(dependent, explanatory) %>%
166
#'   fit2df(estimate_suffix = " (multivariable)")
167
#'
168
#' # Pipe together
169
#' apistrat %>%
170
#'   summary_factorlist(dependent, explanatory, fit_id = TRUE) %>%
171
#'   ff_merge(fit_uni) %>% 
172
#'   ff_merge(fit_multi) %>% 
173
#'   select(-fit_id, -index) %>%
174
#'   dependent_label(apistrat, dependent)
175
#'
176
#' # Binomial example
177
#' ## Note model family needs specified and exponentiation if desired
178
#' 
179
#' dependent = "sch.wide"
180
#' explanatory = c("ell", "meals", "mobility")
181
#' 
182
#' # Univariable fit
183
#' fit_uni = dstrat %>%
184
#'   svyglmuni(dependent, explanatory, family = "quasibinomial") %>%
185
#'   fit2df(exp = TRUE, estimate_name = "OR", estimate_suffix = " (univariable)")
186
#'
187
#' # Multivariable fit
188
#' fit_multi = dstrat %>%
189
#'   svyglmmulti(dependent, explanatory, family = "quasibinomial") %>%
190
#'   fit2df(exp = TRUE, estimate_name = "OR", estimate_suffix = " (multivariable)")
191
#'
192
#' # Pipe together
193
#' apistrat %>%
194
#'   summary_factorlist(dependent, explanatory, fit_id = TRUE) %>%
195
#'   ff_merge(fit_uni) %>% 
196
#'   ff_merge(fit_multi) %>% 
197
#'   select(-fit_id, -index) %>%
198
#'   dependent_label(apistrat, dependent)
199
svyglmmulti <- function(design, dependent, explanatory, ...){
200 1
	result = list()
201 1
	for (i in 1:length(dependent)){
202 1
		result[[i]] = ff_eval(
203 1
			survey::svyglm(ff_formula(dependent[i], explanatory),  design = design, ...)
204
		)
205
	}
206 1
	result = setNames(result, dependent)
207 1
	class(result) = c("svyglmlist", "glmlist")
208 1
	return(result)
209
}

Read our documentation on viewing source code .

Loading