• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Analysis Factor

The Analysis Factor

Statistical Consulting, Resources, and Statistics Workshops for Researchers

  • Home
  • About
    • Our Programs
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Guest Instructors
  • Membership
    • Statistically Speaking Membership Program
    • Login
  • Workshops
    • Online Workshops
    • Login
  • Consulting
    • Statistical Consulting Services
    • Login
  • Free Webinars
  • Contact
  • Login

R Is Not So Hard! A Tutorial, Part 5: Fitting an Exponential Model

by guest 10 Comments

by David Lillis, Ph.D.

In Part 3 and Part 4 we used the lm() command to perform least squares regressions. We saw how to check for non-linearity in our data by fitting polynomial models and checking whether they fit the data better than a linear model. Now let’s see how to fit an exponential model in R.

As before, we will use a data set of counts (atomic disintegration events that take place within a radiation source), taken with a Geiger counter at a nuclear plant.

The counts were registered over a 30 second period for a short-lived, man-made radioactive compound. We read in the data and subtract the background count of 623.4 counts per second in order to obtain the counts that pertain to the radio-active source.

Once again, cut and paste the following data into the R workspace.

A <- structure(list(Time = c(0, 1, 2, 4, 6, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30), 
Counts = c(126.6, 101.8, 71.6, 101.6, 68.1, 62.9, 45.5, 41.9, 
46.3, 34.1, 38.2, 41.7, 24.7, 41.5, 36.6, 19.6, 
22.8, 29.6, 23.5, 15.3, 13.4, 26.8, 9.8, 18.8, 25.9, 19.3)), .Names = c("Time", "Counts"), row.names = c(1L, 2L,
3L, 5L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L, 28L, 29L, 30L,
31L), class = "data.frame")

Let’s attach the entire dataset so that we can refer to all variables directly by name.

attach(A)
names(A)

Let’s see if an exponential function fits the data even better than a quadratic. We set up a grid of points and superpose the exponential function on the previous plot. An exponential function in the Time variable can be treated as a model of the log of the Counts variable.

exponential.model <- lm(log(Counts)~ Time)
summary(exponential.model)

R returns the following output:

Call:
lm(formula = log(Counts) ~ Time)

Residuals:
     Min       1Q   Median       3Q     Max 
-0.54715 -0.17618 0.02855 0.18850 0.55254 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept) 4.555249   0.111690   40.78 < 2e-16 ***
Time       -0.063915   0.006158 -10.38 2.36e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2814 on 24 degrees of freedom
Multiple R-squared: 0.8178,   Adjusted R-squared: 0.8102 
F-statistic: 107.7 on 1 and 24 DF, p-value: 2.362e-10

This model is pretty good, though it explains about 81% of the variance by comparison with the 89% explained by the quadratic model. Let’s plot it on a grid of time values from 0 to 30 in intervals of 0.1 seconds.

timevalues <- seq(0, 30, 0.1)
Counts.exponential2 <- exp(predict(exponential.model,list(Time=timevalues)))
plot(Time, Counts,pch=16)
lines(timevalues, Counts.exponential2,lwd=2, col = "red", xlab = "Time (s)", ylab = "Counts")

Note that we used the exponential of the predicted values in the second line of syntax above.

image1

So – we have fitted our exponential model. For our data the fitted exponential model fits the data less well than the quadratic model, but still looks like a good model.

In Part 6 we will look at some basic plotting syntax.

See our full R Tutorial Series and other blog posts regarding R programming

About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.

Bookmark and Share

Four Critical Steps in Building Linear Regression Models
While you’re worrying about which predictors to enter, you might be missing issues that have a big impact your analysis. This training will help you achieve more accurate results and a less-frustrating model building experience.

Tagged With: linear model, linear regression, lm, R

Related Posts

  • Linear Models in R: Improving Our Regression Model
  • R Is Not So Hard! A Tutorial, Part 4: Fitting a Quadratic Model
  • R is Not So Hard! A Tutorial, Part 2: Variable Creation
  • What R Commander Can do in R Without Coding–More Than You Would Think

Reader Interactions

Comments

  1. norman jones says

    July 25, 2020 at 10:50 pm

    Your exponential model was made by assuming that the best-fit exponential curve has no vertical or horizontal shift.
    If we use a model y=A*exp(k*(t-h))+v

    A 24.32223247
    k -0.110612853
    h 12.99889508
    v 14.02693519

    this model has a smaller sum of squared differences.

    Reply
  2. ana says

    January 21, 2019 at 4:48 pm

    Hi,

    Thank you for your tutorial, very helpful. I would like to ask why the intercept is ~4.55 instead of ~100.

    Thanks

    Reply
    • Karen Grace-Martin says

      March 4, 2019 at 11:25 am

      Hi Ana,

      It’s 4.55 on the log scale. It’s only around 100 once you exponentiate.

      Reply
  3. Abhishek says

    November 5, 2018 at 2:38 am

    Hi ,

    I wanted to plot a exponential graph with some data set (like x= cus_id and y=address_id), but how to do it in R serve .

    Could you please help me how can i design exponential regression on this data set in R language.

    Thanks,
    Abhishek

    Reply
  4. desp says

    November 3, 2018 at 3:25 pm

    hi,
    why you didnt use the nls() instead of lm().Iam asking that because exponential models are non-linear models

    Reply
  5. Alper says

    January 14, 2018 at 8:46 pm

    I believe the fitted equation is

    log(counts) = 4.555 – log(0.0639)*time

    or

    counts = e^4.555*(e^-0.0639)^time

    or

    counts = 95.11*(0.938^time)

    Reply
    • evgen says

      February 10, 2019 at 8:42 pm

      it is actually

      log(counts) = 4.555 – 0.0639*time

      counts = e^(4.555 – 0.0639*time)

      counts = e^4.555 * (e^-0.0639*time)

      counts = e^4.555 * (e^-0.0639)^time

      Reply
  6. Marina says

    September 13, 2016 at 4:06 am

    Please, would be very helpful if you can confirm that the fitted equation is:

    counts = 4.555-log(0.0639)*time

    Thank you!

    Reply
  7. Attia says

    November 1, 2015 at 12:19 pm

    Could you please write the equation of this fitted curve. Is it

    counts = 4.555-log(0.0639)*time

    Thanks

    Reply
  8. Conor says

    May 2, 2015 at 10:35 am

    Hi,
    How would you increase the slope of the fitted curve?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.

Primary Sidebar

Free Webinars

Binary, Ordinal, and Multinomial Logistic Regression for Categorical Outcomes (Signup)

This Month’s Statistically Speaking Live Training

  • April Member Training: Statistical Contrasts

Upcoming Workshops

  • Logistic Regression for Binary, Ordinal, and Multinomial Outcomes (May 2021)
  • Introduction to Generalized Linear Mixed Models (May 2021)

Read Our Book



Data Analysis with SPSS
(4th Edition)

by Stephen Sweet and
Karen Grace-Martin

Statistical Resources by Topic

  • Fundamental Statistics
  • Effect Size Statistics, Power, and Sample Size Calculations
  • Analysis of Variance and Covariance
  • Linear Regression
  • Complex Surveys & Sampling
  • Count Regression Models
  • Logistic Regression
  • Missing Data
  • Mixed and Multilevel Models
  • Principal Component Analysis and Factor Analysis
  • Structural Equation Modeling
  • Survival Analysis and Event History Analysis
  • Data Analysis Practice and Skills
  • R
  • SPSS
  • Stata

Copyright © 2008–2021 The Analysis Factor, LLC. All rights reserved.
877-272-8096   Contact Us

The Analysis Factor uses cookies to ensure that we give you the best experience of our website. If you continue we assume that you consent to receive cookies on all websites from The Analysis Factor.
Continue Privacy Policy
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

SAVE & ACCEPT