Title: | Predicted Values and Discrete Changes for Regression Models |
---|---|
Description: | Functions to calculate predicted values and the difference between the two cases with confidence interval for lm() [linear model], glm() [generalized linear model], glm.nb() [negative binomial model], polr() [ordinal logistic model], vglm() [generalized ordinal logistic model], multinom() [multinomial model], tobit() [tobit model], svyglm() [survey-weighted generalised linear models] and lmer() [linear multilevel models] using Monte Carlo simulations or bootstrap. Reference: Bennet A. Zelner (2009) <doi:10.1002/smj.783>. |
Authors: | Benjamin E. Schlegel [aut,cre] |
Maintainer: | Benjamin E. Schlegel <[email protected]> |
License: | GPL (>=2) |
Version: | 4.3-1.9000 |
Built: | 2025-02-19 04:46:20 UTC |
Source: | https://github.com/benjaminschlegel/glm.predict |
This package provides functions to calculate predicted values and the difference between two cases with confidence interval.
Benjamin Schlegel
Maintainer: Benjamin Schlegel <[email protected]>
The generic function calculates the predicted value with the confidence interval. It can be used for any lm(), glm(), glm.nb(), polr(), tobit() or multinom() model.
basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with glm(), glm.nb(), polr() or multinom() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # predicted probability of a non smoking person with height 150 and average pulse basepredict(model1, c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)))
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # predicted probability of a non smoking person with height 150 and average pulse basepredict(model1, c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)))
The function calculates the predicted value with the confidence interval. It can be used for any glm model.
## S3 method for class 'glm' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'glm' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with glm() or glm.nb() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a 3x3 matrix having in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # predicted probability of a non smoking person with height 150 and average pulse basepredict(model1, c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)))
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # predicted probability of a non smoking person with height 150 and average pulse basepredict(model1, c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)))
The function calculates the predicted value with the confidence interval for a lm model.
## S3 method for class 'lm' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'lm' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with lm() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a 3x3 matrix having in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = lm(Pulse ~ Height + Smoke, data=MASS::survey) summary(model1) # predicted pulse value of a non smoking person with height 150 basepredict(model1, c(1,150,1,0,0))
model1 = lm(Pulse ~ Height + Smoke, data=MASS::survey) summary(model1) # predicted pulse value of a non smoking person with height 150 basepredict(model1, c(1,150,1,0,0))
The function calculates the (average) predicted value with the confidence interval for a lmer model.
## S3 method for class 'lmerMod' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'lmerMod' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with lmer() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a 3x3 matrix having in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = lme4::lmer(lr_self ~ age + gender + (1 | canton), data=selects2015) summary(model1) # predicted left-right position of a 18 year old woman. basepredict(model1, c(1,18,1))
model1 = lme4::lmer(lr_self ~ age + gender + (1 | canton), data=selects2015) summary(model1) # predicted left-right position of a 18 year old woman. basepredict(model1, c(1,18,1))
The function calculates the predicted value with the confidence interval. It can be used for any mlogit model.
## S3 method for class 'mlogit' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'mlogit' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with mlogit() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
type is ignored as only simulation is implemented |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: df_selects_withoutNA = selects2015 |> dplyr::filter(vote_choice != "other") |> dplyr::mutate(vote_choice = factor(vote_choice)) |> dplyr::select(age, gender, vote_choice, starts_with("lr_")) |> na.omit() mlogit_data = dfidx::dfidx(df_selects_withoutNA, varying = 5:11, sep = "_", shape = "wide", choice = "vote_choice") mlogit_data$distance = abs(mlogit_data$lr - mlogit_data$lr_self) model1 = mlogit::mlogit(vote_choice ~ distance | lr_self + gender, data = mlogit_data) summary(model1) # predicted probability of a left male person with a distance of 2 basepredict(model1, list(1, c(2, 5, 5, 5, 5, 5), 0, 0)) ## End(Not run)
## Not run: df_selects_withoutNA = selects2015 |> dplyr::filter(vote_choice != "other") |> dplyr::mutate(vote_choice = factor(vote_choice)) |> dplyr::select(age, gender, vote_choice, starts_with("lr_")) |> na.omit() mlogit_data = dfidx::dfidx(df_selects_withoutNA, varying = 5:11, sep = "_", shape = "wide", choice = "vote_choice") mlogit_data$distance = abs(mlogit_data$lr - mlogit_data$lr_self) model1 = mlogit::mlogit(vote_choice ~ distance | lr_self + gender, data = mlogit_data) summary(model1) # predicted probability of a left male person with a distance of 2 basepredict(model1, list(1, c(2, 5, 5, 5, 5, 5), 0, 0)) ## End(Not run)
The function calculates the predicted value with the confidence interval. It can be used for any multinom model.
## S3 method for class 'multinom' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'multinom' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with multinom() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: model1 = nnet::multinom(Clap ~ Height + Smoke + Pulse, data=MASS::survey) summary(model1) # predicted probability of a non smoking person with height 150 and average pulse basepredict(model1, c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) ## End(Not run)
## Not run: model1 = nnet::multinom(Clap ~ Height + Smoke + Pulse, data=MASS::survey) summary(model1) # predicted probability of a non smoking person with height 150 and average pulse basepredict(model1, c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) ## End(Not run)
The function calculates the predicted value with the confidence interval. It can be used for any polr model.
## S3 method for class 'polr' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'polr' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with polr() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = polr(Smoke ~ Height + Pulse, data=data) summary(model1) # predicted probability of smoking of a person with height 170 and an average pulse basepredict(model1, c(170,mean(MASS::survey$Pulse,na.rm=TRUE))) ## End(Not run)
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = polr(Smoke ~ Height + Pulse, data=data) summary(model1) # predicted probability of smoking of a person with height 170 and an average pulse basepredict(model1, c(170,mean(MASS::survey$Pulse,na.rm=TRUE))) ## End(Not run)
The function calculates the predicted value with the confidence interval for a tobit model.
## S3 method for class 'tobit' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'tobit' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with tobit() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL only simulation is supported for tobit() |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a 3x3 matrix having in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
library(AER) model1 = tobit(Age ~ Height + Pulse, right = 65, data=MASS::survey) summary(model1) # Person with a height of 160 and a pulse of 80 basepredict(model1, values = c(1,160,80))
library(AER) model1 = tobit(Age ~ Height + Pulse, right = 65, data=MASS::survey) summary(model1) # Person with a height of 160 and a pulse of 80 basepredict(model1, values = c(1,160,80))
The function calculates the predicted value with the confidence interval. It can be used for any vglm model.
## S3 method for class 'vglm' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'vglm' basepredict(model, values, sim.count=1000, conf.int=0.95, sigma=NULL, set.seed=NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model Object generated with vglm() |
values |
the values of the case as vector in the order how they appear in the summary(model) Estimate |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000). Note: bootstrap is very slow for vglm() models. |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted value, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = VGAM::vglm(Smoke ~ Height + Pulse, data=data, family = cumulative(parallel=FALSE ~ Pulse), maxit=1000) summary(model1) # predicted probability of smoking of a person with height 170 and an average pulse basepredict(model1, c(170,mean(MASS::survey$Pulse,na.rm=TRUE))) ## End(Not run)
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = VGAM::vglm(Smoke ~ Height + Pulse, data=data, family = cumulative(parallel=FALSE ~ Pulse), maxit=1000) summary(model1) # predicted probability of smoking of a person with height 170 and an average pulse basepredict(model1, c(170,mean(MASS::survey$Pulse,na.rm=TRUE))) ## End(Not run)
The generic function calculates the predicted values and the difference of two cases with the confidence interval. It can be used for any lm(), glm(), glm.nb(), polr(), tobit() or multinom() model.
dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with glm(), glm.nb(), polr() or multinom() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(1,151,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) # the higher person has a greater probability to be a man # the difference is significant, because the confidence interval # does not include the 0
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(1,151,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) # the higher person has a greater probability to be a man # the difference is significant, because the confidence interval # does not include the 0
The function calculates the predicted values and the difference of two cases with the confidence interval. It can be used for any glm model.
## S3 method for class 'glm' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'glm' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with glm() or glm.nb() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(1,151,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) # the higher person has a greater probability to be a man # the difference is significant, because the confidence interval # does not include the 0
model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(1,151,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) # the higher person has a greater probability to be a man # the difference is significant, because the confidence interval # does not include the 0
The function calculates the predicted values and the difference of two cases with the confidence interval for a lm model.
## S3 method for class 'lm' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'lm' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with lm() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = lm(Pulse ~ Height + Smoke, data=MASS::survey) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,150,1,0,0), values2 = c(1,151,1,0,0)) # the difference is not significant, because the confidence interval # includes the 0
model1 = lm(Pulse ~ Height + Smoke, data=MASS::survey) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,150,1,0,0), values2 = c(1,151,1,0,0)) # the difference is not significant, because the confidence interval # includes the 0
The function calculates the predicted values and the difference of two cases with the confidence interval for a lm model.
## S3 method for class 'lmerMod' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'lmerMod' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with lmer() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
model1 = lme4::lmer(lr_self ~ age + gender + (1 | canton), data=selects2015) summary(model1) # comparing a 20 year old woman with a 20 year old man considering their left-right position dc(model1, values1 = c(1,20,1), values2 = c(1,20,0))
model1 = lme4::lmer(lr_self ~ age + gender + (1 | canton), data=selects2015) summary(model1) # comparing a 20 year old woman with a 20 year old man considering their left-right position dc(model1, values1 = c(1,20,1), values2 = c(1,20,0))
The function calculates the predicted values and the difference of two cases with the confidence interval. It can be used for a mlogit model.
## S3 method for class 'mlogit' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'mlogit' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with mlogit() |
values |
the values of case 1 and 2 as list in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as list in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. Can be single values or a vector of values with the length of choices - 1 for condition variables. |
values2 |
the values of case 2 as list in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. Can be single values or a vector of values with the length of choices - 1 for condition variables. |
type |
type is ignored as only simulation is implemented |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: df_selects_withoutNA = selects2015 |> filter(vote_choice != "other") |> mutate(vote_choice = factor(vote_choice)) |> select(age, gender, vote_choice, starts_with("lr_")) |> na.omit() mlogit_data = dfidx::dfidx(df_selects_withoutNA, varying = 5:11, sep = "_", shape = "wide", choice = "vote_choice") mlogit_data$distance = abs(mlogit_data$lr - mlogit_data$lr_self) model1 = mlogit::mlogit(vote_choice ~ distance | lr_self + gender, data = mlogit_data) summary(model1) # predicted probability of a left male person with a distance of 2 dc(model1, list(1, c(2, 5, 5, 5, 5, 5), 0, 0, 1, c(2, 5, 5, 5, 5, 5), 10, 0)) ## End(Not run)
## Not run: df_selects_withoutNA = selects2015 |> filter(vote_choice != "other") |> mutate(vote_choice = factor(vote_choice)) |> select(age, gender, vote_choice, starts_with("lr_")) |> na.omit() mlogit_data = dfidx::dfidx(df_selects_withoutNA, varying = 5:11, sep = "_", shape = "wide", choice = "vote_choice") mlogit_data$distance = abs(mlogit_data$lr - mlogit_data$lr_self) model1 = mlogit::mlogit(vote_choice ~ distance | lr_self + gender, data = mlogit_data) summary(model1) # predicted probability of a left male person with a distance of 2 dc(model1, list(1, c(2, 5, 5, 5, 5, 5), 0, 0, 1, c(2, 5, 5, 5, 5, 5), 10, 0)) ## End(Not run)
The function calculates the predicted values and the difference of two cases with the confidence interval. It can be used for a multinom model.
## S3 method for class 'multinom' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'multinom' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with multinom() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: model1 = nnet::multinom(Clap ~ Height + Smoke + Pulse, data=MASS::survey) summary(model1) dc(model1, values1 = c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(1,151,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) # the higher person has a greater probability to be left clapping # the difference is significant, because the confidence interval # does not include the 0 ## End(Not run)
## Not run: model1 = nnet::multinom(Clap ~ Height + Smoke + Pulse, data=MASS::survey) summary(model1) dc(model1, values1 = c(1,150,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(1,151,1,0,0,mean(MASS::survey$Pulse,na.rm=TRUE))) # the higher person has a greater probability to be left clapping # the difference is significant, because the confidence interval # does not include the 0 ## End(Not run)
The function calculates the predicted values and the difference of two cases with the confidence interval. It can be used for a polr model.
## S3 method for class 'polr' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'polr' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with polr() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 1000) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = polr(Smoke ~ Height + Pulse, data=data) summary(model1) dc(model1, values1 = c(150,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(151,mean(MASS::survey$Pulse,na.rm=TRUE))) # all differences are significant as the confidence intervals do not include 0 ## End(Not run)
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = polr(Smoke ~ Height + Pulse, data=data) summary(model1) dc(model1, values1 = c(150,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(151,mean(MASS::survey$Pulse,na.rm=TRUE))) # all differences are significant as the confidence intervals do not include 0 ## End(Not run)
The function calculates the predicted values and the difference of two cases with the confidence interval for a tobit model.
## S3 method for class 'tobit' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'tobit' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with tobit() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL only simulation is supported for tobit() |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
library(AER) model1 = tobit(Age ~ Height + Pulse, right = 65, data=MASS::survey) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,160,80), values2 = c(1,170,80)) # the difference is not significant, because the confidence interval # includes the 0
library(AER) model1 = tobit(Age ~ Height + Pulse, right = 65, data=MASS::survey) summary(model1) # comparing a person with the height 150cm to 151cm dc(model1, values1 = c(1,160,80), values2 = c(1,170,80)) # the difference is not significant, because the confidence interval # includes the 0
The function calculates the predicted values and the difference of two cases with the confidence interval. It can be used for a vglm model.
## S3 method for class 'vglm' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
## S3 method for class 'vglm' dc(model, values = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, values1 = NULL, values2 = NULL, type = c("any", "simulation", "bootstrap"), summary = TRUE)
model |
the model-Object generated with vglm() |
values |
the values of case 1 and 2 as vector in the order how they appear in the summary(model) Estimate. Values is if values1 and values2 are specified after each other in the same vector. Either values or values1 and values2 have to be specified. |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
values1 |
the values of case 1 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
values2 |
the values of case 2 as vector in the order how they appear in the summary(model) Estimate. Has to be defined if values is not defined. |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 500) (boostrap is very slow for vglm models) |
summary |
OPTIONAL if mean/quantiles should be return or all simulated values (default: TRUE) |
The function makes a simulation for the two cases and compares them to each other.
The output is a matrix have in the first column the predicted values, in the second column the lower value of the confidence interval and in the third column the upper value of the confidence interval.
Benjamin Schlegel, [email protected]
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = VGAM::vglm(Smoke ~ Height + Pulse, data=data, family = cumulative(parallel=FALSE ~ Pulse), maxit=1000) summary(model1) dc(model1, values1 = c(150,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(151,mean(MASS::survey$Pulse,na.rm=TRUE)), type = "simulation") # all differences are significant as the confidence intervals do not include 0 ## End(Not run)
## Not run: data = MASS::survey data$Smoke = ordered(data$Smoke, levels = c("Never", "Occas", "Regul", "Heavy")) model1 = VGAM::vglm(Smoke ~ Height + Pulse, data=data, family = cumulative(parallel=FALSE ~ Pulse), maxit=1000) summary(model1) dc(model1, values1 = c(150,mean(MASS::survey$Pulse,na.rm=TRUE)), values2 = c(151,mean(MASS::survey$Pulse,na.rm=TRUE)), type = "simulation") # all differences are significant as the confidence intervals do not include 0 ## End(Not run)
The function calculates the predicted values and the difference of a range of cases with the confidence interval. It can be used for any glm, polr or multinom model.
predicts(model, values, position = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, doPar = FALSE, type = c("any", "simulation", "bootstrap"))
predicts(model, values, position = NULL, sim.count = 1000, conf.int = 0.95, sigma = NULL, set.seed = NULL, doPar = FALSE, type = c("any", "simulation", "bootstrap"))
model |
the model-Object generated with glm(), glm.nb(), polr(), multinom(), mlogit() or tobit() |
values |
The values of cases as character in the order how they appear in the summary(model) Estimate. The values must be in the following way: "value1;value2;value3;...". Each one of the values can be one of the following:
|
position |
OPTIONAL which variable should be taken for the discrete change, the variable must have at least two values. default: only predicted probabilities |
sim.count |
OPTIONAL numbers of simulations to be done by the function. default: 1000 |
conf.int |
OPTIONAL the confidence interval used by the function. default: 0.95 |
sigma |
OPTIONAL the variance-covariance matrix, can be changed when having for example robust or clustered vcov. default: vcov(model) |
set.seed |
OPTIONAL set a seed for the random number generator |
doPar |
OPTIONAL if the code should run parallel if more than 2 cores are detected |
type |
OPTIONAL choose between simulation and bootstrap, "any" chooses between those two according to the number of cases (bootstrap if n < 500) |
The function makes a simulation for the all combination of cases and compares them to each other.
The output is a data.frame with the predicted values and discrete changes.
Benjamin Schlegel, [email protected]
## Not run: model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # comparing person with hight 150 to 160, 160 to 170, 170 to 180, 180 to 190 # with all combination of(non-)smokers and a median of pulse predicts(model1, "150-190,10;F;median", position = 1, doPar = FALSE) ## End(Not run)
## Not run: model1 = glm(Sex ~ Height + Smoke + Pulse, data=MASS::survey, family=binomial(link=logit)) summary(model1) # comparing person with hight 150 to 160, 160 to 170, 170 to 180, 180 to 190 # with all combination of(non-)smokers and a median of pulse predicts(model1, "150-190,10;F;median", position = 1, doPar = FALSE) ## End(Not run)
A simplified dataset of the Selects 2015 data. Selects 2015 was conducted after the elections to the national council in Switzerland on October 2015.
selects2015
selects2015
A data frame with 5337 rows and 15 variables:
the gender of the participant
the age of the participant
the canton where the participant lives
the highest education of the participant
Indicates if the participant participated in the national election or not
The party the participant mainly voted for: SVP, FDP, CVP, SP, GPS, GLP, BDP, other
political interest of the participant, self declaration
left right self placement
left right placement of SVP
left right placement of FDP
left right placement of CVP
left right placement of SP
left right placement of GPS
left right placement of GLP
left right placement of BDP
political knowledge scale between 0 and 4
opinion about social expense
opinion if Switzerland should join the EU
opinion if foreigners should be treated equal to Swiss
opinion if environment or economy is more important
opinion on nuclear energy
opinion on high income taxes
A weight to make the survey representative (design weight * turnout * vote_choice)
Selects: Post-electoral study - 2015 [Dataset]. Distributed by FORS, Lausanne, 2016. https://forscenter.ch/projects/selects/ doi:10.23662/FORS-DS-726-5