The graph below shows the population growth since 1790, along with the parameters of the fitted exponential model.
# Data
x <- c(0, 10, 20, 30, 40, 50, 60)
y <- c(3.929, 5.308, 7.240, 9.638, 12.866, 17.069, 23.192)
# Fit the model
fit <- nls(y ~ a * exp(k * x), start = list(a = 3.9, k = 0.03))
# Adjust plot limits to fit labels
x_range <- range(x)
y_range <- range(y) * c(1, 1.1) # Increase upper y limit by 10%
# Plot the data and fit
plot(x, y, xlab = "Years since 1790", ylab = "Population (millions)",
main = "Population since 1790", type = "o", col = "black", pch = 16,
ylim = y_range) # Adjust y-axis limits
grid()
lines(x, predict(fit, list(x = x)), col = "blue", lwd = 2)
# Add point labels
labels <- round(y, 3) # Labels for each point
text(x, y, labels = labels, pos = 3, cex = 0.8, col = "red") # Position above points
# Add legend
legend("topleft", legend = c("Data", "Exponential Fit"),
col = c("black", "blue"), pch = c(16, NA), lty = c(NA, 1), lwd = c(NA, 2))
# Extract parameters
params <- coef(fit)
a <- params["a"]
k <- params["k"]
cat("**Intercept (a):**", round(a, 3), "\n")
## **Intercept (a):** 3.974
cat("**Growth rate (k):**", round(k, 3), "\n")
## **Growth rate (k):** 0.029