问题
Does anyone know of an existing function to extract the full linear equation from a lm object?
Suppose I have:
lm1 = lm(y~x1+x2...xn, data=df)
For this course in regression I'm taking, the professor repeatedly wants the resulting regression equation in the form: e(y) = b1 +b2x1 [...] bnx(n-1).
Currently, I am doing something like this:
(paste("y=", coef(lm1)[1], '+', coef(lm1[2]), '*x2', [...])
It has carried on for weeks like this. It would not be a huge problem to copy and paste the paste function above, but he wants the actual variable labels in there instead of y,x1, etc. As you can see, my hand and mind are hurting from repeatedly doing this.
I have crossed my pain threshold for this and today started to think about to create my own function for this, but I'm just checking to see if anyone knows of an existing function that does this automatically.
I don't think it will be exceptionally difficult to create a function that does the same paste function except on a variable length for the number of coefficients, but it's just something where I would rather go with an existing solution given the end product is such a useless construct.
Note: this is quite similar but also not quite the same question as posed here: Extract Formula From lm with Coefficients (R)
Why is it different? That questions addresses a one off case of, "how do you extract a linear regression equation from an lm object?" This question is, "is there a an existing base method or (if not) a method for systematically getting the linear regression equation from an lm object. This is especially apparent when you see the answer posed here versus the accepted answer on the other page.
回答1:
My naive approach to this problem would also be to customize my own function using paste0()
:
regEq <- function(lmObj, dig) {
paste0("y = ",
paste0(
c(round(lmObj$coef[1], dig), round(sign(lmObj$coef[-1])*lmObj$coef[-1], dig)),
c("", rep("*", length(lmObj$coef)-1)),
paste0(c("", names(lmObj$coef)[-1]), c(ifelse(sign(lmObj$coef)[-1]==1," + "," - "), "")),
collapse=""
)
)
}
where it gets a little messy because of the signs and intercept. Lets look at a simple call:
> fit <- lm(-mpg ~ cyl + hp + drat, data=mtcars)
> fit
Call:
lm(formula = -mpg ~ cyl + hp + drat, data = mtcars)
Coefficients:
(Intercept) cyl hp drat
-22.51406 1.36060 0.02878 -2.84090
> regEq(fit,3)
[1] "y = -22.514 + 1.361*cyl + 0.029*hp - 2.841*drat"
EDIT:
wrt. comment: In order to replace y
with the variable name and change the interaction operator to *
rewrite:
regEq <- function(lmObj, dig) {
gsub(":", "*",
paste0(
names(lmObj$model)[1]," = ",
paste0(
c(round(lmObj$coef[1], dig), round(sign(lmObj$coef[-1])*lmObj$coef[-1], dig)),
c("", rep("*", length(lmObj$coef)-1)),
paste0(c("", names(lmObj$coef)[-1]), c(ifelse(sign(lmObj$coef)[-1]==1," + "," - "), "")),
collapse=""
)
)
)
}
> regEq(lm(mpg ~ hp * drat, data=mtcars), 3)
[1] "mpg = 5.55 - 0.013*hp + 6.069*drat - 0.01*hp*drat"
来源:https://stackoverflow.com/questions/31770729/r-automate-extraction-of-linear-regression-equation-from-lm