exponential fit in ggplot R

前端 未结 1 2018
南方客
南方客 2021-01-24 10:10

I\'ve been trying to fit an exponential curve to my data using ggplot and geom_smooth. I\'m trying to replicate the answer to a similar problem (geom_smooth and exponential fits

相关标签:
1条回答
  • 2021-01-24 10:40

    Set up data:

    dd <- data.frame(x=c(1981,1990,2000:2013),
      y = c(3.262897,2.570096,7.098903,5.428424,6.056302,5.593942,
      10.869635,12.425793,5.601889,6.498187,6.967503,5.358961,3.519295,
      7.137202,19.121631,6.479928))
    

    The problem is that exponentiating any number larger than about 709 gives a number greater than the maximum value storeable as a double-precision floating-point value (approx. 1e308), and hence leads to a numeric overflow. You can easily remedy this by shifting your x variable:

    lm(y~exp(x),data=dd) ## error
    lm(y~exp(x-1981),data=dd) ## fine
    

    However, you can plot the fitted value for this model more easily as follows:

    library(ggplot2); theme_set(theme_bw())
    ggplot(dd,aes(x,y))+geom_point()+
       geom_smooth(method="glm",
                method.args=list(family=gaussian(link="log")))
    
    0 讨论(0)
提交回复
热议问题