使用1/var作为第二个变量时lm()给出错误结果的问题[重复]

pod7payv  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(125)

This question already has answers here:

What does the capital letter "I" in R linear regression formula mean? (2 answers)
Closed 8 hours ago.
I have a dataframe with two variables that I will plot with the inverse of the varaibles and make a linear regression on this.

linear_mod <- lm((1/df$var1[3:length(var1)]) ~       # 3 because the first two are 0 and would result in 1/0
                 (1/df$var2[3:length(var2)]))
png("lineweaver-burk_glc6p.png", height = 400)
plot(1/(df$var1), 1/(df$var2))
abline(linear_mod)

however this just results in y=0.897, with no slope.
I know I can assign 1/var to two variables and use them to get it to work like so

temp1 <- 1/df$var1[3:length(df$var1)]
temp2 <- 1/df$var2[3:length(df$var2)]

which does result in a correct regression line (0.83022 + 0.01768x), but I would like to know what causes the lm() function to not function when using the above.
I have tested using one temp variable and one explicitly written out, and this only gives a slope when the temp variable occupies the second spot, so the lm() function only seem to accept the 1/df$var1[3:length(var1)] if it is before ~, and not after ~.
Changing it to just be without 1/ on the second variable makes it give a slope.

linear_mod <- lm((1/df$var1[3:length(var1)]) ~       # 3 because the first two are 0 and would result in 1/0
                 (df$var2[3:length(var2)]))

and putting for example 2+df$var... as the second variable also gives a slope, thus there seem to be something specific with / and it seems to be an inconsistency between using this math in the first and second variable and I wonder why this is the case. Putting the second variable inside c() does make it work, but I don't see why that wouldn't also be necessary for the first variable.
Here is a teble with the variables in the dataframe. |var2|var1 | |----|------------------| |0 |0.0133976420150054 | |0 |0.00803858520900322| |0.1|1.17363344051447 | |0.1|1.13076098606645 | |0.2|2.05787781350482 | |0.2|2.18113612004287 | |0.2|1.7524115755627 | |0.2|0.844051446945338 | |0.2|1.42550911039657 | |0.3|0.244908896034298 | |0.3|0.616291532690247 | |0.3|1.39067524115756 | |0.3|0.669882100750268 | |0.3|1.66934619506967 | |0.3|1.56752411575563 | |0.3|1.33976420150054 | |0.4|1.83547695605573 | |0.4|1.77920685959271 | |0.5|1.83547695605573 | |0.5|1.84887459807074 | |1 |1.92390139335477 | |2 |1.94533762057878 | |2 |1.7470525187567 |

mutmk8jj

mutmk8jj1#

使用带lm的公式界面时(),您需要使用I()表示法。有关详细信息,请检查?公式。这是因为除法运算符**/不按元素工作,而是对整个向量执行一次除法,因此您将得到一个标量值。因此,如果你想使用变量的逆来执行回归,你必须使用I()函数来告诉R把除法当作一个元素级的运算。同样,如果你想跳过前两行数据,你可以简单地在lm()函数中添加data = df [-c(1:2),]**,如下所示:

linear_mod <- lm(I(1/var1) ~ I(1/var2), data=df[-c(1:2),])

希望能有所帮助

相关问题