R:从lm自动提取线性回归方程 [英] R: Automate Extraction of Linear Regression Equation from lm

查看:515
本文介绍了R:从lm自动提取线性回归方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道从lm对象中提取完整线性方程的现有函数吗?

Does anyone know of an existing function to extract the full linear equation from a lm object?

假设我有:

lm1 = lm(y~x1+x2...xn, data=df)

对于本人正在参加的回归课程,教授反复希望得到以下形式的回归方程:e(y)= b1 + b2x1 [...] bnx(n-1).

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', [...])

这样已经进行了数周.复制并粘贴上面的粘贴函数并不是一个大问题,但是他希望在其中而不是y,x1等处使用实际的变量标签.如您所见,反复这样做会伤害到我的手脚.

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.

注意:这与此处提出的问题非常相似,但又不完全相同: 从lm中提取具有系数(R)的公式

Note: this is quite similar but also not quite the same question as posed here: Extract Formula From lm with Coefficients (R)

为什么不同?这些问题解决了一个一次性案例,即如何从lm对象提取线性回归方程式?"这个问题是,是否存在现有的基本方法,或者(如果没有)用于从lm对象系统地获取线性回归方程的方法.当您看到此处给出的答案与在另一页上接受的答案.

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.

推荐答案

我对这一问题的幼稚方法也将是使用paste0()定制我自己的函数:

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"

wrt.注释:为了用变量名替换y并将交互操作符更改为*重写:

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"

这篇关于R:从lm自动提取线性回归方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆