1

I'm using qplot to plot and fit a slope to my data:

qplot(log(X),log(Y),geom=c("point","smooth"),method="gam",formula=y~ns(x,2))

It works fine. But how do I get the coefficients of the plotted regression slope? I'm aware I can get the slope explicitly using, e.g., nls. Nonetheless, I'd like to know what slope qplot is fitting to my data. I'm grateful for any advice.

  • 1
    see http://stackoverflow.com/a/9790803/1317221 – user1317221_G Nov 28 '13 at 14:18
  • 3
    [**This**](http://stackoverflow.com/questions/8845279/predicted-values-for-logistic-regression-from-glm-and-stat-smooth-in-ggplot2-are/8846361#8846361) may also be relevant, i.e. "geom_smooth isn't supposed to replace actual modeling" – Henrik Nov 28 '13 at 14:22

1 Answers1

1

How about this:

g<-qplot(log(X),log(Y),geom=c("point","smooth"))
lookinside<-ggplot_build(g)$data[[2]]
smooth<-data.frame(cbind(x=lookinside$x,y=lookinside$ymax-lookinside$ymin))
lm(smooth$x~smooth$y)

  Call:
    lm(formula = smooth$x ~ smooth$y)

  Coefficients:
    (Intercept)     smooth$y  
  1.25482      0.06275 


qplot(smooth$x,smooth$y)+geom_abline(intercept=1.25482,slope=0.06275,color="red")

enter image description here

Troy
  • 8,581
  • 29
  • 32