Reputation: 21
I'm trying to form a regression model in R, of the form Y = A+BX.
x<-rpois(100, 50);
y<-rpois(100, 100);
df = cbind.data.frame(x, y, x*y, x^2, x^3, x^4, y*x^2);
plot(y~x, col = "black", data = df);
regmodel1<-lm(y~x, data = df)
abline(regmodel1, lwd=3, col="red")
coef1<-solve(rbind(c(1*100, sum(df$x)), c(sum(df$x), sum(df$`x^2`))), cbind(c(sum(df$y), sum(df$`x * y`))));
eq1=function(x){coef1[1]+coef1[2]*x}
par(new=TRUE)
plot(eq1, lwd=3, col="blue", axes=FALSE)
but using the lm command and the concept of normalized equations
give two very different regression lines.
why....?
Upvotes: 0
Views: 67
Reputation: 2949
Both are lines are almost same with small difference between the predicted and actual values. As you have kept the axis = FALSE, you couldn't see the difference in scale. If you can set the y axis limit same for both the plots, you could see the lines appearing together with small difference in slope and intercept.
BTW, I don't see a reason to manually calculate the coefficients, when you can get is by running regmodel1$coefficients
. Also, you create a custom function function(x)
, whenever you are using the function you need to call the x in the function, like eq1(df$x)
. Since you have not called the values, the plot has taken the distribution from 0 to 1. That's why when you plot the second one keeping axis=TRUE, you could see the x axis between 0 and 1 instead of the original x values.
P.S.: Instead of having function(x){coef1[1]+coef1[2]*x}
, you can use predict(regmodel1)
Upvotes: 1