Learn R Programming

lavaSearch2 (version 1.4)

extractData: Extract Data From a Model

Description

Extract data from a model using nlme::getData, riskRegression::coxDesign or model.frame.. If it fails it will try to extract it by its name according to model$call$data.

Usage

extractData(object, design.matrix, as.data.frame, envir)

# S3 method for lm extractData(object, design.matrix = FALSE, as.data.frame = TRUE, envir = environment())

# S3 method for coxph extractData(object, design.matrix = FALSE, as.data.frame = TRUE, envir = environment())

# S3 method for cph extractData(object, design.matrix = FALSE, as.data.frame = TRUE, envir = environment())

# S3 method for lvmfit extractData(object, design.matrix = FALSE, as.data.frame = TRUE, envir = environment())

# S3 method for gls extractData(object, design.matrix = FALSE, as.data.frame = TRUE, envir = environment())

# S3 method for lme extractData(object, design.matrix = FALSE, as.data.frame = TRUE, envir = environment())

Arguments

object

the fitted model.

design.matrix

[logical] should the data be extracted after transformation (e.g. conversion of categorical variables to dummy variables)? Otherwise the original data will be returned.

as.data.frame

[logical] should the output be converted into a data.frame object?

envir

[environment] the environment from which to search the data.

Value

a dataset.

Examples

Run this code
# NOT RUN {
set.seed(10)
n <- 101

#### linear regression ####
Y1 <- rnorm(n, mean = 0)
Y2 <- rnorm(n, mean = 0.3)
Id <- findInterval(runif(n), seq(0.1,1,0.1))
data.df <- rbind(data.frame(Y=Y1,G="1",Id = Id),
           data.frame(Y=Y2,G="2",Id = Id)
           )
m.lm <- lm(Y ~ G, data = data.df)
a <- extractData(m.lm, design.matrix = TRUE)
b <- extractData(m.lm, design.matrix = FALSE)

library(nlme)
m.gls <- gls(Y ~ G, weights = varIdent(form = ~ 1|Id), data = data.df)
c <- extractData(m.gls)
m.lme <- lme(Y ~ G, random = ~ 1|Id, data = data.df)
d <- extractData(m.lme)

library(lava)
e.lvm <- estimate(lvm(Y ~ G), data = data.df)
e <- extractData(e.lvm)
e <- extractData(e.lvm, design.matrix = TRUE)

#### survival #### 
library(survival)

# }
# NOT RUN {
  library(riskRegression) ## needs version >=1.4.3
  dt.surv <- sampleData(n, outcome = "survival")
  m.cox <- coxph(Surv(time, event) ~ X1 + X2, data = dt.surv, x = TRUE, y = TRUE)
  f <- extractData(m.cox, design.matrix = FALSE)
  f <- extractData(m.cox, design.matrix = TRUE)
  m.cox <- coxph(Surv(time, event) ~ strata(X1) + X2, data = dt.surv, x = TRUE, y = TRUE)
  f <- extractData(m.cox, design.matrix = TRUE)
# }
# NOT RUN {
#### nested fuuctions ####
fct1 <- function(m){
   fct2(m)
}
fct2 <- function(m){ 
   extractData(m)
}
g <- fct1(m.gls)
# }

Run the code above in your browser using DataLab