Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
library(readxl)data <-read_excel ("C:/Users/PC/Desktop/1978-2024全国人口数据.xlsx", sheet ="Sheet2")years <-as.numeric(data$指标)population <-as.numeric(data$`年末总人口(万人)`)pearl_curve <-function(t, L, a, b) {return(L / (1+ a *exp(-b * t)))}fit <-nls(population ~pearl_curve(years-1978, L, a, b), start =list(L =max(population) *1.1, a =10, b =0.05))summary(fit)
Formula: population ~ pearl_curve(years - 1978, L, a, b)
Parameters:
Estimate Std. Error t value Pr(>|t|)
L 1.506e+05 6.795e+02 221.68 <2e-16 ***
a 5.843e-01 5.820e-03 100.40 <2e-16 ***
b 5.038e-02 1.133e-03 44.46 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 566.4 on 43 degrees of freedom
Number of iterations to convergence: 9
Achieved convergence tolerance: 3.085e-06
L_fit <-coef(fit)[["L"]]a_fit <-coef(fit)[["a"]]b_fit <-coef(fit)[["b"]]future_years <-2024:2030predicted_population <-pearl_curve(future_years -1978, L_fit, a_fit, b_fit)plot(years, population, type ="p", pch =16, xlab ="Year", ylab ="Population", main ="Population Growth Model")lines(years, predict(fit), col ="blue", lwd =2)points(future_years, predicted_population, type ="p", pch =17, col ="red")legend("topleft", legend =c("Observed Data", "Fitted Curve", "Predicted Data"), pch =c(16, NA, 17), lty =c(NA, 1, NA), col =c("black", "blue", "red"))
library(readxl)d1 <-read_excel ("C:/Users/PC/Desktop/1978-2022广东省人口数据.xls", sheet ="Sheet2")year<-as.numeric(d1$年份)populations<-as.numeric(d1$总人口)pearl_curve <-function(t, L, a, b) {return(L / (1+ a *exp(-b * t)))}fit1 <-nls(populations ~pearl_curve(year-1978, L1, a1, b1), start =list(L1 =max(populations)*1.1 , a1 =10, b1 =0.05))summary(fit)
Formula: population ~ pearl_curve(years - 1978, L, a, b)
Parameters:
Estimate Std. Error t value Pr(>|t|)
L 1.506e+05 6.795e+02 221.68 <2e-16 ***
a 5.843e-01 5.820e-03 100.40 <2e-16 ***
b 5.038e-02 1.133e-03 44.46 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 566.4 on 43 degrees of freedom
Number of iterations to convergence: 9
Achieved convergence tolerance: 3.085e-06
L1 <-coef(fit)[["L"]]a1 <-coef(fit)[["a"]]b1 <-coef(fit)[["b"]]future_year <-2024:2030predicted_populations <-pearl_curve(future_years -1978, L_fit, a_fit, b_fit)plot(year, populations, type ="p", pch =16, xlab ="Year", ylab ="Population", main ="Population Growth Model")lines(year, predict(fit1), col ="blue", lwd =2)points(future_year, predicted_populations, type ="p", pch =17, col ="red")legend("topleft", legend =c("Observed Data", "Fitted Curve", "Predicted Data"), pch =c(16, NA, 17), lty =c(NA, 1, NA), col =c("black", "blue", "red"))