Skip to content

Commit

Permalink
update inst/doc
Browse files Browse the repository at this point in the history
  • Loading branch information
pat-s committed Nov 12, 2016
1 parent ddce882 commit cd372eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
26 changes: 9 additions & 17 deletions inst/doc/function.tutorial.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,27 @@ library(cowplot)

## ---- results='hide'-----------------------------------------------------
library(oddsratio)
suppressPackageStartupMessages(library(mgcv))
set.seed(1234)
n <- 200
sig <- 2
dat <- suppressMessages(gamSim(1, n = n, scale = sig))
dat$x4 <- as.factor(c(rep("A", 50), rep("B", 50), rep("C", 50), rep("D", 50)))

fit.gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = dat)
fit.gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data.gam)

## ------------------------------------------------------------------------
calc.oddsratio.gam(data = dat, model = fit.gam, pred = "x2",
calc.oddsratio.gam(data = data.gam, model = fit.gam, pred = "x2",
values = c(0.099, 0.198))

## ------------------------------------------------------------------------
calc.oddsratio.gam(data = dat, model = fit.gam,
calc.oddsratio.gam(data = data.gam, model = fit.gam,
pred = "x4", values = c("A", "B"))

## ------------------------------------------------------------------------
calc.oddsratio.gam(data = dat, model = fit.gam, pred = "x2",
calc.oddsratio.gam(data = data.gam, model = fit.gam, pred = "x2",
percentage = 20, slice = TRUE)

## ------------------------------------------------------------------------
pl.smooth.gam(fit.gam, pred = "x2", title = "Predictor 'x2'")

## ------------------------------------------------------------------------
plot.object <- pl.smooth.gam(fit.gam, pred = "x2", title = "Predictor 'x2'")
or.object <- calc.oddsratio.gam(data = dat, model = fit.gam,
or.object <- calc.oddsratio.gam(data = data.gam, model = fit.gam,
pred = "x2", values = c(0.099, 0.198))

plot <- add.oddsratio.into.plot(plot.object, or.object, or.yloc = 3,
Expand All @@ -47,7 +41,7 @@ plot <- add.oddsratio.into.plot(plot.object, or.object, or.yloc = 3,
plot

## ------------------------------------------------------------------------
or.object2 <- calc.oddsratio.gam(data = dat, model = fit.gam,
or.object2 <- calc.oddsratio.gam(data = data.gam, model = fit.gam,
pred = "x2", values = c(0.4, 0.6))

add.oddsratio.into.plot(plot, or.object2, or.yloc = 2.1, values.yloc = 2,
Expand All @@ -58,14 +52,12 @@ add.oddsratio.into.plot(plot, or.object2, or.yloc = 2.1, values.yloc = 2,
arrow.length = 0.02, rect = TRUE)

## ------------------------------------------------------------------------
dat <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
dat$rank <- factor(dat$rank)
fit.glm <- glm(admit ~ gre + gpa + rank, data = dat, family = "binomial")
fit.glm <- glm(admit ~ gre + gpa + rank, data = data.glm, family = "binomial")

## ------------------------------------------------------------------------
calc.oddsratio.glm(data = dat, model = fit.glm, incr = list(gre = 380, gpa = 5))
calc.oddsratio.glm(data = data.glm, model = fit.glm, incr = list(gre = 380, gpa = 5))

## ------------------------------------------------------------------------
calc.oddsratio.glm(data = dat, model = fit.glm,
calc.oddsratio.glm(data = data.glm, model = fit.glm,
incr = list(gre = 380, gpa = 5), CI = 0.70)

34 changes: 13 additions & 21 deletions inst/doc/function.tutorial.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,14 @@ library(ggplot2)
library(cowplot)
```

## Load example data
## Load example data & fit model

Data source: `?mgcv::predict.gam`

```{r, results='hide'}
library(oddsratio)
suppressPackageStartupMessages(library(mgcv))
set.seed(1234)
n <- 200
sig <- 2
dat <- suppressMessages(gamSim(1, n = n, scale = sig))
dat$x4 <- as.factor(c(rep("A", 50), rep("B", 50), rep("C", 50), rep("D", 50)))
fit.gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = dat)
fit.gam <- mgcv::gam(y ~ s(x0) + s(I(x1^2)) + s(x2) + offset(x3) + x4, data = data.gam)
```

## GAM example
Expand All @@ -52,7 +46,7 @@ from 0.099 to 0.198 while holding all other predictors constant.


```{r}
calc.oddsratio.gam(data = dat, model = fit.gam, pred = "x2",
calc.oddsratio.gam(data = data.gam, model = fit.gam, pred = "x2",
values = c(0.099, 0.198))
```

Expand All @@ -62,10 +56,10 @@ These actions results in two log odds values, respectively, which are transforme

### Calculate OR for level change of indicator variable

If the predictor is a indicator variable, i.e. consists of fixed levels, you can use the function in the same way by just putting in the respective levels you are interested in:
If the predictor is an indicator variable, i.e. consists of fixed levels, you can use the function in the same way by just putting in the respective levels you are interested in:

```{r}
calc.oddsratio.gam(data = dat, model = fit.gam,
calc.oddsratio.gam(data = data.gam, model = fit.gam,
pred = "x4", values = c("A", "B"))
```

Expand All @@ -78,7 +72,7 @@ Here we slice predictor `x2` into 5 parts by taking the predictor values of ever


```{r}
calc.oddsratio.gam(data = dat, model = fit.gam, pred = "x2",
calc.oddsratio.gam(data = data.gam, model = fit.gam, pred = "x2",
percentage = 20, slice = TRUE)
```

Expand Down Expand Up @@ -106,7 +100,7 @@ the respective values we want to insert.

```{r}
plot.object <- pl.smooth.gam(fit.gam, pred = "x2", title = "Predictor 'x2'")
or.object <- calc.oddsratio.gam(data = dat, model = fit.gam,
or.object <- calc.oddsratio.gam(data = data.gam, model = fit.gam,
pred = "x2", values = c(0.099, 0.198))
plot <- add.oddsratio.into.plot(plot.object, or.object, or.yloc = 3,
Expand All @@ -127,7 +121,7 @@ take the already produced plot as an input to `add.oddsratio.into.plot()` and us
object:

```{r}
or.object2 <- calc.oddsratio.gam(data = dat, model = fit.gam,
or.object2 <- calc.oddsratio.gam(data = data.gam, model = fit.gam,
pred = "x2", values = c(0.4, 0.6))
add.oddsratio.into.plot(plot, or.object2, or.yloc = 2.1, values.yloc = 2,
Expand All @@ -143,13 +137,11 @@ Aesthetics like opacity or color are fully customizable.

## GLM example

Create example data.
Fit model.
Data source: http://www.ats.ucla.edu/stat/r/dae/logit.htm

```{r}
dat <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
dat$rank <- factor(dat$rank)
fit.glm <- glm(admit ~ gre + gpa + rank, data = dat, family = "binomial")
fit.glm <- glm(admit ~ gre + gpa + rank, data = data.glm, family = "binomial")
```

### Calculate odds ratio for continuous predictors
Expand All @@ -167,14 +159,14 @@ Indicator predictor `rank` has four levels. Subsequently, we will get three odds
The output is interpreted as follows: "Having `rank2` instead of `rank1` while holding all other values constant results in a decrease in odds of 49.1% (1-0.509)".

```{r}
calc.oddsratio.glm(data = dat, model = fit.glm, incr = list(gre = 380, gpa = 5))
calc.oddsratio.glm(data = data.glm, model = fit.glm, incr = list(gre = 380, gpa = 5))
```

You can also set other confident intervals for GLM(M) models. The resulting data
frame will automatically adjust its column names to the specified level.

```{r}
calc.oddsratio.glm(data = dat, model = fit.glm,
calc.oddsratio.glm(data = data.glm, model = fit.glm,
incr = list(gre = 380, gpa = 5), CI = 0.70)
```

Expand Down

0 comments on commit cd372eb

Please sign in to comment.