Reputation: 3
I want to add a random curve to a plot to test whats fitting best to my data.
df <- data.frame(a = c(-1.973, -1.685, 2.050, 0.496, 3.310, 2.604, 1.477, 5.552, 2.039, 1.503, 2.171, 1.095),
b = c(21,27,36,36,66,53,40,40,65,39,37,32))
lm(b~a,data = df)plot(df$a, df$b, xlab = "a", ylab = "b")
x<-lm(df$b~df$a)$coefficients[1]
curve(x+lm(df$b~df$a)$coefficients[2], add=TRUE, col="red")
Though this plot I can draw a regression line (picture) but can I also add a random curve?
When I run the code, I only see the curve and not the data. Camn anyone help what I have to do that the data and the curve is visible? Thanks...
Upvotes: 0
Views: 181
Reputation: 206496
I'm not sure exactly what you mean by "random curve". You seem to just be trying to plot the regression line. In this case, you can just use abline
reg <- lm(b~a,data = df)
plot(df$a, df$b, xlab = "a", ylab = "b")
abline(reg, col="red")
Or if you really wanted to use
curve()
, the x
variable is special and needs to be able to change. So it would be
curve(reg$coefficients[1] + x*reg$coefficients[2], add=TRUE, col="red")
Upvotes: 0