Title: | Average and Conditional Effects |
---|---|
Description: | Use structural equation modeling to estimate average and conditional effects of a treatment variable on an outcome variable, taking into account multiple continuous and categorical covariates. |
Authors: | Axel Mayer [aut, cre], Lisa Dietzfelbinger [ctb], Yves Rosseel [ctb] |
Maintainer: | Axel Mayer <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.5-1 |
Built: | 2025-02-16 03:46:30 UTC |
Source: | https://github.com/amayer2010/effectliter |
Use structural equation modeling to estimate average and conditional effects of a treatment variable on an outcome variable, taking into account multiple continuous and categorical covariates.
Maintainer: Axel Mayer [email protected]
Other contributors:
Lisa Dietzfelbinger [email protected] [contributor]
Yves Rosseel [email protected] [contributor]
Useful links:
Automatically selects a subset of the original dataset for computing specific aggregated effects. The subset is selected such that it is as close as possible to the user supplied newdata frame. The function uses exact matching for categorical covariates (and the treatment if specified) and matching based on the Mahalanobis distance for continuous covariates.
autoSelectSubset(obj, newdata, nsub = 10)
autoSelectSubset(obj, newdata, nsub = 10)
obj |
Object of class |
newdata |
A data.frame with a single row, containing the same continuous and categorical covariates (and potentially the treatment variable) as used when fitting the EffectLiteR model in obj. |
nsub |
Integer. How many data points should be used for matching the continous covariates. Will be ignored if no values for continuous covariates are specified. |
Vector of integers indicating the rows to use for computing the aggregated
effects. Can directly be used in computeAggregatedEffects
m1 <- effectLite(y="dv", z=c("z1"), k=c("k1"), x="x", control="control", data=example01, fixed.cell=TRUE, fixed.z=TRUE) newdata <- data.frame(k1=NA, z1=1) agg.subset <- autoSelectSubset(m1, newdata)
m1 <- effectLite(y="dv", z=c("z1"), k=c("k1"), x="x", control="control", data=example01, fixed.cell=TRUE, fixed.z=TRUE) newdata <- data.frame(k1=NA, z1=1) agg.subset <- autoSelectSubset(m1, newdata)
Computes aggregates of conditional effects for a subset of the original dataset based on a fitted EffectLiteR model.
computeAggregatedEffects(obj, agg.subset)
computeAggregatedEffects(obj, agg.subset)
obj |
Object of class |
agg.subset |
Vector of integers indicating the row numbers of the original dataset for the subset used to compute the aggregated effect |
Object of class "data.frame"
.
m1 <- effectLite(y="dv", z=c("z1"), k=c("k1"), x="x", control="control", data=example01, fixed.cell=TRUE, fixed.z=TRUE) newdata <- data.frame(k1=NA, z1=1) agg.subset <- autoSelectSubset(m1, newdata) computeAggregatedEffects(m1, agg.subset)
m1 <- effectLite(y="dv", z=c("z1"), k=c("k1"), x="x", control="control", data=example01, fixed.cell=TRUE, fixed.z=TRUE) newdata <- data.frame(k1=NA, z1=1) agg.subset <- autoSelectSubset(m1, newdata) computeAggregatedEffects(m1, agg.subset)
Can be used to make a conditional effects plot with an effect function on the
y axis and a covariate on the x axis. ggplot2
is used to create the plot.
conditionalEffectsPlot( obj, zsel = "id", gxsel = "g1", colour = "", show.ci = FALSE, regression = "default", regression.ci = FALSE )
conditionalEffectsPlot( obj, zsel = "id", gxsel = "g1", colour = "", show.ci = FALSE, regression = "default", regression.ci = FALSE )
obj |
Object of class |
zsel |
Name of a covariate (character string) plotted on the x-axis. If "id" (the default) the subject index is shown on the x-axis, where subjects in the data are enumerated as 1:nrow(data). |
gxsel |
Name of an effect function (character string) plotted on the y-axis. |
colour |
Name of a covariate (character string) used as colour variable in the plot. |
show.ci |
Logical. Should 95 percent confidence intervals around conditional effects be shown in the plot. |
regression |
Specifies if a regression line should be drawn. Can be one of c("default","smooth","linear","none") |
regression.ci |
Logical. Will be passed on to |
Object of class c("gg", "ggplot")
.
m1 <- effectLite(y="dv", x="x", k="k1", z="z1", control="control", data=example01) conditionalEffectsPlot(m1, zsel="z1", gxsel="g1", colour="k1")
m1 <- effectLite(y="dv", x="x", k="k1", z="z1", control="control", data=example01) conditionalEffectsPlot(m1, zsel="z1", gxsel="g1", colour="k1")
This function is the main function of the package and can be used to estimate average and conditional effects of a treatment variable on an outcome variable, taking into account any number of continuous and categorical covariates. It automatically generates lavaan syntax for a multi-group structural equation model, runs the model using lavaan, and extracts various average and conditional effects of interest.
effectLite( y, x, k = NULL, z = NULL, data, method = "sem", control = "default", measurement = character(), fixed.cell = "default", fixed.z = "default", missing = "default", se = "default", syntax.only = FALSE, interactions = "all", homoscedasticity = "default", test.stat = "default", propscore = NULL, ids = ~0, weights = NULL, add = character(), ... )
effectLite( y, x, k = NULL, z = NULL, data, method = "sem", control = "default", measurement = character(), fixed.cell = "default", fixed.z = "default", missing = "default", se = "default", syntax.only = FALSE, interactions = "all", homoscedasticity = "default", test.stat = "default", propscore = NULL, ids = ~0, weights = NULL, add = character(), ... )
y |
Dependent variable (character string). Can be the name of a manifest variable or of a latent variable. |
x |
Treatment variable (character string) treated as categorical variable. |
k |
Vector of manifest variables treated as categorical covariates (character vector). |
z |
Vector of continuous covariates (character vector). Names of both manifest and latent variables are allowed. |
data |
A data frame. |
method |
Can be one of |
control |
Value of |
measurement |
Measurement model. The measurement model is lavaan syntax (character string), that will be appended before the automatically generated lavaan input. It can be used to specify a measurement for a latent outcome variable and/or latent covariates. See also the example and |
fixed.cell |
logical. If |
fixed.z |
logical. If |
missing |
Missing data handling. Will be passed on to |
se |
Type of standard errors. Will be
passed on to |
syntax.only |
logical. If |
interactions |
character. Indicates the type of interaction. Can be one of |
homoscedasticity |
logical. If |
test.stat |
character. Can be one of |
propscore |
Vector of covariates (character vector) that will be used to compute (multiple) propensity scores based on a multinomial regression without interactions. Alternatively, the user can specify a formula with the treatment variable as dependent variable for more control over the propensity score model. |
ids |
Formula specifying cluster ID variable. Because |
weights |
Formula to specify sampling weights. Because |
add |
Character string that will be pasted at the end of the generated lavaan syntax. Can for example be used to add additional (in-) equality constraints or to compute user-defined conditional effects. |
... |
Further arguments passed to |
Object of class effectlite.
Mayer, A., Dietzfelbinger, L., Rosseel, Y. & Steyer, R. (2016). The EffectLiteR approach for analyzing average and conditional effects. Multivariate Behavioral Research, 51, 374-391.
## Example with one categorical covariate m1 <- effectLite(y="y", x="x", k="z", control="0", data=nonortho) print(m1) ## Example with one categorical and one continuous covariate m1 <- effectLite(y="dv", x="x", k=c("k1"), z=c("z1"), control="control", data=example01) print(m1) ## Example with latent outcome and latent covariate measurement <- ' eta2 =~ 1*CPM12 + 1*CPM22 eta1 =~ 1*CPM11 + 1*CPM21 CPM11 + CPM12 ~ 0*1 CPM21 ~ c(m,m)*1 CPM22 ~ c(p,p)*1' m1 <- effectLite(y="eta2", x="x", z=c("eta1"), control="0", measurement=measurement, data=example02lv) print(m1) ## Example with cluster variable and sampling weights m1 <- effectLite(y="y", x="x", z="z", fixed.cell=TRUE, control="0", syntax.only=FALSE, data=example_multilevel, cluster="cid", sampling.weights="weights") print(m1)
## Example with one categorical covariate m1 <- effectLite(y="y", x="x", k="z", control="0", data=nonortho) print(m1) ## Example with one categorical and one continuous covariate m1 <- effectLite(y="dv", x="x", k=c("k1"), z=c("z1"), control="control", data=example01) print(m1) ## Example with latent outcome and latent covariate measurement <- ' eta2 =~ 1*CPM12 + 1*CPM22 eta1 =~ 1*CPM11 + 1*CPM21 CPM11 + CPM12 ~ 0*1 CPM21 ~ c(m,m)*1 CPM22 ~ c(p,p)*1' m1 <- effectLite(y="eta2", x="x", z=c("eta1"), control="0", measurement=measurement, data=example02lv) print(m1) ## Example with cluster variable and sampling weights m1 <- effectLite(y="y", x="x", z="z", fixed.cell=TRUE, control="0", syntax.only=FALSE, data=example_multilevel, cluster="cid", sampling.weights="weights") print(m1)
Informative hypothesis tests for effectLite
effectLite_iht(object, constraints = NULL, test = "default")
effectLite_iht(object, constraints = NULL, test = "default")
object |
effectlite. Fitted model of class effectlite estimated with |
constraints |
character. Specification of constraints for the ordered hypothesis test. |
test |
character. Statistical test to be used for the ordered hypothesis test. Can be one of |
list with test statistics and p-value.
m1 <- effectLite(y="dv", x="x", k="k1", z="z1", method = "sem", fixed.cell=TRUE, fixed.z=TRUE, data=example01) test <- effectLite_iht(object = m1, constraints = 'adjmean2 > adjmean1 adjmean1 > adjmean0') print(test)
m1 <- effectLite(y="dv", x="x", k="k1", z="z1", method = "sem", fixed.cell=TRUE, fixed.z=TRUE, data=example01) test <- effectLite_iht(object = m1, constraints = 'adjmean2 > adjmean1 adjmean1 > adjmean0') print(test)
This function calls a shiny interface for effectLite.
effectLiteGUI(launch.browser = TRUE)
effectLiteGUI(launch.browser = TRUE)
launch.browser |
Option will be passed on to |
A simulated dataset for testing measurement models with categorical items:
A data frame with 10000 rows and 13 variables.
x. Treatment variable with values 0, 1.
z11. indicator for covariate.
z21. indicator for covariate.
z31. indicator for covariate.
z41. indicator for covariate.
z51. indicator for covariate.
y11. indicator for outcome.
y21. indicator for outcome.
y31. indicator for outcome.
y41. indicator for outcome.
y51. indicator for outcome.
y61. indicator for outcome.
y71. indicator for outcome.
A simulated dataset for logistic regression from Kiefer, Lugauer, and Mayer (2024):
A data frame with 600 rows and 3 variables.
Y. Outcome variable with values 0, 1.
X. Treatment variable with values 0, 1.
Z. continuous covariate.
A simulated dataset for testing logistic regression:
A data frame with 10000 rows and 6 variables.
y. Outcome variable with values 0, 1.
x. Treatment variable with values 0, 1.
z1. continuous covariate.
z2. continuous covariate.
k1. categorical covariate.
k2. categorical covariate.
This function can be used to estimate average and conditional effects of a treatment variable on an outcome variable, taking into account any number of continuous and categorical covariates. It takes a user defined generalized linear model (or another statistical model with a suitable predict method) as input and computes the corresponding effects.
elrEffects(object, x, from = 0, to = 1, type = "difference", subset. = NULL)
elrEffects(object, x, from = 0, to = 1, type = "difference", subset. = NULL)
object |
User defined generalized linear model (or another statistical model with a suitable predict method) |
x |
Treatment variable (character string) |
from |
from and to (values of treatment variable) specify the considered change in the treatment variable for the effect computation |
to |
from and to (values of treatment variable) specify the considered change in the treatment variable for the effect computation |
type |
character. Indicates the type of effect considered. Can be one of |
subset. |
Logical vector for computing effects in a subset of the data (conditional effects). |
Object of class elreffects
## Example with a logistic regression m1logreg <- glm(y ~ x+z1+z2+k1+k2, data=elrdata_logreg, family=binomial) elrEffects(m1logreg, "x", from="0", to="1", type="difference", subset.=NULL)
## Example with a logistic regression m1logreg <- glm(y ~ x+z1+z2+k1+k2, data=elrdata_logreg, family=binomial) elrEffects(m1logreg, "x", from="0", to="1", type="difference", subset.=NULL)
This function calls a shiny interface for elrEffects.
elrEffectsGUI(launch.browser = TRUE)
elrEffectsGUI(launch.browser = TRUE)
launch.browser |
Option will be passed on to |
Predicts conditional treatment effects based on a fitted EffectLiteR model.
elrPredict(obj, newdata = NULL, add.columns = "expected-outcomes")
elrPredict(obj, newdata = NULL, add.columns = "expected-outcomes")
obj |
Object of class |
newdata |
An optional data.frame, containing the same continuous and categorical covariates as used when fitting the EffectLiteR model in obj. Only covariates (and neither the dependent variable nor indicators for latent variables) should be included. |
add.columns |
Used to request additional columns. Can be one or several of c("covariates", "modmat", "expected-outcomes", "prop-covariates"). |
Object of class "data.frame"
.
m1 <- effectLite(y="dv", z=c("z1"), k=c("k1","kateg2"), x="x", control="control", data=example01) newdata <- data.frame(k1="male", kateg2="1", z1=2) elrPredict(m1, newdata)
m1 <- effectLite(y="dv", z=c("z1"), k=c("k1","kateg2"), x="x", control="control", data=example01) newdata <- data.frame(k1="male", kateg2="1", z1=2) elrPredict(m1, newdata)
Tries to determine the format of the data by the file ending and
chooses the appropriate function to read data. Currently supports .csv,
.dat, .txt, .sav, and .xpt and calls read.csv
, read.csv2
,
read.table
, read.spss
,
read.xport
accordingly. The default values for
arguments depend on the function used to read data.
elrReadData( file, name = NULL, header = "default", sep = "default", dec = "default", use.value.labels = "default", na.strings = "NA" )
elrReadData( file, name = NULL, header = "default", sep = "default", dec = "default", use.value.labels = "default", na.strings = "NA" )
file |
Name of the file to read. |
name |
Pure file name (without path to file) to read.
If |
header |
See |
sep |
See |
dec |
See |
use.value.labels |
See |
na.strings |
See |
Object of class "data.frame"
.
A simulated dataset with a cluster ID and sampling weights to test multilevel options. The variables are:
A data frame with 800 rows and 7 variables.
y. Coninuous dependent variable.
x. Treatment variable with values 0, 1.
z. Continuous covariate.
xz. Product of x and z.
cid. Cluster ID.
weights. Sampling weights.
iptw. Classic inverse probability of treatment weights based on a logistic regression of x on z. Use with care (only for average effects).
A simulated dataset. The variables are:
A data frame with 2000 rows and 7 variables.
x. Treatment variable with values control, treat1, and treat2.
k1. Categorical covariate with values male and female.
kateg2. Categorical covariate with values 1 and 2.
z1-z3. Continuous covariates.
dv. Coninuous dependent variable.
A simulated dataset with latent variables. The variables are:
A data frame with 300 rows and 6 variables.
CPM11. First indicator of latent covariate.
CPM21. Second indicator of latent covariate.
CPM12. First indicator of latent outcome.
CPM22. Second indicator of latent outcome.
x. Dichotomous treatment variable with values 0 (control), and 1 (treatment).
k. Categorical covariate with values first, second, and third.
This function automatically generates lavaan
syntax for the measurement model for a call to effectLite
. It is currently also used in the shiny interface.
generateMeasurementModel( names = NULL, indicators, ncells, model = NULL, data = NULL )
generateMeasurementModel( names = NULL, indicators, ncells, model = NULL, data = NULL )
names |
A vector of character strings with names of latent variables. If not specified, names(indicators) is used. |
indicators |
A list of vectors of character strings to specify indicators of latent variables (see example). |
ncells |
Number of groups/cells. |
model |
A vector of character strings of the same length as names. It is used to specify the type of measurement model for each of the latent variables. Each element can be one of |
data |
A data set that includes the indicator variables. It is required only for categorical indicators to detect the number of categories. |
## Example with three latent variables names <- c("eta", "xi1", "xi2") indicators <- list("eta" = c("y1","y2","y3"), "xi1" = c("z1","z2"), "xi2" = c("z12","z22","z32","z42")) ncells = 6 model = c("parallel","tau-equi","tau-cong") cat(generateMeasurementModel(names, indicators, ncells, model)) ## Example with method factor names <- c("eta", "xi", "mf") indicators <- list("eta" = c("y12","y22"), "xi" = c("y11","y21"), "mf" = c("y12","y22")) ncells = 2 cat(generateMeasurementModel(names, indicators, ncells)) ## Example with categorical items names <- c("eta", "xi") indicators <- list("eta" = paste0("y",1:7,1), "xi" = paste0("z",1:5,1)) ncells = 2 model = c("tau-equi-categorical","tau-cong-categorical") cat(generateMeasurementModel(names, indicators, ncells, model, data=elrdata_categorical_items))
## Example with three latent variables names <- c("eta", "xi1", "xi2") indicators <- list("eta" = c("y1","y2","y3"), "xi1" = c("z1","z2"), "xi2" = c("z12","z22","z32","z42")) ncells = 6 model = c("parallel","tau-equi","tau-cong") cat(generateMeasurementModel(names, indicators, ncells, model)) ## Example with method factor names <- c("eta", "xi", "mf") indicators <- list("eta" = c("y12","y22"), "xi" = c("y11","y21"), "mf" = c("y12","y22")) ncells = 2 cat(generateMeasurementModel(names, indicators, ncells)) ## Example with categorical items names <- c("eta", "xi") indicators <- list("eta" = paste0("y",1:7,1), "xi" = paste0("z",1:5,1)) ncells = 2 model = c("tau-equi-categorical","tau-cong-categorical") cat(generateMeasurementModel(names, indicators, ncells, model, data=elrdata_categorical_items))
The simulated dataset with latent variables used in Mayer, Dietzfelbinger, Rosseel, and Steyer (2016). The variables are:
A data frame with 1000 rows and 10 variables.
y11. First indicator of latent covariate (pretest mental health).
y21. Second indicator of latent covariate (pretest mental health).
y31. Third indicator of latent covariate (pretest mental health).
y12. First indicator of latent outcome (posttest mental health).
y22. Second indicator of latent outcome (posttest mental health).
y32. Third indicator of latent outcome (posttest mental health).
x. Categorical treatment variable with values 0 (wait list control group), 1 (conventional therapy), and 2 (innovative therapy).
k. Categorical covariate with values 0 (male) and 1 (female).
Ix1. Binary indicator for conventional therapy (X=1).
Ix2. Binary indicator for innovative therapy (X=2).
A simulated dataset. The variables are:
A data frame with 500 rows and 3 variables
y. Continuous dependent variable depression.
x. Treatment variable with values 0 (control), 1 (treat1), and 2 (treat2).
z. Categorical covariate with values 0 (low neediness), 1 (medium neediness) and 2 (high neediness).
A simulated dataset based on the SOPHONET-study (Leichsenring et al., 2013). The variables are:
A data frame with 328 rows and 24 variables.
lsas.a.t2
lsas.v.t2
lsas.a.t1
lsas.v.t1
bdi.t1.i1
bdi.t1.i2
bdi.t1.i3
ecr.anx.t1.i1
ecr.anx.t1.i2
ecr.anx.t1.i3
ecr.avoi.t1.i1
ecr.avoi.t1.i2
ecr.avoi.t1.i3
tpq.ha.i1
tpq.ha.i2
tpq.ha.i3
tosca.shame.t1.i1
tosca.shame.t1.i2
fskn.se.t1.i1
fskn.se.t1.i2
comorbid
iip.lov
iip.dom tb
Leichsenring, F., Salzer, S., Beutel, M. E., Herpertz, S., Hiller, W., Hoyer, J., Huesing, J., ..., Leibing, E. (2013). Psychodynamic therapy and cognitive-behavioral therapy in social anxiety disorder: A multicenter randomized controlled trial. American Journal of Psychiatry, 170, 759–767.