simu_pred: Exporting Simulated Predictions

Description Usage Arguments Value Examples

View source: R/simu_pred.R

Description

Generate prediction through Monte Carlo simulation based on approximation.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
simu_pred(
  m,
  profile = NULL,
  y.label = "auto",
  at = "means",
  type = "response",
  show.ci = TRUE,
  level.ci = 0.95,
  vcov.est = NULL,
  robust.type = "HC1",
  cluster.var = NULL,
  iterate.num = 1000,
  iterate.seed = 578,
  rawbeta = NULL,
  dropbeta = NULL,
  ...
)

Arguments

m

Single model object.

profile

The case profile (matrix or data.frame). Column names are variables in the model and prediction is made for each row. Any missing variable in the profile is substituted by the rule defined by at. The default is NULL (all values are set using at option).

y.label

The label of the dependent variable (optional, character).

at

Values to be fixed if not specified in profile. Choose from "means" (default), "medians", or NULL (Stop if any variable is missing in profile)

type

If "response" (default), the output is transformed to the response format. If "asis", transformation will not occur.

show.ci

Show confidence interval (boulean). The default is TRUE.

level.ci

The level used for confidence interval (numeric: 0-1). The default is 0.95.

vcov.est

Variance-covariance matrix to draw coefficents. If "robust", use robust variance-covariance matrix (also see robust.type). If "cluster", use cluster robust variance-covariance matrix (also see cluster.var). If "boot", draw beta by non-parametric bootstrap method using Boot function. If "rawbeta", use pre-simulated beta given in rawbeta argument. If NULL (default), use the standard variance covariance matrix stored the model. You can also directly set the variance-covariance matrix created by vcov() or vcovHC().

robust.type

The type of leverage adjustment passed to vcovHC (applied only when vcov.est=="robust").

cluster.var

A vector, matrix, or data.frame of cluster variables, where each column is a separate variable. Alternatively, a formula specifying the cluster variables to be used (see Details in cluster.vcov. Applied only when vcov.est=="cluster".)

iterate.num

The number of iteration in simulation.

iterate.seed

The seed value for random number generator used for the draws from multivariate normal distribution.

rawbeta

The matrix of pre-simulated beta. Columns are variables, raws are simulated cases. Used only when vcov.est=="rawbeta".

dropbeta

If not NULL, beta of specified locations (numeric vector) are not used for prediction (rarely used).

...

Additional arguments passed to vcovHC, cluster.vcov, or Boot depending on the value of vcov.est.

Value

A list of:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## Load Data
library(pscl)
data(vote92)

## Recode Variables
vote92$voteBush <- as.numeric(
factor(vote92$vote,levels=c("Clinton","Bush")))*1 - 1
vote92$bushdis <- sqrt(vote92$bushdis)
vote92$clintondis <- sqrt(vote92$clintondis)

## Estimate Logistic Regression
fm <- formula(voteBush ~ dem + rep +
                clintondis + bushdis +
                persfinance + natlecon)
m <- glm(fm, data = vote92,
         family = binomial("logit"))

## Comparing Partisans

# Profile
prof1 <- data.frame(dem=c(1,0,0),rep=c(0,0,1))

# Prediction (Missing Variables are Fixed at Mean/Mode)
predprof1 <- simu_pred(m, prof1, y.label = "Bush Vote")
summary(predprof1)

# Additional Profile to Give Labels
addprof1 <- data.frame(pid=c("Democrat","Independent","Republican"))
addprof1$pid <- factor(addprof1$pid, levels=unique(addprof1$pid))

# Plot 
plot_simu(predprof1, name.x="pid", addprof=addprof1, label.x = "Party ID")
# Change it to Point Graph
plot_simu(predprof1, name.x="pid", addprof=addprof1, 
          label.x = "Party ID", type.est = "point")

## Comparing Effects of Ideological Distance by Party ID

# Profile
prof2 <- data.frame(dem=rep(rep(c(1,0,0),each=50),2),
                    rep=rep(rep(c(0,0,1),each=50),2),
                    bushdis=c(rep(seq(0,4,length=50),3), 
                              rep(0.5,150)),
                    clintondis=c(rep(0.5,150), 
                                 rep(seq(0,4,length=50),3)))

# Prediction (Missing Variables are Fixed at Mean/Mode)
predprof2 <- simu_pred(m, prof2, y.label = "Bush Vote")
summary(predprof2)

# Additional Profile to Give Labels
addprof2 <- data.frame(pid=rep(c("Democrat","Independent","Republican"),each=50),
                       dis=rep(seq(0,4,length=50),6),
                       labdis=rep(c("Ideological Distance from Bush",
                                    "Ideological Distance from Clinton"),each=150))
addprof2$pid <- factor(addprof2$pid, levels=rev(unique(addprof2$pid)))

# Plot 
plot_simu(predprof2, name.x="dis", addprof=addprof2,
          name.facet.x = "labdis", name.linetype="pid",
          label.x = NULL, label.linetype="Party ID")
# Change Color of CIs
plot_simu(predprof2, name.x="dis", addprof=addprof2,
          name.facet.x = "labdis", name.linetype="pid", name.fill="pid",
          label.x = NULL, label.linetype="Party ID")

gentok/estvis documentation built on April 2, 2020, 1:58 p.m.