在 lm() 中使用列号而不是名称
我想按数字而不是名称指定列,而不是像 lm(bp~height+age, data=mydata)
。
我尝试了 lm(mydata[[1]]~mydata[[2]]+mydata[[3]]) 但问题是,在拟合模型中,系数被命名为 < code>mydata[[2]]、mydata[[3]]
等,而我希望它们具有真实的列名称。
也许这是鱼与熊掌兼得的情况,但如果专家能建议这是否可能,我将不胜感激
Instead of something like lm(bp~height+age, data=mydata)
I would like to specify the columns by number, not name.
I tried lm(mydata[[1]]~mydata[[2]]+mydata[[3]])
but the problem with this is that, in the fitted model, the coefficients are named mydata[[2]]
, mydata[[3]]
etc, whereas I would like them to have the real column names.
Perhaps this is a case of not having your cake and eating it, but if the experts could advise whether this is possible I would be grateful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用所需的索引数量来代替
c(2, 3)
(不需要 for 循环)。Instead of
c(2, 3)
you can use how many indices you want (no need for for loop).我在 R 课程中发现的技巧是删除响应列,否则您会收到警告“本质上完美适合:摘要可能不可靠”。我不知道它为什么有效,它不符合文档。通常,我们保留响应栏。
托马斯之前的回答的简化版本:
The trick that I found in a course on R is to remove the response column, otherwise you get warning "essentially perfect fit: summary may be unreliable". I do not know why it works, it does not follow from documentation. Normally, we keep the response column in.
And a simplified version of the earlier answer by Tomas: