sem.predict: Returns model predictions for SEM

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/sem.predict.R

Description

Returns predictions for responses in the list of structured equations.

Usage

1
sem.predict(object, newdata, sefit = FALSE, ...)

Arguments

object

a single model or list of regressions representing the structural equation model.

newdata

a data.frame of predictors used to generate the model predictions.

sefit

whether standard errors of predictions should be returned. Default is FALSE.

...

additional arguments passed to predict.

Details

Mixed model predictions includes only fixed effects and *not* random effects (default is level = 0 or re.form = 0). This can be changed by passing additional arguments from predict.lme and predict.merMod.

If sefit = TRUE for mixed models, then standard errors on predictions are estimated using fixed effects *only*. See explanation here: http://glmm.wikidot.com/faq.

Value

Returns a data.frame containing the new data and the predicted responses based on fixed effects only for the innermost level of grouping (i.e., population residuals) corresponding to re.form = NA for lme4 models and level = Q for nlme models.

Author(s)

Jon Lefcheck

See Also

predict, predict.lme, predict.merMod

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
# Load example data
data(shipley2009)

# Load model packages
library(lme4)
library(nlme)

# Create list of models 
shipley2009.modlist = list(
  
  lme(DD ~ lat, random = ~1|site/tree, na.action = na.omit, 
      data = shipley2009),
  
  lme(Date ~ DD, random = ~1|site/tree, na.action = na.omit, 
      data = shipley2009),
  
  lme(Growth ~ Date, random = ~1|site/tree, na.action = na.omit, 
      data = shipley2009),
  
  glmer(Live ~ Growth+(1|site)+(1|tree), 
        family=binomial(link = "logit"), data = shipley2009) 
  
)

# Create new data for predictions
shipley2009.new = data.frame(
  DD = seq(min(shipley2009$DD, na.rm = TRUE), 
           max(shipley2009$DD, na.rm = TRUE), 
           by = 0.01)
)

# Generate predictions
shipley2009.new.pred = sem.predict(shipley2009.modlist, shipley2009.new)
head(shipley2009.new.pred)

# Plot predicted fit
plot(shipley2009$Date ~ shipley2009$DD, col = "grey60")
lines(shipley2009.new.pred$Date.fit ~ shipley2009.new.pred$DD, lwd = 2, col = "red")

# Generate predictions with standard errors (based on fixed effects only)
shipley2009.new.pred = sem.predict(shipley2009.modlist, shipley2009.new, sefit = TRUE)

# Add 95 percent confidence intervals
lines(shipley2009.new.pred$DD, 
      shipley2009.new.pred$Date.fit + 2 * shipley2009.new.pred$Date.se.fit, 
      lwd = 1.5, lty = 2, col = "red")

lines(shipley2009.new.pred$DD, 
      shipley2009.new.pred$Date.fit - 2 * shipley2009.new.pred$Date.se.fit,
      lwd = 1.5, lty = 2, col = "red")

piecewiseSEM documentation built on May 29, 2017, 6:05 p.m.