Created
June 17, 2016 22:08
-
-
Save jiaweih/90f38e0a8f163f2db4192b9d55767517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"library(lme4)\n", | |
"library(plyr)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"data <- read.csv('/ihme/forecasting/data/fertility/inputs/asfr_ldi_edu.csv')\n", | |
"data <- data[!(data$asfr==0),]\n", | |
"mean.age <- read.csv('/ihme/forecasting/data/fertility/inputs/mean_fertile_age_by_asfr_ratio.csv')\n", | |
"mean.age <- rename(mean.age,c('year' = 'year_id'))\n", | |
"data <- merge.data.frame(x = data,y = mean.age,by = c('location_id','year_id'))\n", | |
"data$sex <- NULL\n", | |
"data$iso3 <- NULL" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table>\n", | |
"<thead><tr><th></th><th scope=col>location_id</th><th scope=col>year_id</th><th scope=col>age_group_id</th><th scope=col>asfr</th><th scope=col>ldi</th><th scope=col>edu</th><th scope=col>mean_fertile_age</th></tr></thead>\n", | |
"<tbody>\n", | |
"\t<tr><th scope=row>1</th><td>101</td><td>1951</td><td>9</td><td>0.204566</td><td>9.36871</td><td>6.826699</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>2</th><td>101</td><td>1951</td><td>13</td><td>0.03092</td><td>9.36871</td><td>6.414093</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>3</th><td>101</td><td>1951</td><td>11</td><td>0.149179</td><td>9.36871</td><td>6.93765</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>4</th><td>101</td><td>1951</td><td>10</td><td>0.2077373</td><td>9.36871</td><td>6.93765</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>5</th><td>101</td><td>1951</td><td>14</td><td>0.0028385</td><td>9.36871</td><td>5.782532</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>6</th><td>101</td><td>1951</td><td>12</td><td>0.0873706</td><td>9.36871</td><td>6.414093</td><td>27.82598</td></tr>\n", | |
"</tbody>\n", | |
"</table>\n" | |
], | |
"text/latex": [ | |
"\\begin{tabular}{r|lllllll}\n", | |
" & location_id & year_id & age_group_id & asfr & ldi & edu & mean_fertile_age\\\\\n", | |
"\\hline\n", | |
"\t1 & 101 & 1951 & 9 & 0.204566 & 9.36871 & 6.826699 & 27.82598\\\\\n", | |
"\t2 & 101 & 1951 & 13 & 0.03092 & 9.36871 & 6.414093 & 27.82598\\\\\n", | |
"\t3 & 101 & 1951 & 11 & 0.149179 & 9.36871 & 6.93765 & 27.82598\\\\\n", | |
"\t4 & 101 & 1951 & 10 & 0.2077373 & 9.36871 & 6.93765 & 27.82598\\\\\n", | |
"\t5 & 101 & 1951 & 14 & 0.0028385 & 9.36871 & 5.782532 & 27.82598\\\\\n", | |
"\t6 & 101 & 1951 & 12 & 0.0873706 & 9.36871 & 6.414093 & 27.82598\\\\\n", | |
"\\end{tabular}\n" | |
], | |
"text/plain": [ | |
" location_id year_id age_group_id asfr ldi edu mean_fertile_age\n", | |
"1 101 1951 9 0.2045660 9.36871 6.826699 27.82598\n", | |
"2 101 1951 13 0.0309200 9.36871 6.414093 27.82598\n", | |
"3 101 1951 11 0.1491790 9.36871 6.937650 27.82598\n", | |
"4 101 1951 10 0.2077373 9.36871 6.937650 27.82598\n", | |
"5 101 1951 14 0.0028385 9.36871 5.782532 27.82598\n", | |
"6 101 1951 12 0.0873706 9.36871 6.414093 27.82598" | |
] | |
}, | |
"execution_count": 74, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"head(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"regression <- function(formula) {\n", | |
" lm_lst <- list()\n", | |
" \n", | |
" for (age_group_id in 8:14) {\n", | |
" \n", | |
" train.data <- data[(data$age_group_id == age_group_id)&(data$year_id < 2010),]\n", | |
" lm_lst[[length(lm_lst) + 1]] <- formula(train.data)\n", | |
" \n", | |
" }\n", | |
" return(lm_lst)\n", | |
"} " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"test.data.lst = list()\n", | |
"for (age_group_id in 8:14){\n", | |
" test.data.lst[[length(test.data.lst)+1]] <- data[(data$age_group_id==age_group_id)&(data$year_id>=2010),]\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"get.rmse <- function(fit.model){\n", | |
" rmse.lst <- list()\n", | |
" for (ix in 1:length(test.data.lst)){\n", | |
" predicted.data <- predict(fit.model[[ix]], newdata = test.data.lst[[ix]])\n", | |
" rmse <- sqrt(mean((test.data.lst[[ix]]$asfr - exp(predicted.data))^2))\n", | |
" rmse.lst[[ix]] <- rmse\n", | |
" }\n", | |
" names(rmse.lst) <- c('8','9','10','11','12','13','14')\n", | |
" return(rmse.lst)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"$$log(ASFR) = \\alpha + \\beta_1 LDI + \\beta_2 \\text{matern_edu}$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.formula <- function(data) {\n", | |
" formula <- lm('log(asfr) ~ ldi + edu',data=data)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 106, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.lm <- regression(ldi.edu.formula)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\n", | |
"Call:\n", | |
"lm(formula = \"log(asfr) ~ ldi + edu\", data = data)\n", | |
"\n", | |
"Residuals:\n", | |
" Min 1Q Median 3Q Max \n", | |
"-3.8936 -0.3611 0.0996 0.4546 1.3359 \n", | |
"\n", | |
"Coefficients:\n", | |
" Estimate Std. Error t value Pr(>|t|) \n", | |
"(Intercept) -0.930911 0.051877 -17.95 <2e-16 ***\n", | |
"ldi -0.123767 0.007246 -17.08 <2e-16 ***\n", | |
"edu -0.146951 0.002615 -56.19 <2e-16 ***\n", | |
"---\n", | |
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n", | |
"\n", | |
"Residual standard error: 0.6599 on 11032 degrees of freedom\n", | |
"Multiple R-squared: 0.4552,\tAdjusted R-squared: 0.4551 \n", | |
"F-statistic: 4608 on 2 and 11032 DF, p-value: < 2.2e-16\n" | |
] | |
}, | |
"execution_count": 107, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"summary(ldi.edu.lm[[1]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 108, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table>\n", | |
"<thead><tr><th></th><th scope=col>location_id</th><th scope=col>year_id</th><th scope=col>age_group_id</th><th scope=col>asfr</th><th scope=col>ldi</th><th scope=col>edu</th><th scope=col>mean_fertile_age</th></tr></thead>\n", | |
"<tbody>\n", | |
"\t<tr><th scope=row>1</th><td>101</td><td>1951</td><td>9</td><td>0.204566</td><td>9.36871</td><td>6.826699</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>2</th><td>101</td><td>1951</td><td>13</td><td>0.03092</td><td>9.36871</td><td>6.414093</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>3</th><td>101</td><td>1951</td><td>11</td><td>0.149179</td><td>9.36871</td><td>6.93765</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>4</th><td>101</td><td>1951</td><td>10</td><td>0.2077373</td><td>9.36871</td><td>6.93765</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>5</th><td>101</td><td>1951</td><td>14</td><td>0.0028385</td><td>9.36871</td><td>5.782532</td><td>27.82598</td></tr>\n", | |
"\t<tr><th scope=row>6</th><td>101</td><td>1951</td><td>12</td><td>0.0873706</td><td>9.36871</td><td>6.414093</td><td>27.82598</td></tr>\n", | |
"</tbody>\n", | |
"</table>\n" | |
], | |
"text/latex": [ | |
"\\begin{tabular}{r|lllllll}\n", | |
" & location_id & year_id & age_group_id & asfr & ldi & edu & mean_fertile_age\\\\\n", | |
"\\hline\n", | |
"\t1 & 101 & 1951 & 9 & 0.204566 & 9.36871 & 6.826699 & 27.82598\\\\\n", | |
"\t2 & 101 & 1951 & 13 & 0.03092 & 9.36871 & 6.414093 & 27.82598\\\\\n", | |
"\t3 & 101 & 1951 & 11 & 0.149179 & 9.36871 & 6.93765 & 27.82598\\\\\n", | |
"\t4 & 101 & 1951 & 10 & 0.2077373 & 9.36871 & 6.93765 & 27.82598\\\\\n", | |
"\t5 & 101 & 1951 & 14 & 0.0028385 & 9.36871 & 5.782532 & 27.82598\\\\\n", | |
"\t6 & 101 & 1951 & 12 & 0.0873706 & 9.36871 & 6.414093 & 27.82598\\\\\n", | |
"\\end{tabular}\n" | |
], | |
"text/plain": [ | |
" location_id year_id age_group_id asfr ldi edu mean_fertile_age\n", | |
"1 101 1951 9 0.2045660 9.36871 6.826699 27.82598\n", | |
"2 101 1951 13 0.0309200 9.36871 6.414093 27.82598\n", | |
"3 101 1951 11 0.1491790 9.36871 6.937650 27.82598\n", | |
"4 101 1951 10 0.2077373 9.36871 6.937650 27.82598\n", | |
"5 101 1951 14 0.0028385 9.36871 5.782532 27.82598\n", | |
"6 101 1951 12 0.0873706 9.36871 6.414093 27.82598" | |
] | |
}, | |
"execution_count": 108, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"head(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 109, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$8</dt>\n", | |
"\t\t<dd>0.0319077678258407</dd>\n", | |
"\t<dt>$9</dt>\n", | |
"\t\t<dd>0.0447523546332716</dd>\n", | |
"\t<dt>$10</dt>\n", | |
"\t\t<dd>0.0449359589594723</dd>\n", | |
"\t<dt>$11</dt>\n", | |
"\t\t<dd>0.0483877794978591</dd>\n", | |
"\t<dt>$12</dt>\n", | |
"\t\t<dd>0.03836958085842</dd>\n", | |
"\t<dt>$13</dt>\n", | |
"\t\t<dd>0.0189623726319228</dd>\n", | |
"\t<dt>$14</dt>\n", | |
"\t\t<dd>0.00750321493163991</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$8] 0.0319077678258407\n", | |
"\\item[\\$9] 0.0447523546332716\n", | |
"\\item[\\$10] 0.0449359589594723\n", | |
"\\item[\\$11] 0.0483877794978591\n", | |
"\\item[\\$12] 0.03836958085842\n", | |
"\\item[\\$13] 0.0189623726319228\n", | |
"\\item[\\$14] 0.00750321493163991\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$8\n", | |
": 0.0319077678258407\n", | |
"$9\n", | |
": 0.0447523546332716\n", | |
"$10\n", | |
": 0.0449359589594723\n", | |
"$11\n", | |
": 0.0483877794978591\n", | |
"$12\n", | |
": 0.03836958085842\n", | |
"$13\n", | |
": 0.0189623726319228\n", | |
"$14\n", | |
": 0.00750321493163991\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$`8`\n", | |
"[1] 0.03190777\n", | |
"\n", | |
"$`9`\n", | |
"[1] 0.04475235\n", | |
"\n", | |
"$`10`\n", | |
"[1] 0.04493596\n", | |
"\n", | |
"$`11`\n", | |
"[1] 0.04838778\n", | |
"\n", | |
"$`12`\n", | |
"[1] 0.03836958\n", | |
"\n", | |
"$`13`\n", | |
"[1] 0.01896237\n", | |
"\n", | |
"$`14`\n", | |
"[1] 0.007503215\n" | |
] | |
}, | |
"execution_count": 109, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rmse.lst <- get.rmse(ldi.edu.lm)\n", | |
"rmse.lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 110, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"0.0335455756197752" | |
], | |
"text/latex": [ | |
"0.0335455756197752" | |
], | |
"text/markdown": [ | |
"0.0335455756197752" | |
], | |
"text/plain": [ | |
"[1] 0.03354558" | |
] | |
}, | |
"execution_count": 110, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mean(unlist(rmse.lst))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"$$log(ASFR) = \\alpha_0 + \\alpha_{country} + \\beta_1 LDI + \\beta_2 \\text{matern_edu}$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 111, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.country.formula <- function(data) {\n", | |
" formula <- lmer('log(asfr) ~ 1 + ldi + edu + (1 | location_id)',data=data)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 112, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.country.lmer <- regression(ldi.edu.country.formula)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 113, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Linear mixed model fit by REML ['lmerMod']\n", | |
"Formula: log(asfr) ~ 1 + ldi + edu + (1 | location_id)\n", | |
" Data: data\n", | |
"\n", | |
"REML criterion at convergence: 5070.1\n", | |
"\n", | |
"Scaled residuals: \n", | |
" Min 1Q Median 3Q Max \n", | |
"-6.0732 -0.4762 0.0378 0.5067 4.2781 \n", | |
"\n", | |
"Random effects:\n", | |
" Groups Name Variance Std.Dev.\n", | |
" location_id (Intercept) 0.37258 0.6104 \n", | |
" Residual 0.08416 0.2901 \n", | |
"Number of obs: 11035, groups: location_id, 188\n", | |
"\n", | |
"Fixed effects:\n", | |
" Estimate Std. Error t value\n", | |
"(Intercept) -1.787888 0.083201 -21.49\n", | |
"ldi -0.028082 0.009277 -3.03\n", | |
"edu -0.136874 0.001925 -71.12\n", | |
"\n", | |
"Correlation of Fixed Effects:\n", | |
" (Intr) ldi \n", | |
"ldi -0.839 \n", | |
"edu 0.540 -0.719" | |
] | |
}, | |
"execution_count": 113, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"summary(ldi.edu.country.lmer[[1]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$8</dt>\n", | |
"\t\t<dd>0.0155930273474457</dd>\n", | |
"\t<dt>$9</dt>\n", | |
"\t\t<dd>0.0326650479784671</dd>\n", | |
"\t<dt>$10</dt>\n", | |
"\t\t<dd>0.0322068909498717</dd>\n", | |
"\t<dt>$11</dt>\n", | |
"\t\t<dd>0.0350849346478507</dd>\n", | |
"\t<dt>$12</dt>\n", | |
"\t\t<dd>0.0274731757048421</dd>\n", | |
"\t<dt>$13</dt>\n", | |
"\t\t<dd>0.0133370642204525</dd>\n", | |
"\t<dt>$14</dt>\n", | |
"\t\t<dd>0.00680279344915293</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$8] 0.0155930273474457\n", | |
"\\item[\\$9] 0.0326650479784671\n", | |
"\\item[\\$10] 0.0322068909498717\n", | |
"\\item[\\$11] 0.0350849346478507\n", | |
"\\item[\\$12] 0.0274731757048421\n", | |
"\\item[\\$13] 0.0133370642204525\n", | |
"\\item[\\$14] 0.00680279344915293\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$8\n", | |
": 0.0155930273474457\n", | |
"$9\n", | |
": 0.0326650479784671\n", | |
"$10\n", | |
": 0.0322068909498717\n", | |
"$11\n", | |
": 0.0350849346478507\n", | |
"$12\n", | |
": 0.0274731757048421\n", | |
"$13\n", | |
": 0.0133370642204525\n", | |
"$14\n", | |
": 0.00680279344915293\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$`8`\n", | |
"[1] 0.01559303\n", | |
"\n", | |
"$`9`\n", | |
"[1] 0.03266505\n", | |
"\n", | |
"$`10`\n", | |
"[1] 0.03220689\n", | |
"\n", | |
"$`11`\n", | |
"[1] 0.03508493\n", | |
"\n", | |
"$`12`\n", | |
"[1] 0.02747318\n", | |
"\n", | |
"$`13`\n", | |
"[1] 0.01333706\n", | |
"\n", | |
"$`14`\n", | |
"[1] 0.006802793\n" | |
] | |
}, | |
"execution_count": 114, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rmse.lst <- get.rmse(ldi.edu.country.lmer)\n", | |
"rmse.lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 115, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"0.0233089906140118" | |
], | |
"text/latex": [ | |
"0.0233089906140118" | |
], | |
"text/markdown": [ | |
"0.0233089906140118" | |
], | |
"text/plain": [ | |
"[1] 0.02330899" | |
] | |
}, | |
"execution_count": 115, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mean(unlist(rmse.lst))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"$$log(ASFR) = \\alpha + \\beta_1 LDI + \\beta_2 \\text{matern_edu} + \\beta_3 \\text{mean_fertile_age}$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 116, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.mean.age.formula <- function(data) {\n", | |
" formula <- lm('log(asfr) ~ ldi + edu + mean_fertile_age',data=data)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 117, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.mean.age.lm <- regression(ldi.edu.mean.age.formula)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 118, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\n", | |
"Call:\n", | |
"lm(formula = \"log(asfr) ~ ldi + edu + mean_fertile_age\", data = data)\n", | |
"\n", | |
"Residuals:\n", | |
" Min 1Q Median 3Q Max \n", | |
"-3.7874 -0.3464 0.0897 0.4321 1.2553 \n", | |
"\n", | |
"Coefficients:\n", | |
" Estimate Std. Error t value Pr(>|t|) \n", | |
"(Intercept) 3.996711 0.136934 29.19 <2e-16 ***\n", | |
"ldi -0.143271 0.006822 -21.00 <2e-16 ***\n", | |
"edu -0.170721 0.002532 -67.43 <2e-16 ***\n", | |
"mean_fertile_age -0.163947 0.004258 -38.50 <2e-16 ***\n", | |
"---\n", | |
"Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n", | |
"\n", | |
"Residual standard error: 0.6196 on 11031 degrees of freedom\n", | |
"Multiple R-squared: 0.5197,\tAdjusted R-squared: 0.5196 \n", | |
"F-statistic: 3979 on 3 and 11031 DF, p-value: < 2.2e-16\n" | |
] | |
}, | |
"execution_count": 118, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"summary(ldi.edu.mean.age.lm[[1]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 119, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$8</dt>\n", | |
"\t\t<dd>0.0302085126163373</dd>\n", | |
"\t<dt>$9</dt>\n", | |
"\t\t<dd>0.0422509888596671</dd>\n", | |
"\t<dt>$10</dt>\n", | |
"\t\t<dd>0.045020229393368</dd>\n", | |
"\t<dt>$11</dt>\n", | |
"\t\t<dd>0.03879694423414</dd>\n", | |
"\t<dt>$12</dt>\n", | |
"\t\t<dd>0.0335811193661152</dd>\n", | |
"\t<dt>$13</dt>\n", | |
"\t\t<dd>0.01904128600931</dd>\n", | |
"\t<dt>$14</dt>\n", | |
"\t\t<dd>0.00816808104230157</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$8] 0.0302085126163373\n", | |
"\\item[\\$9] 0.0422509888596671\n", | |
"\\item[\\$10] 0.045020229393368\n", | |
"\\item[\\$11] 0.03879694423414\n", | |
"\\item[\\$12] 0.0335811193661152\n", | |
"\\item[\\$13] 0.01904128600931\n", | |
"\\item[\\$14] 0.00816808104230157\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$8\n", | |
": 0.0302085126163373\n", | |
"$9\n", | |
": 0.0422509888596671\n", | |
"$10\n", | |
": 0.045020229393368\n", | |
"$11\n", | |
": 0.03879694423414\n", | |
"$12\n", | |
": 0.0335811193661152\n", | |
"$13\n", | |
": 0.01904128600931\n", | |
"$14\n", | |
": 0.00816808104230157\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$`8`\n", | |
"[1] 0.03020851\n", | |
"\n", | |
"$`9`\n", | |
"[1] 0.04225099\n", | |
"\n", | |
"$`10`\n", | |
"[1] 0.04502023\n", | |
"\n", | |
"$`11`\n", | |
"[1] 0.03879694\n", | |
"\n", | |
"$`12`\n", | |
"[1] 0.03358112\n", | |
"\n", | |
"$`13`\n", | |
"[1] 0.01904129\n", | |
"\n", | |
"$`14`\n", | |
"[1] 0.008168081\n" | |
] | |
}, | |
"execution_count": 119, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rmse.lst <- get.rmse(ldi.edu.mean.age.lm)\n", | |
"rmse.lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 120, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"0.0310095945030342" | |
], | |
"text/latex": [ | |
"0.0310095945030342" | |
], | |
"text/markdown": [ | |
"0.0310095945030342" | |
], | |
"text/plain": [ | |
"[1] 0.03100959" | |
] | |
}, | |
"execution_count": 120, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mean(unlist(rmse.lst))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"$$log(ASFR) = \\alpha_0 + \\alpha_{country} + \\beta_1 LDI + \\beta_2 \\text{matern_edu} + \\beta_3 \\text{mean_fertile_age}$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 121, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.country.mean.age.formula <- function(data) {\n", | |
" formula <- lmer('log(asfr) ~ 1 + ldi + edu + mean_fertile_age + (1 | location_id)',data=data)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"ldi.edu.country.mean.age.lmer <- regression(ldi.edu.country.mean.age.formula)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Linear mixed model fit by REML ['lmerMod']\n", | |
"Formula: log(asfr) ~ 1 + ldi + edu + mean_fertile_age + (1 | location_id)\n", | |
" Data: data\n", | |
"\n", | |
"REML criterion at convergence: 1514\n", | |
"\n", | |
"Scaled residuals: \n", | |
" Min 1Q Median 3Q Max \n", | |
"-6.7691 -0.4684 0.0434 0.4975 5.0650 \n", | |
"\n", | |
"Random effects:\n", | |
" Groups Name Variance Std.Dev.\n", | |
" location_id (Intercept) 0.36338 0.6028 \n", | |
" Residual 0.06061 0.2462 \n", | |
"Number of obs: 11035, groups: location_id, 188\n", | |
"\n", | |
"Fixed effects:\n", | |
" Estimate Std. Error t value\n", | |
"(Intercept) 3.829279 0.114139 33.55\n", | |
"ldi -0.046762 0.007914 -5.91\n", | |
"edu -0.147587 0.001644 -89.76\n", | |
"mean_fertile_age -0.191212 0.002944 -64.96\n", | |
"\n", | |
"Correlation of Fixed Effects:\n", | |
" (Intr) ldi edu \n", | |
"ldi -0.551 \n", | |
"edu 0.259 -0.712 \n", | |
"mean_frtl_g -0.759 0.039 0.099" | |
] | |
}, | |
"execution_count": 123, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"summary(ldi.edu.country.mean.age.lmer[[1]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$8</dt>\n", | |
"\t\t<dd>0.0147251713573406</dd>\n", | |
"\t<dt>$9</dt>\n", | |
"\t\t<dd>0.0304239008189843</dd>\n", | |
"\t<dt>$10</dt>\n", | |
"\t\t<dd>0.031845038874304</dd>\n", | |
"\t<dt>$11</dt>\n", | |
"\t\t<dd>0.0261615890885939</dd>\n", | |
"\t<dt>$12</dt>\n", | |
"\t\t<dd>0.0213119968286809</dd>\n", | |
"\t<dt>$13</dt>\n", | |
"\t\t<dd>0.0115559978335628</dd>\n", | |
"\t<dt>$14</dt>\n", | |
"\t\t<dd>0.00576590154839973</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$8] 0.0147251713573406\n", | |
"\\item[\\$9] 0.0304239008189843\n", | |
"\\item[\\$10] 0.031845038874304\n", | |
"\\item[\\$11] 0.0261615890885939\n", | |
"\\item[\\$12] 0.0213119968286809\n", | |
"\\item[\\$13] 0.0115559978335628\n", | |
"\\item[\\$14] 0.00576590154839973\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$8\n", | |
": 0.0147251713573406\n", | |
"$9\n", | |
": 0.0304239008189843\n", | |
"$10\n", | |
": 0.031845038874304\n", | |
"$11\n", | |
": 0.0261615890885939\n", | |
"$12\n", | |
": 0.0213119968286809\n", | |
"$13\n", | |
": 0.0115559978335628\n", | |
"$14\n", | |
": 0.00576590154839973\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$`8`\n", | |
"[1] 0.01472517\n", | |
"\n", | |
"$`9`\n", | |
"[1] 0.0304239\n", | |
"\n", | |
"$`10`\n", | |
"[1] 0.03184504\n", | |
"\n", | |
"$`11`\n", | |
"[1] 0.02616159\n", | |
"\n", | |
"$`12`\n", | |
"[1] 0.021312\n", | |
"\n", | |
"$`13`\n", | |
"[1] 0.011556\n", | |
"\n", | |
"$`14`\n", | |
"[1] 0.005765902\n" | |
] | |
}, | |
"execution_count": 124, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rmse.lst <- get.rmse(ldi.edu.country.mean.age.lmer)\n", | |
"rmse.lst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"0.0202556566214095" | |
], | |
"text/latex": [ | |
"0.0202556566214095" | |
], | |
"text/markdown": [ | |
"0.0202556566214095" | |
], | |
"text/plain": [ | |
"[1] 0.02025566" | |
] | |
}, | |
"execution_count": 125, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mean(unlist(rmse.lst))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "R", | |
"language": "R", | |
"name": "ir" | |
}, | |
"language_info": { | |
"codemirror_mode": "r", | |
"file_extension": ".r", | |
"mimetype": "text/x-r-source", | |
"name": "R", | |
"pygments_lexer": "r", | |
"version": "3.2.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment