gmodel: Graph the function implicit in a model

Description Usage Arguments Details Examples

Description

Currently, this is the same as fmodel. I think the name gmodel makes more sense, and I want to be able to add additional functionality (such as including data points on the plot) without breaking fmodel.

Usage

1
2
3
gmodel(model = NULL, formula = NULL, data = NULL, nlevels = 3,
  at = list(), prob_of = NULL, intervals = c("none", "confidence",
  "prediction"), post_transform = NULL, ...)

Arguments

model

the model to display graphically

formula

setting the y ~ x + color variables

data

optional data set from which to extract levels for explanatory variables

nlevels

how many levels to display for those variables shown at discrete levels

at

named list giving specific values at which to hold the variables. You can accomplish this without forming a list by using .... See examples.

prob_of

if to show probability of a given level of the output, name the class here as a character string.

intervals

show confidence or prediction intervals: values "none", "confidence", "prediction"

post_transform

a scalar transformation and new name for the response variable, e.g. post_transform = c(price = exp) to undo a log transformation of price.

...

specific values for explantory variables and/or arguments to predict()

Details

#' Plot out model values

Often you will want to show some data along with the model functions. You can do this with 'ggplot2::geom_point()' making sure to set the data argument to be a data frame with the cases you want to plot.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## Not run: 
mod1 <- lm(wage ~ age * sex + sector, data = mosaicData::CPS85)
fmodel(mod1)
fmodel(mod1, ~ sector + sex + age) # not necessarily a good ordering
# show the data used for fitting along with the model
fmodel(mod1, ~ age + sex + sector, nlevels = 8) + 
  ggplot2::geom_point(data = mosaicData::CPS85, alpha = 0.1)
require(ggplot2)
fmodel(mod1, ~ age + sex + sector, nlevels = 8) + 
  geom_point(data = mosaicData::CPS85, alpha = 0.1) +
  ylim(0, 20)
mod2 <- lm(log(wage) ~ age + sex + sector, data = mosaicData::CPS85)
fmodel(mod2, post_transform = c(wage = exp)) # undo the log in the display
mod3 <- glm(married == "Married" ~ age + sex * sector,
            data = mosaicData::CPS85, family = "binomial")
fmodel(mod3, type = "response")
# Adding the raw data requires an as.numeric() trick when it's TRUE/FALSE
fmodel(mod3, ~ age + sex + sector, nlevels = 10, type = "response") + 
  geom_point(data = mosaicData::CPS85, 
  aes(x = age, y = as.numeric(married == "Married")), alpha = .1)

## End(Not run)

Example output

Loading required package: ggplot2
Warning: Ignoring unknown aesthetics: linetype
Warning message:
Removed 24 rows containing missing values (geom_point). 
Warning message:
In predict.lm(object, newdata, se.fit, scale = 1, type = ifelse(type ==  :
  prediction from a rank-deficient fit may be misleading
Warning message:
In predict.lm(object, newdata, se.fit, scale = 1, type = ifelse(type ==  :
  prediction from a rank-deficient fit may be misleading

statisticalModeling documentation built on May 29, 2017, 11:56 p.m.