获取特定变量的线性回归方程如何?

huangapple go评论58阅读模式
英文:

How to get the linear regression equation for a certain variable?

问题

Error in eval(predvars, data, env) : object 'OBP' not found

在尝试获取线性回归方程时,我一直遇到这个错误:

错误信息:在 eval(predvars, data, env) 中找不到对象 'OBP'。

这是我的代码段:

teams_oak %>%
select(G:FP) %>%
mutate(OBP=H+BB+HBP/AB+BB+HBP+SF) %>%
ggplot(aes(x=OBP,y=W))+
geom_point()+
geom_smooth(method = "lm")+
labs(title="OBP vs. Wins in MLB 1982-2002 ex:1994",x="On Base Percentage",y="Wins") %>%

teams_oak %>%
select(G:FP) %>%
mutate(OBP=H+BB+HBP/AB+BB+HBP+SF) %>%
m1<-lm(data = teams_oak,formula = W~OBP)

我尝试将所有函数串在一起,以确保代码正确读取突变的变量,但似乎无法找到它。

英文:

When trying to retreive a linear regression equation I keep getting this error:

Error in eval(predvars, data, env) : object 'OBP' not found

Here is my chunk,

teams_oak %&gt;%
  select(G:FP) %&gt;% 
  mutate(OBP=H+BB+HBP/AB+BB+HBP+SF) %&gt;%
  ggplot(aes(x=OBP,y=W))+
  geom_point()+
  geom_smooth(method = &quot;lm&quot;)+
  labs(title=&quot;OBP vs. Wins in MLB 1982-2002 ex:1994&quot;,x=&quot;On Base Percentage&quot;,y=&quot;Wins&quot;) %&gt;% 

  teams_oak %&gt;%
  select(G:FP) %&gt;% 
  mutate(OBP=H+BB+HBP/AB+BB+HBP+SF) %&gt;% 
  m1&lt;-lm(data = teams_oak,formula = W~OBP)

I've tried to pipe all the functions together to make sure that the code was reading for the mutated variable and it seems it cannot find it.

答案1

得分: 0

以下是已翻译的内容:

当您执行以下操作时:

  teams_oak %>% 
  select(G:FP) %>% 
  mutate(OBP=H+BB+HBP/AB+BB+HBP+SF) %>% 
  m1<-lm(data = teams_oak,formula = W~OBP)

在最后两行,您使用了管道运算符以及最后一行的赋值操作符。在该赋值操作中,您将teams_oak命名为数据,但由于这在管道中,管道尚未完成,因此它没有OBP变量,所以会失败。

我认为简单地省略最后一个管道操作符并将其转化为两个赋值语句应该可以工作,使用一个空行和缩进来表示管道的结束:

  teams_oak <- teams_oak %>%
    select(G:FP) %>%
    mutate(OBP=H+BB+HBP/AB+BB+HBP+SF)

  m1<-lm(data = teams_oak,formula = W~OBP)

个人而言,我不使用管道操作符,因为它容易以这种方式破坏代码。它不是编写清晰代码的最佳方式。

英文:

When you do this:

  teams_oak %&gt;%
  select(G:FP) %&gt;% 
  mutate(OBP=H+BB+HBP/AB+BB+HBP+SF) %&gt;% 
  m1&lt;-lm(data = teams_oak,formula = W~OBP)

on the last two lines you are using the pipe operator with an assignment on the last line. Within that assignment you are naming teams_oak as the data, but because this is in the pipe and the pipe hasn't finished yet, it doesn't have the OBP variable, so it fails.

I think simply omitting the last pipe operator and turning it into two assigment statements, should work, with a blank line and indentation to indicate the end of the pipe

  teams_oak &lt;- teams_oak %&gt;%
    select(G:FP) %&gt;% 
    mutate(OBP=H+BB+HBP/AB+BB+HBP+SF)

  m1&lt;-lm(data = teams_oak,formula = W~OBP)

Personally I don't use pipes because it does make it easy to break code this way. They are not the best way to write clear code.

huangapple
  • 本文由 发表于 2023年2月6日 08:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定