从lm中提取具有系数(R)的公式 [英] Extract Formula From lm with Coefficients (R)

查看:179
本文介绍了从lm中提取具有系数(R)的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个lm对象,想获取使用系数提取的公式.我知道如何不使用系数来提取公式,如何不使用公式来获取系数,但是不知道如何获取例如. y〜10 + 1.25b,而不是y〜b或截距表,b等的表.

I have an lm object and want to get the formula extracted with coefficients. I know how to extract the formula without coefficients, and how to get the coefficients without the formula, but not how to get eg. y ~ 10 + 1.25b as opposed to y~b or a table of what intercept, b etc. equal

这是我目前正在使用的代码:

This is the code I'm working with currently:

a = c(1, 2, 5)
b = c(12, 15, 20)

model = lm(a~b)
summary(model)
formula = formula(model)
formula
coefficients(model)

我想从以上得到的是y〜-5.326 + .51b

What I'd like to get from the above is y ~ -5.326 + .51b

谢谢

在我的实际代码中,我正在使用63种以上的预测变量和18种不同的模型,因此我希望可以在无需太多工作的情况下进行扩展.

In my actual code I'm working with over 63 predictors and 18 different models, so I'd like something that can scale up without too much work.

推荐答案

as.formula(
  paste0("y ~ ", round(coefficients(model)[1],2), " + ", 
    paste(sprintf("%.2f * %s", 
                  coefficients(model)[-1],  
                  names(coefficients(model)[-1])), 
          collapse=" + ")
  )
)
# y ~ -5.33 + 0.51 * b

这篇关于从lm中提取具有系数(R)的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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