0
$\begingroup$

I am running exponential regression on my data, but when trying to plot the curve I get a Inf problem because the independent variable values seem to be too high. How can I solve this in order to be able to plot the real values of the independent variable and the regression line incl standard errors in the same plot?

DPUT of my data:

structure(list(YR = c(1960, 1961, 1962, 1963, 1964, 1965, 1966, 
1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 
1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016), count = c(1L, 3L, 2L, 4L, 
3L, 5L, 3L, 10L, 9L, 6L, 9L, 7L, 7L, 10L, 7L, 10L, 13L, 7L, 10L, 
12L, 15L, 12L, 11L, 9L, 10L, 16L, 21L, 9L, 10L, 9L, 18L, 19L, 
14L, 14L, 13L, 21L, 20L, 18L, 28L, 21L, 27L, 26L, 25L, 24L, 25L, 
34L, 37L, 31L, 59L, 45L, 49L, 42L, 48L, 65L, 52L, 62L, 49L)), .Names =
c("YR", "count"), class = "data.frame", row.names = 27:83)

And my script:

ggplot(data.df,aes(x=YR,y=count)) +
  scale_y_continuous(limits=c(0,75),breaks=c(10,20,30,40,50,60,70),expand=c(0,0)) +
  scale_x_continuous(limits=c(1960,2018),breaks=c(1960,1965,1970,
                                              1975,1980,1985,1990,1995,
                                              2000,2005,2010,2015)) +
  geom_bar(stat='identity',width=0.8) +
  stat_smooth(method="lm",formula=y~exp(x),se=T, col=2) +
  xlab(" ") + ylab("count")
$\endgroup$
1
  • 1
    $\begingroup$ I disaggree with the close votes. OP has a statistical issue disguised by their programming problems. This question at its heart is on-topic. $\endgroup$
    – Roland
    Feb 14, 2017 at 8:47

1 Answer 1

2
$\begingroup$

You are using the wrong model. You should not transform the x-variable, but log-transform the y-variable to fit an exponential model with lm. That's not possible within ggplot2.

However, judging from your variable names, you have count data. A Poisson GLM seems a better choice then:

library(ggplot2)
mod <- glm(count~YR, data = data.df, family = "poisson")
pred.df <- data.frame(YR = seq(min(data.df$YR), max(data.df$YR), length.out = 100))
pred <- predict(mod, newdata = pred.df, se.fit = TRUE)
pred.df$count <- exp(pred$fit)
pred.df$countmin <- exp(pred$fit - 2 * pred$se.fit)
pred.df$countmax <- exp(pred$fit + 2 * pred$se.fit)

I also see absolutely no good reason to make a barplot for this data. Points can depict the data much better.

ggplot(data.df,aes(x=YR,y=count)) +
  scale_y_continuous(limits=c(0,75),breaks=c(10,20,30,40,50,60,70),expand=c(0,0)) +
  scale_x_continuous(limits=c(1960,2018),breaks=c(1960,1965,1970,
                                                  1975,1980,1985,1990,1995,
                                                  2000,2005,2010,2015)) +
  geom_point() +
  geom_ribbon(data = pred.df, aes(ymin = countmin, ymax = countmax), alpha = 0.3) +
  geom_line(data = pred.df) +
  xlab(" ") + ylab("count")

resulting plot

Note that you also have a time series and should check for autocorrelation of residuals (which appears to be rather pronounced) and use an appropriate model for time series instead. (Also check for over/underdispersion as part of the model diagnostics and change to a more appropriate GLM if necessary.).

You should also study this answer.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.