1

Suppose that I have these data set:

x <- 1:100
y <- 2 +  70* x
z <- 2 + x^2
index <- c(rep(1,100),rep(2,100))

x <- c(x,x)
t<-  c(y,z)
data <- data.frame(x,t,index)

data[,2]= data[,2] + rnorm(200,500,400)

ggplot(data, aes(x = x, y = t, colour = factor(index))) + geom_point() + stat_smooth(method = "lm", formula = y ~ x, se = FALSE)

The ggplot function just fit a linear model that suits for y. How can we add a quadratic model for z to the above function in addition to the linear model.

I look for a better way than this post: ggplot2 - plot multiple models on the same plot

Community
  • 1
  • 1
user1436187
  • 3,252
  • 3
  • 26
  • 59
  • I am not sure what you mean by "adding a quadratic model for z". z is to be explained by a quadratic function of x? If so, just modify your formula to "z ~ poly(x,2)". And, of course, include z in the data. – Maxim.K Jul 05 '13 at 12:52
  • I want a linear model for `y` and a quadratic model for `z`. One formula will be `y ~ x` and the other will be `y ~ x + x^2`. – user1436187 Jul 05 '13 at 13:07

2 Answers2

1

Still not entirely sure how you mean, but I imagine this would be a step in the direction that you want:

ggplot(data, aes(x = x, y = t, colour = factor(index))) + 
  stat_smooth(method = "lm", formula = y ~ x, se = FALSE) +
  stat_smooth(method = "lm", formula = y ~ poly(x,2), se = FALSE)

In your data you have one group (index = 1) that is connected by a linear function, and the other by a quadratic function. If you specify formula y ~ x, a straight regression lines is fitted for both groups, and this is not what you want, correct?

The code above produces two layers, first one your initial layer with two straight lines, the second one contains one straight line and one quadratic curve. Since the linear group is the same in both graphs you see only three lines. If you want to remove the portion with two straight lines, delete the second code line, so that only formula y ~ poly(x,2) remains. poly(x,2) is equivalent to x + I(x^2) which reflects the mathematical formula of y = x + x².

I have removed the geom_point() from the code so that it does not obscure the results. Feel free to include it back. Hope this is what you need.

Maxim.K
  • 4,120
  • 1
  • 26
  • 43
  • Thanks, this is solve my problem but how can I remove the linear regression line for the group two (Quadratic Model). I want only one line and one curve. – user1436187 Jul 06 '13 at 03:16
  • @user1436187 Sorry but I don't seem to understand your needs. You said you need the quadratic model. If you remove the second line (with ´formula = y ~ x´ then you will get one straight line and one curve. You cannot get a curve for the first group, as it is a perfect line. The quadratic equation for that group will have 0 as coefficient for the quadratic term, so that the resulting quadratic function for that group will be a special case: 0*x² + b1*x + a – Maxim.K Jul 06 '13 at 18:13
0

The following code has solved my problem:

ggplot() + layer (data = data[1:100,], mapping=aes(x = x, y = t,colour = factor(index)),stat = "identity") + 
 stat_smooth(data=data[1:100,], mapping = aes(x = x, y = t), method = "lm", formula = y ~ x, se = FALSE) + 
 layer(data=data[100:200,], mapping=aes(x = x, y = t,colour = factor(index)), stat = "identity") + 
 stat_smooth(data = data[100:200,], mapping = aes(x = x, y = t), method = "lm", formula = y ~ poly(x,2), se = FALSE)
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
user1436187
  • 3,252
  • 3
  • 26
  • 59
  • You can now accept your answer by clicking on the check mark to the left of this answer. When possible you should do the same in your other questions too. – Julius Vainora Jul 07 '13 at 07:46