| Title: | Predictive Margins for Survey Analyses |
|---|---|
| Description: | Predictive margins like in the 'Stata' procedure 'margins'. This package is based on the survey::svypredmean() function. Supported regression analyses are based on survey::svyglm() and svyVGAM::svy_vglm() functions (for multinomial logistic regression models). |
| Authors: | Tommi Härkänen [aut, cre] |
| Maintainer: | Tommi Härkänen <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-07-06 19:04:30 UTC |
| Source: | https://github.com/cran/svymargins |
Helper function to convert symmetric probability scale confidence intervals into asymmetric
asymm_ci(x, ci.level = 0.95)asymm_ci(x, ci.level = 0.95)
x |
Matrix with two columns as returned by |
ci.level |
Default 0.95 for 95% confidence intervals. The transformation is based on the logit transformation and the delta method. |
Matrix with two columns as returned by confint function.
library(survey) library(dplyr) library(svyVGAM) n <- 1000 # Generate data: set.seed(1234) d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE))) d <- d |> mutate(age=runif(n, 0, 40) + as.numeric(education) * 20, pr1=1, pr2=exp(-3 + 0.5 * as.numeric(education) + 0.05 * age), pr3=exp(1 + -0.5 * as.numeric(education) + 0.02 * age)) |> rowwise() |> mutate(y=which(rmultinom(1, 1, c(pr1, pr2, pr3))[,1] == 1)) |> ungroup() |> mutate(y=factor(y, levels=1:3, labels=LETTERS[1:3])) # Create survey design: my.svy <- svydesign(~ 1, weights=~ 1, data=d) # Run regression analysis: res <- svy_vglm(y ~ education + age + sex, family=multinomial(refLevel=1), design=my.svy) # Define margins as a named list: target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) # Calculate predictive margins: marg <- svymargins(res, groupfactor=target.l, y.lev=2) confint(marg) confint(marg) |> asymm_ci()library(survey) library(dplyr) library(svyVGAM) n <- 1000 # Generate data: set.seed(1234) d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE))) d <- d |> mutate(age=runif(n, 0, 40) + as.numeric(education) * 20, pr1=1, pr2=exp(-3 + 0.5 * as.numeric(education) + 0.05 * age), pr3=exp(1 + -0.5 * as.numeric(education) + 0.02 * age)) |> rowwise() |> mutate(y=which(rmultinom(1, 1, c(pr1, pr2, pr3))[,1] == 1)) |> ungroup() |> mutate(y=factor(y, levels=1:3, labels=LETTERS[1:3])) # Create survey design: my.svy <- svydesign(~ 1, weights=~ 1, data=d) # Run regression analysis: res <- svy_vglm(y ~ education + age + sex, family=multinomial(refLevel=1), design=my.svy) # Define margins as a named list: target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) # Calculate predictive margins: marg <- svymargins(res, groupfactor=target.l, y.lev=2) confint(marg) confint(marg) |> asymm_ci()
Specify constraints for selected covariates for predicted margins
scenarios2group(scenarios, dat)scenarios2group(scenarios, dat)
scenarios |
A (named) list of scenarios based on factor and/or continuous covariates. |
dat |
A dataframe containing the covariates. Details All combinations of the specified covariates are returned as a dataframe.
If the list contains more than one element, then the dataframes based on the elements
are returned, and binded together.
if |
A dataframe containing the combinations of the specified covariates.
n <- 1000 d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), age=runif(20, 100, n), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE)), y=rnorm(n)) target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) scenarios2group(target.l, d)n <- 1000 d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), age=runif(20, 100, n), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE)), y=rnorm(n)) target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) scenarios2group(target.l, d)
svymargins
Generic function for svymargins
svymargins(adjustmodel, ...)svymargins(adjustmodel, ...)
adjustmodel |
A regression analysis result object. |
... |
Other arguments See other methods for different regression analyses. |
As svypredmeans, an object of class svystat with the predictive marginal means and their covariance matrix. Additional attribute: groups, which contains dataframe with the groupfactor variable values, and the estimated margins and standard errors.
svymargins
The default method for svymargins
## Default S3 method: svymargins(adjustmodel, ...)## Default S3 method: svymargins(adjustmodel, ...)
adjustmodel |
A regression analysis result object. |
... |
Other arguments Executed for regression analyses, which have not been implemented. |
An error message.
svymargins.svyglm, svymargins.svy_vglm
Predictive margins for multinomial logistic regression model
## S3 method for class 'svy_vglm' svymargins(adjustmodel, ..., groupfactor, y.lev = NULL, subs = NULL)## S3 method for class 'svy_vglm' svymargins(adjustmodel, ..., groupfactor, y.lev = NULL, subs = NULL)
adjustmodel |
Result of regression analysis object from the survey::svyglm function. |
... |
Currently unused. |
groupfactor |
Specification of the margins: Character vector of factor variable names or a list. See details and examples. |
y.lev |
Output category (name or number of level) for which margins are calculated. |
subs |
Character string specifying a subset, e.g. Details
|
As svypredmeans, an object of class svystat with the predictive marginal means and their covariance matrix. Additional attribute: groups, which contains dataframe with the groupfactor variable values, and the estimated margins and standard errors. The result of svymargins is named based on the groupfactor, but if there are duplicates, then the result is unnamed.
library(survey) library(dplyr) library(svyVGAM) n <- 1000 # Generate data: set.seed(1234) d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE))) d <- d |> mutate(age=runif(n, 0, 40) + as.numeric(education) * 20, pr1=1, pr2=exp(-3 + 0.5 * as.numeric(education) + 0.05 * age), pr3=exp(1 + -0.5 * as.numeric(education) + 0.02 * age)) |> rowwise() |> mutate(y=which(rmultinom(1, 1, c(pr1, pr2, pr3))[,1] == 1)) |> ungroup() |> mutate(y=factor(y, levels=1:3, labels=LETTERS[1:3])) # Create survey design: my.svy <- svydesign(~ 1, weights=~ 1, data=d) # Run regression analysis: res <- svy_vglm(y ~ education + age + sex, family=multinomial(refLevel=1), design=my.svy) # Define margins as a named list: target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) # Calculate predictive margins: svymargins(res, groupfactor=target.l, y.lev=2) # Get the output table containing the covariate information from "groups" attribute: attr(svymargins(res, groupfactor=target.l, y.lev="B"), "groups")library(survey) library(dplyr) library(svyVGAM) n <- 1000 # Generate data: set.seed(1234) d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE))) d <- d |> mutate(age=runif(n, 0, 40) + as.numeric(education) * 20, pr1=1, pr2=exp(-3 + 0.5 * as.numeric(education) + 0.05 * age), pr3=exp(1 + -0.5 * as.numeric(education) + 0.02 * age)) |> rowwise() |> mutate(y=which(rmultinom(1, 1, c(pr1, pr2, pr3))[,1] == 1)) |> ungroup() |> mutate(y=factor(y, levels=1:3, labels=LETTERS[1:3])) # Create survey design: my.svy <- svydesign(~ 1, weights=~ 1, data=d) # Run regression analysis: res <- svy_vglm(y ~ education + age + sex, family=multinomial(refLevel=1), design=my.svy) # Define margins as a named list: target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) # Calculate predictive margins: svymargins(res, groupfactor=target.l, y.lev=2) # Get the output table containing the covariate information from "groups" attribute: attr(svymargins(res, groupfactor=target.l, y.lev="B"), "groups")
Based on the survey::svypredmeans function, but is more flexible.
No restrictions with interactions. Implements also subsets with the subs argument.
## S3 method for class 'svyglm' svymargins(adjustmodel, ..., groupfactor, y.lev = NULL, subs = NULL)## S3 method for class 'svyglm' svymargins(adjustmodel, ..., groupfactor, y.lev = NULL, subs = NULL)
adjustmodel |
Result of regression analysis object from the survey::svyglm function. |
... |
Currently unused. |
groupfactor |
Specification of the margins: Character vector of factor variable names or a list. See details and examples. |
y.lev |
Not currently used in |
subs |
Character string specifying a subset, e.g. Details
|
As svypredmeans, an object of class svystat with the predictive marginal means and their covariance matrix. Additional attribute: groups, which contains dataframe with the groupfactor variable values, and the estimated margins and standard errors. The result of svymargins is named based on the groupfactor, but if there are duplicates, then the result is unnamed.
library(survey) library(dplyr) n <- 1000 # Generate data: set.seed(1234) d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE))) d <- d |> mutate(age=runif(n, 0, 40) + as.numeric(education) * 20, y=rnorm(n, sd=5) + as.numeric(education) + 0.05 * age) # Create survey design: my.svy <- svydesign(~ 1, weights=~ 1, data=d) # Run regression analysis: res <- svyglm(y ~ education + age + sex, design=my.svy) # Define margins as a named list: target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) # Calculate predictive margins: svymargins(res, groupfactor=target.l) # Get the output table containing the covariate information from "groups" attribute: attr(svymargins(res, groupfactor=target.l), "groups")library(survey) library(dplyr) n <- 1000 # Generate data: set.seed(1234) d <- data.frame(sex=factor(sample(c("M", "F"), n, replace=TRUE)), education=factor(sample(c("low", "middle", "high"), n, replace=TRUE))) d <- d |> mutate(age=runif(n, 0, 40) + as.numeric(education) * 20, y=rnorm(n, sd=5) + as.numeric(education) + 0.05 * age) # Create survey design: my.svy <- svydesign(~ 1, weights=~ 1, data=d) # Run regression analysis: res <- svyglm(y ~ education + age + sex, design=my.svy) # Define margins as a named list: target.l <- list(null=list(), educ=list("education"), age=list(age=seq(40,70,10)), educ_age=list("education", age=seq(40,70,10))) # Calculate predictive margins: svymargins(res, groupfactor=target.l) # Get the output table containing the covariate information from "groups" attribute: attr(svymargins(res, groupfactor=target.l), "groups")