r - Extracting dependent variable from lm object -
is there function extract y lm object?
i use residual(m) , predict(m) using object internal structures extract y...
m = lm(y ~ x1, d) head(m$model$y) [1] -0.791214 -1.291986 -0.472839 1.940940 -0.977910 -1.705539
you use model.frame()
, following:
# stats::lm documentation ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("ctl","trt")) weight <- c(ctl, trt) lm1 <- lm(weight ~ group) model.frame(lm1)$weight ## [1] 4.17 5.58 5.18 6.11 4.50 4.61 5.17 4.53 5.33 5.14 4.81 4.17 4.41 ## 3.59 5.87 3.83 6.03 4.89 4.32 4.69
if call function on 1 or more of variables in formula, like
lm2 <- lm(log(weight) ~ group)
you can untransformed values get_all_vars(lm2)$weight
(model.frame()
returns transformed values).
if want see functions (particularly extractor functions) available particular class, can check using methods(class = "lm")
(or whatever object class you're interested in).
Comments
Post a Comment