We were provided the following data that represents the US population growth from 1790 to 1850.
read.table('Final.csv')
## V1
## 1 A,pop
## 2 0,3.929
## 3 10,5.308
## 4 20,7.24
## 5 30,9.638
## 6 40,12.866
## 7 50,17.069
## 8 60,23.192
The A represents the year and the pop represents the population growth for that year. We were then assigned to read said table/data and plot with labels.
mydata <- read.csv('Final.csv')
pop <- mydata$pop
year <- mydata$A
plot(year,pop,xlab = 'Years (0= 1790)',ylab = 'Population Growth (Millions)',main = 'US population growth from 1790 to 1850')
The Years was measured using 1790 as 0 and the years from 1790 and population was measured in the millions. Once this data was plotted we could see that it is an exponential model. We were asked to determine the intercept and k values through the use of R’s nls2 command. Before I used nls2 I tried doing it manually to see what I’d get.
We understand this follows the following equation model: \(population= initial population * e^(k*year)\)
Since the beginning year of the data is 1790 we know our initial population is 3.929 million. So I can say that: \(population= 3.929 * e^(k*year)\)
Now to find k manually we need to use a point, I used the point 60 years and 23.192 million and substituted so now we have the following:
\(23.192= 3.929 * e^(60 *k)\)
To solve for k I divided 23.192 million by 3.929 million and got the following equation:
\(5.902774243=e^(60 *k)\)
Then to get rid of the e I simply did the ln of it on both sides and from ln rules we know that we can bring the exponent forward so we get:
\(1.775422451=60*k\)
And to find k I simply divided by 60 years and got k= 0.0295903742. Since I did this I had an idea of what value to start k at and the value of the initial population which was given.
Using the nls2 command I was able to have R calculate the initial and k value for the data set and since I did the manual calculations I knew where to begin k and the initial population at.
library(nls2)
## Loading required package: proto
tryfit <- nls2(pop ~ (popIni)* exp(k*year), start = list(popIni=3.9744064,k=0.0295903742))
tryfit
## Nonlinear regression model
## model: pop ~ (popIni) * exp(k * year)
## data: parent.frame()
## popIni k
## 3.97441 0.02934
## residual sum-of-squares: 0.04819
##
## Number of iterations to convergence: 2
## Achieved convergence tolerance: 9.818e-06
This gave me the following data:
summary(tryfit)
##
## Formula: pop ~ (popIni) * exp(k * year)
##
## Parameters:
## Estimate Std. Error t value Pr(>|t|)
## popIni 3.9744056 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: 2
## Achieved convergence tolerance: 9.818e-06
R gave us that the initial population was 3.9744056 which is our intercept and that the k value was 0.0293421. We were then asked to use this to plot the data with a line of best fit.
plot(year,pop,xlab = 'Years (0= 1790)',ylab = 'Population Growth (Millions)',main = 'US population growth from 1790 to 1850')
lines(year,predict(tryfit), col='blue')
This shows that our line of best fit fits very nicely on top of our data. This was very close compared to my manual estimation. This shows the growth rate is 0.0293421 which shows a percent error of ( 0.0295903742-0.0293421/ 0.0295903742 *100= 0.8%) 0.8%. This shows both values are very close.