x y data set provided is US population growth from 1790 to 1850
year 1790 = 0 because using actual years will explode the exponent!
year <- c(0,10,20,30,40,50,60)
population in millions
pop <- c( 3.929, 5.308, 7.240, 9.638, 12.866, 17.069, 23.192)
read data and plot with labels
determine intercept and k values through nls2, assuming exponential function. pop = pop(ini) * exp (k*year)
plot fit curve with the data
annotate and convert to rmarkdown & rpub
submit to form by final date, Dec 18
### Part 1: Programming Project Code
## (1) read data and plot with labels
# year 1970=0
year<-c(0,10,20,30,40,50,60)
# population in millions
pop<-c(3.929,5.308,7.240,9.638,12.866,17.069,23.192)
# generating plot of Population vs Time
plot(year,pop,main="US Population Growth from 1790 to 1850",xlab="Time Since 1790 (years)",ylab="Population (millions)",ylim=c(0,max(pop)))
## (2) determine intercept and k values through nls2, assuming exponential function. pop = pop(ini) * exp (k*year)
# importing nls2 library
library(nls2)
## Loading required package: proto
# calling nls2 function with expected model
model<-nls2(pop~pop_ini*exp(k*year),start=c(pop_ini=3.9,k=0.03))
# printing summary of nsl2 fitted model
# pop_ini (intercept) = 3.9744
# k = 0.0293
summary(model)
##
## Formula: pop ~ pop_ini * exp(k * year)
##
## Parameters:
## Estimate Std. Error t value Pr(>|t|)
## pop_ini 3.9744064 0.0407277 97.58 2.14e-09 ***
## k 0.0293421 0.0002023 145.02 2.96e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.09817 on 5 degrees of freedom
##
## Number of iterations to convergence: 3
## Achieved convergence tolerance: 2.369e-08
## (3) plot fit curve with the data
# generating line with nls2 fitted model
lines(year,predict(model),col="blue")