Skip to content

Instantly share code, notes, and snippets.

@vincenttzc
Created October 1, 2017 03:17
Show Gist options
  • Save vincenttzc/784259b9b6b25071006672b0f26313b3 to your computer and use it in GitHub Desktop.
Save vincenttzc/784259b9b6b25071006672b0f26313b3 to your computer and use it in GitHub Desktop.
ECON207 Assignment 2
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# ECON207 Assignment 2 (Part 2)\n",
"\n",
"## Tan Zhi Chong"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"Attaching package: 'dplyr'\n",
"\n",
"The following objects are masked from 'package:stats':\n",
"\n",
" filter, lag\n",
"\n",
"The following objects are masked from 'package:base':\n",
"\n",
" intersect, setdiff, setequal, union\n",
"\n"
]
}
],
"source": [
"## Prepare library for data preparation and data visualisation\n",
"\n",
"library(ggplot2)\n",
"library(dplyr)\n",
"\n",
"## Set the working directory to obtain the data\n",
"\n",
"setwd(\"C:\\\\Users\\\\vince\\\\Documents\\\\R Scripts\\\\R data\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>'data'</li>\n",
"\t<li>'desc'</li>\n",
"\t<li>'self'</li>\n",
"</ol>\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'data'\n",
"\\item 'desc'\n",
"\\item 'self'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'data'\n",
"2. 'desc'\n",
"3. 'self'\n",
"\n",
"\n"
],
"text/plain": [
"[1] \"data\" \"desc\" \"self\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\n",
"## Load data using read.csv function\n",
"\n",
"x <- load(file = \"sleep75.RData\")\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<thead><tr><th scope=col>age</th><th scope=col>black</th><th scope=col>case</th><th scope=col>clerical</th><th scope=col>construc</th><th scope=col>educ</th><th scope=col>earns74</th><th scope=col>gdhlth</th><th scope=col>inlf</th><th scope=col>leis1</th><th scope=col>...</th><th scope=col>spwrk75</th><th scope=col>totwrk</th><th scope=col>union</th><th scope=col>worknrm</th><th scope=col>workscnd</th><th scope=col>exper</th><th scope=col>yngkid</th><th scope=col>yrsmarr</th><th scope=col>hrwage</th><th scope=col>agesq</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td>32 </td><td>0 </td><td>1 </td><td>0 </td><td>0 </td><td>12 </td><td> 0 </td><td>0 </td><td>1 </td><td>3529 </td><td>... </td><td>0 </td><td>3438 </td><td>0 </td><td>3438 </td><td> 0 </td><td>14 </td><td>0 </td><td>13 </td><td> 7.070004</td><td>1024 </td></tr>\n",
"\t<tr><td>31 </td><td>0 </td><td>2 </td><td>0 </td><td>0 </td><td>14 </td><td> 9500 </td><td>1 </td><td>1 </td><td>2140 </td><td>... </td><td>0 </td><td>5020 </td><td>0 </td><td>5020 </td><td> 0 </td><td>11 </td><td>0 </td><td> 0 </td><td> 1.429999</td><td> 961 </td></tr>\n",
"\t<tr><td>44 </td><td>0 </td><td>3 </td><td>0 </td><td>0 </td><td>17 </td><td>42500 </td><td>1 </td><td>1 </td><td>4595 </td><td>... </td><td>1 </td><td>2815 </td><td>0 </td><td>2815 </td><td> 0 </td><td>21 </td><td>0 </td><td> 0 </td><td>20.529997</td><td>1936 </td></tr>\n",
"\t<tr><td>30 </td><td>0 </td><td>4 </td><td>0 </td><td>0 </td><td>12 </td><td>42500 </td><td>1 </td><td>1 </td><td>3211 </td><td>... </td><td>1 </td><td>3786 </td><td>0 </td><td>3786 </td><td> 0 </td><td>12 </td><td>0 </td><td>12 </td><td> 9.619998</td><td> 900 </td></tr>\n",
"\t<tr><td>64 </td><td>0 </td><td>5 </td><td>0 </td><td>0 </td><td>14 </td><td> 2500 </td><td>1 </td><td>1 </td><td>4052 </td><td>... </td><td>1 </td><td>2580 </td><td>0 </td><td>2580 </td><td> 0 </td><td>44 </td><td>0 </td><td>33 </td><td> 2.750000</td><td>4096 </td></tr>\n",
"\t<tr><td>41 </td><td>0 </td><td>6 </td><td>0 </td><td>0 </td><td>12 </td><td> 0 </td><td>1 </td><td>1 </td><td>4812 </td><td>... </td><td>0 </td><td>1205 </td><td>0 </td><td> 0 </td><td>1205 </td><td>23 </td><td>0 </td><td>23 </td><td>19.249998</td><td>1681 </td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\begin{tabular}{r|llllllllllllllllllllllllllllllllll}\n",
" age & black & case & clerical & construc & educ & earns74 & gdhlth & inlf & leis1 & ... & spwrk75 & totwrk & union & worknrm & workscnd & exper & yngkid & yrsmarr & hrwage & agesq\\\\\n",
"\\hline\n",
"\t 32 & 0 & 1 & 0 & 0 & 12 & 0 & 0 & 1 & 3529 & ... & 0 & 3438 & 0 & 3438 & 0 & 14 & 0 & 13 & 7.070004 & 1024 \\\\\n",
"\t 31 & 0 & 2 & 0 & 0 & 14 & 9500 & 1 & 1 & 2140 & ... & 0 & 5020 & 0 & 5020 & 0 & 11 & 0 & 0 & 1.429999 & 961 \\\\\n",
"\t 44 & 0 & 3 & 0 & 0 & 17 & 42500 & 1 & 1 & 4595 & ... & 1 & 2815 & 0 & 2815 & 0 & 21 & 0 & 0 & 20.529997 & 1936 \\\\\n",
"\t 30 & 0 & 4 & 0 & 0 & 12 & 42500 & 1 & 1 & 3211 & ... & 1 & 3786 & 0 & 3786 & 0 & 12 & 0 & 12 & 9.619998 & 900 \\\\\n",
"\t 64 & 0 & 5 & 0 & 0 & 14 & 2500 & 1 & 1 & 4052 & ... & 1 & 2580 & 0 & 2580 & 0 & 44 & 0 & 33 & 2.750000 & 4096 \\\\\n",
"\t 41 & 0 & 6 & 0 & 0 & 12 & 0 & 1 & 1 & 4812 & ... & 0 & 1205 & 0 & 0 & 1205 & 23 & 0 & 23 & 19.249998 & 1681 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"age | black | case | clerical | construc | educ | earns74 | gdhlth | inlf | leis1 | ... | spwrk75 | totwrk | union | worknrm | workscnd | exper | yngkid | yrsmarr | hrwage | agesq | \n",
"|---|---|---|---|---|---|\n",
"| 32 | 0 | 1 | 0 | 0 | 12 | 0 | 0 | 1 | 3529 | ... | 0 | 3438 | 0 | 3438 | 0 | 14 | 0 | 13 | 7.070004 | 1024 | \n",
"| 31 | 0 | 2 | 0 | 0 | 14 | 9500 | 1 | 1 | 2140 | ... | 0 | 5020 | 0 | 5020 | 0 | 11 | 0 | 0 | 1.429999 | 961 | \n",
"| 44 | 0 | 3 | 0 | 0 | 17 | 42500 | 1 | 1 | 4595 | ... | 1 | 2815 | 0 | 2815 | 0 | 21 | 0 | 0 | 20.529997 | 1936 | \n",
"| 30 | 0 | 4 | 0 | 0 | 12 | 42500 | 1 | 1 | 3211 | ... | 1 | 3786 | 0 | 3786 | 0 | 12 | 0 | 12 | 9.619998 | 900 | \n",
"| 64 | 0 | 5 | 0 | 0 | 14 | 2500 | 1 | 1 | 4052 | ... | 1 | 2580 | 0 | 2580 | 0 | 44 | 0 | 33 | 2.750000 | 4096 | \n",
"| 41 | 0 | 6 | 0 | 0 | 12 | 0 | 1 | 1 | 4812 | ... | 0 | 1205 | 0 | 0 | 1205 | 23 | 0 | 23 | 19.249998 | 1681 | \n",
"\n",
"\n"
],
"text/plain": [
" age black case clerical construc educ earns74 gdhlth inlf leis1 ... spwrk75\n",
"1 32 0 1 0 0 12 0 0 1 3529 ... 0 \n",
"2 31 0 2 0 0 14 9500 1 1 2140 ... 0 \n",
"3 44 0 3 0 0 17 42500 1 1 4595 ... 1 \n",
"4 30 0 4 0 0 12 42500 1 1 3211 ... 1 \n",
"5 64 0 5 0 0 14 2500 1 1 4052 ... 1 \n",
"6 41 0 6 0 0 12 0 1 1 4812 ... 0 \n",
" totwrk union worknrm workscnd exper yngkid yrsmarr hrwage agesq\n",
"1 3438 0 3438 0 14 0 13 7.070004 1024 \n",
"2 5020 0 5020 0 11 0 0 1.429999 961 \n",
"3 2815 0 2815 0 21 0 0 20.529997 1936 \n",
"4 3786 0 3786 0 12 0 12 9.619998 900 \n",
"5 2580 0 2580 0 44 0 33 2.750000 4096 \n",
"6 1205 0 0 1205 23 0 23 19.249998 1681 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sleep75 <- data\n",
"head(sleep75)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<thead><tr><th scope=col>variable</th><th scope=col>label</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td>age </td><td>in years </td></tr>\n",
"\t<tr><td>black </td><td>=1 if black </td></tr>\n",
"\t<tr><td>case </td><td>identifier </td></tr>\n",
"\t<tr><td>clerical </td><td>=1 if clerical worker </td></tr>\n",
"\t<tr><td>construc </td><td>=1 if construction worker </td></tr>\n",
"\t<tr><td>educ </td><td>years of schooling </td></tr>\n",
"\t<tr><td>earns74 </td><td>total earnings, 1974 </td></tr>\n",
"\t<tr><td>gdhlth </td><td>=1 if in good or excel. health</td></tr>\n",
"\t<tr><td>inlf </td><td>=1 if in labor force </td></tr>\n",
"\t<tr><td>leis1 </td><td>sleep - totwrk </td></tr>\n",
"\t<tr><td>leis2 </td><td>slpnaps - totwrk </td></tr>\n",
"\t<tr><td>leis3 </td><td>rlxall - totwrk </td></tr>\n",
"\t<tr><td>smsa </td><td>=1 if live in smsa </td></tr>\n",
"\t<tr><td>lhrwage </td><td>log hourly wage </td></tr>\n",
"\t<tr><td>lothinc </td><td>log othinc, unless othinc &lt; 0 </td></tr>\n",
"\t<tr><td>male </td><td>=1 if male </td></tr>\n",
"\t<tr><td>marr </td><td>=1 if married </td></tr>\n",
"\t<tr><td>prot </td><td>=1 if Protestant </td></tr>\n",
"\t<tr><td>rlxall </td><td>slpnaps + personal activs </td></tr>\n",
"\t<tr><td>selfe </td><td>=1 if self employed </td></tr>\n",
"\t<tr><td>sleep </td><td>mins sleep at night, per wk </td></tr>\n",
"\t<tr><td>slpnaps </td><td>minutes sleep, inc. naps </td></tr>\n",
"\t<tr><td>south </td><td>=1 if live in south </td></tr>\n",
"\t<tr><td>spsepay </td><td>spousal wage income </td></tr>\n",
"\t<tr><td>spwrk75 </td><td>=1 if spouse works </td></tr>\n",
"\t<tr><td>totwrk </td><td>mins worked per week </td></tr>\n",
"\t<tr><td>union </td><td>=1 if belong to union </td></tr>\n",
"\t<tr><td>worknrm </td><td>mins work main job </td></tr>\n",
"\t<tr><td>workscnd </td><td>mins work second job </td></tr>\n",
"\t<tr><td>exper </td><td>age - educ - 6 </td></tr>\n",
"\t<tr><td><span style=white-space:pre-wrap>yngkid </span> </td><td><span style=white-space:pre-wrap>=1 if children &lt; 3 present </span></td></tr>\n",
"\t<tr><td>yrsmarr </td><td>years married </td></tr>\n",
"\t<tr><td>hrwage </td><td>hourly wage </td></tr>\n",
"\t<tr><td>agesq </td><td>age^2 </td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\begin{tabular}{r|ll}\n",
" variable & label\\\\\n",
"\\hline\n",
"\t age & in years \\\\\n",
"\t black & =1 if black \\\\\n",
"\t case & identifier \\\\\n",
"\t clerical & =1 if clerical worker \\\\\n",
"\t construc & =1 if construction worker \\\\\n",
"\t educ & years of schooling \\\\\n",
"\t earns74 & total earnings, 1974 \\\\\n",
"\t gdhlth & =1 if in good or excel. health\\\\\n",
"\t inlf & =1 if in labor force \\\\\n",
"\t leis1 & sleep - totwrk \\\\\n",
"\t leis2 & slpnaps - totwrk \\\\\n",
"\t leis3 & rlxall - totwrk \\\\\n",
"\t smsa & =1 if live in smsa \\\\\n",
"\t lhrwage & log hourly wage \\\\\n",
"\t lothinc & log othinc, unless othinc < 0 \\\\\n",
"\t male & =1 if male \\\\\n",
"\t marr & =1 if married \\\\\n",
"\t prot & =1 if Protestant \\\\\n",
"\t rlxall & slpnaps + personal activs \\\\\n",
"\t selfe & =1 if self employed \\\\\n",
"\t sleep & mins sleep at night, per wk \\\\\n",
"\t slpnaps & minutes sleep, inc. naps \\\\\n",
"\t south & =1 if live in south \\\\\n",
"\t spsepay & spousal wage income \\\\\n",
"\t spwrk75 & =1 if spouse works \\\\\n",
"\t totwrk & mins worked per week \\\\\n",
"\t union & =1 if belong to union \\\\\n",
"\t worknrm & mins work main job \\\\\n",
"\t workscnd & mins work second job \\\\\n",
"\t exper & age - educ - 6 \\\\\n",
"\t yngkid & =1 if children < 3 present \\\\\n",
"\t yrsmarr & years married \\\\\n",
"\t hrwage & hourly wage \\\\\n",
"\t agesq & age\\textasciicircum{}2 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"variable | label | \n",
"|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
"| age | in years | \n",
"| black | =1 if black | \n",
"| case | identifier | \n",
"| clerical | =1 if clerical worker | \n",
"| construc | =1 if construction worker | \n",
"| educ | years of schooling | \n",
"| earns74 | total earnings, 1974 | \n",
"| gdhlth | =1 if in good or excel. health | \n",
"| inlf | =1 if in labor force | \n",
"| leis1 | sleep - totwrk | \n",
"| leis2 | slpnaps - totwrk | \n",
"| leis3 | rlxall - totwrk | \n",
"| smsa | =1 if live in smsa | \n",
"| lhrwage | log hourly wage | \n",
"| lothinc | log othinc, unless othinc < 0 | \n",
"| male | =1 if male | \n",
"| marr | =1 if married | \n",
"| prot | =1 if Protestant | \n",
"| rlxall | slpnaps + personal activs | \n",
"| selfe | =1 if self employed | \n",
"| sleep | mins sleep at night, per wk | \n",
"| slpnaps | minutes sleep, inc. naps | \n",
"| south | =1 if live in south | \n",
"| spsepay | spousal wage income | \n",
"| spwrk75 | =1 if spouse works | \n",
"| totwrk | mins worked per week | \n",
"| union | =1 if belong to union | \n",
"| worknrm | mins work main job | \n",
"| workscnd | mins work second job | \n",
"| exper | age - educ - 6 | \n",
"| yngkid | =1 if children < 3 present | \n",
"| yrsmarr | years married | \n",
"| hrwage | hourly wage | \n",
"| agesq | age^2 | \n",
"\n",
"\n"
],
"text/plain": [
" variable label \n",
"1 age in years \n",
"2 black =1 if black \n",
"3 case identifier \n",
"4 clerical =1 if clerical worker \n",
"5 construc =1 if construction worker \n",
"6 educ years of schooling \n",
"7 earns74 total earnings, 1974 \n",
"8 gdhlth =1 if in good or excel. health\n",
"9 inlf =1 if in labor force \n",
"10 leis1 sleep - totwrk \n",
"11 leis2 slpnaps - totwrk \n",
"12 leis3 rlxall - totwrk \n",
"13 smsa =1 if live in smsa \n",
"14 lhrwage log hourly wage \n",
"15 lothinc log othinc, unless othinc < 0 \n",
"16 male =1 if male \n",
"17 marr =1 if married \n",
"18 prot =1 if Protestant \n",
"19 rlxall slpnaps + personal activs \n",
"20 selfe =1 if self employed \n",
"21 sleep mins sleep at night, per wk \n",
"22 slpnaps minutes sleep, inc. naps \n",
"23 south =1 if live in south \n",
"24 spsepay spousal wage income \n",
"25 spwrk75 =1 if spouse works \n",
"26 totwrk mins worked per week \n",
"27 union =1 if belong to union \n",
"28 worknrm mins work main job \n",
"29 workscnd mins work second job \n",
"30 exper age - educ - 6 \n",
"31 yngkid =1 if children < 3 present \n",
"32 yrsmarr years married \n",
"33 hrwage hourly wage \n",
"34 agesq age^2 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"desc"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<thead><tr><th scope=col>variable</th><th scope=col>label</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td>age </td><td>in years </td></tr>\n",
"\t<tr><td>educ </td><td>years of schooling </td></tr>\n",
"\t<tr><td>sleep </td><td>mins sleep at night, per wk</td></tr>\n",
"\t<tr><td>totwrk </td><td>mins worked per week </td></tr>\n",
"\t<tr><td>yngkid </td><td>=1 if children &lt; 3 present </td></tr>\n",
"\t<tr><td>agesq </td><td>age^2 </td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\begin{tabular}{r|ll}\n",
" variable & label\\\\\n",
"\\hline\n",
"\t age & in years \\\\\n",
"\t educ & years of schooling \\\\\n",
"\t sleep & mins sleep at night, per wk\\\\\n",
"\t totwrk & mins worked per week \\\\\n",
"\t yngkid & =1 if children < 3 present \\\\\n",
"\t agesq & age\\textasciicircum{}2 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"variable | label | \n",
"|---|---|---|---|---|---|\n",
"| age | in years | \n",
"| educ | years of schooling | \n",
"| sleep | mins sleep at night, per wk | \n",
"| totwrk | mins worked per week | \n",
"| yngkid | =1 if children < 3 present | \n",
"| agesq | age^2 | \n",
"\n",
"\n"
],
"text/plain": [
" variable label \n",
"1 age in years \n",
"2 educ years of schooling \n",
"3 sleep mins sleep at night, per wk\n",
"4 totwrk mins worked per week \n",
"5 yngkid =1 if children < 3 present \n",
"6 agesq age^2 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"filter(desc, variable %in% c(\"sleep\",\"totwrk\",\"educ\",\"age\",\"agesq\",\"yngkid\"))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = sleep ~ totwrk + educ + age + agesq + yngkid, data = sleep75)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-2427.76 -241.36 16.97 252.67 1308.13 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 3825.37490 240.25848 15.922 <2e-16 ***\n",
"totwrk -0.14605 0.01688 -8.652 <2e-16 ***\n",
"educ -11.13772 5.89017 -1.891 0.0591 . \n",
"age -8.12395 11.37049 -0.714 0.4752 \n",
"agesq 0.12629 0.13519 0.934 0.3505 \n",
"yngkid 17.15441 50.00839 0.343 0.7317 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 419.6 on 700 degrees of freedom\n",
"Multiple R-squared: 0.1147,\tAdjusted R-squared: 0.1084 \n",
"F-statistic: 18.14 on 5 and 700 DF, p-value: < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fit_R <- lm(data = sleep75, sleep ~ totwrk + educ + age + agesq + yngkid)\n",
"summary(fit_R)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>400</li>\n",
"\t<li>34</li>\n",
"</ol>\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 400\n",
"\\item 34\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 400\n",
"2. 34\n",
"\n",
"\n"
],
"text/plain": [
"[1] 400 34"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>306</li>\n",
"\t<li>34</li>\n",
"</ol>\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 306\n",
"\\item 34\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 306\n",
"2. 34\n",
"\n",
"\n"
],
"text/plain": [
"[1] 306 34"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
" Min. 1st Qu. Median Mean 3rd Qu. Max. \n",
" 23.00 30.00 37.00 39.13 47.00 65.00 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sleep75_male <- filter(sleep75, male == 1)\n",
"sleep75_female <- filter(sleep75, male == 0)\n",
"dim(sleep75_male)\n",
"dim(sleep75_female)\n",
"summary(sleep75_male$age)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = sleep ~ totwrk + educ + age + agesq + yngkid, data = sleep75_male)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-1793.96 -216.05 7.93 244.57 1141.21 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 3648.20826 310.03933 11.767 < 2e-16 ***\n",
"totwrk -0.18212 0.02449 -7.438 6.45e-13 ***\n",
"educ -13.05238 7.41422 -1.760 0.0791 . \n",
"age 7.15659 14.32037 0.500 0.6175 \n",
"agesq -0.04477 0.16841 -0.266 0.7905 \n",
"yngkid 60.38021 59.02278 1.023 0.3069 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 402.3 on 394 degrees of freedom\n",
"Multiple R-squared: 0.1562,\tAdjusted R-squared: 0.1455 \n",
"F-statistic: 14.59 on 5 and 394 DF, p-value: 3.952e-13\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"## Male\n",
"\n",
"fit_male <- lm(data = sleep75_male, sleep ~ totwrk + educ + age + agesq + yngkid)\n",
"summary(fit_male)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = sleep ~ totwrk + educ + age + agesq + yngkid, data = sleep75_female)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-2485.02 -244.18 7.24 270.64 1376.91 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 4238.72933 384.89226 11.013 < 2e-16 ***\n",
"totwrk -0.13995 0.02766 -5.060 7.33e-07 ***\n",
"educ -10.20514 9.58885 -1.064 0.288 \n",
"age -30.35657 18.53091 -1.638 0.102 \n",
"agesq 0.36794 0.22334 1.647 0.101 \n",
"yngkid -118.28256 93.18757 -1.269 0.205 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 437 on 300 degrees of freedom\n",
"Multiple R-squared: 0.09768,\tAdjusted R-squared: 0.08264 \n",
"F-statistic: 6.495 on 5 and 300 DF, p-value: 9.42e-06\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"## Female\n",
"fit_female <- lm(data = sleep75_female, sleep ~ totwrk + educ + age + agesq + yngkid)\n",
"summary(fit_female)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = sleep ~ totwrk + educ + age + agesq + yngkid + male + \n",
" male * totwrk + male * educ + male * age + male * agesq + \n",
" male * yngkid, data = sleep75)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-2485.02 -226.74 7.93 257.77 1376.91 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 4238.72933 367.85193 11.523 < 2e-16 ***\n",
"totwrk -0.13995 0.02643 -5.294 1.61e-07 ***\n",
"educ -10.20514 9.16432 -1.114 0.2658 \n",
"age -30.35657 17.71049 -1.714 0.0870 . \n",
"agesq 0.36794 0.21345 1.724 0.0852 . \n",
"yngkid -118.28256 89.06187 -1.328 0.1846 \n",
"male -590.52107 488.79159 -1.208 0.2274 \n",
"totwrk:male -0.04217 0.03667 -1.150 0.2506 \n",
"educ:male -2.84724 11.96795 -0.238 0.8120 \n",
"age:male 37.51316 23.12332 1.622 0.1052 \n",
"agesq:male -0.41271 0.27591 -1.496 0.1352 \n",
"yngkid:male 178.66277 108.10510 1.653 0.0988 . \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 417.6 on 694 degrees of freedom\n",
"Multiple R-squared: 0.1306,\tAdjusted R-squared: 0.1168 \n",
"F-statistic: 9.479 on 11 and 694 DF, p-value: 4.947e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fit_UR <- lm(data = sleep75, sleep ~ totwrk + educ + age + agesq + yngkid + male + male*totwrk \n",
" + male*educ + male*age + male*agesq + male*yngkid)\n",
"summary(fit_UR)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"2.11635062871259"
],
"text/latex": [
"2.11635062871259"
],
"text/markdown": [
"2.11635062871259"
],
"text/plain": [
"[1] 2.116351"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Rsq_R <- summary(fit_R)$r.squared\n",
"Rsq_UR <- summary(fit_UR)$r.squared\n",
"F_stat <- ((Rsq_UR - Rsq_R)/6) / ((1 - Rsq_UR)/(dim(sleep75)[1] - (11 + 1)))\n",
"F_stat"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = sleep ~ totwrk + educ + age + agesq + yngkid + male + \n",
" new, data = newdata)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-2401.41 -232.60 9.96 254.40 1357.21 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 3808.26766 241.08468 15.796 < 2e-16 ***\n",
"totwrk -0.14192 0.02626 -5.404 8.94e-08 ***\n",
"educ -11.74231 5.87081 -2.000 0.0459 * \n",
"age -8.66185 11.32682 -0.765 0.4447 \n",
"agesq 0.12515 0.13467 0.929 0.3531 \n",
"yngkid -0.15211 50.26629 -0.003 0.9976 \n",
"male 172.70500 82.56363 2.092 0.0368 * \n",
"new -0.04118 0.03633 -1.134 0.2573 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 417.9 on 698 degrees of freedom\n",
"Multiple R-squared: 0.1244,\tAdjusted R-squared: 0.1156 \n",
"F-statistic: 14.16 on 7 and 698 DF, p-value: < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"newdata <- mutate(sleep75, new = male * totwrk)\n",
"fit <- lm(data = newdata, sleep ~ totwrk + educ + age + agesq + yngkid + male + new)\n",
"summary(fit)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = sleep ~ totwrk + educ + age + agesq + yngkid + male, \n",
" data = sleep75)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-2378.00 -243.29 6.73 259.24 1350.19 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 3840.85207 239.41393 16.043 <2e-16 ***\n",
"totwrk -0.16342 0.01816 -8.997 <2e-16 ***\n",
"educ -11.71327 5.87195 -1.995 0.0465 * \n",
"age -8.69740 11.32909 -0.768 0.4429 \n",
"agesq 0.12844 0.13467 0.954 0.3405 \n",
"yngkid -0.02280 50.27641 0.000 0.9996 \n",
"male 87.75455 34.66794 2.531 0.0116 * \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 418 on 699 degrees of freedom\n",
"Multiple R-squared: 0.1228,\tAdjusted R-squared: 0.1152 \n",
"F-statistic: 16.3 on 6 and 699 DF, p-value: < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fit_UR_2 <- lm(data = sleep75, sleep ~ totwrk + educ + age + agesq + yngkid + male)\n",
"summary(fit_UR_2)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0gAAANICAMAAADKOT/pAAAAOVBMVEUAAAAzMzNNTU1ZWVlo\naGh8fHyMjIyampqnp6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3j3+vLwcFhLQgvNPOB++TBeGddj54n5yHn3+Cwf0k\n8GZ4n50HQloY3mcAAggJQAAhAQggJAABhAQggJAABBASgABCAhDw/7BCDcfG0IXhAAAAAElF\nTkSuQmCC",
"text/plain": [
"plot without title"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"qplot(fit_UR$residuals, geom = \"histogram\", bins= 30)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"\tJarque-Bera test for normality\n",
"\n",
"data: fit_UR$residuals\n",
"JB = 231.2, p-value < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"library(normtest)\n",
"jb.norm.test(fit_UR$residuals)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"## Qns 3"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>'data'</li>\n",
"\t<li>'desc'</li>\n",
"\t<li>'self'</li>\n",
"</ol>\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 'data'\n",
"\\item 'desc'\n",
"\\item 'self'\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 'data'\n",
"2. 'desc'\n",
"3. 'self'\n",
"\n",
"\n"
],
"text/plain": [
"[1] \"data\" \"desc\" \"self\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"qns3 <- load(\"fertil2.Rdata\")\n",
"qns3"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<thead><tr><th scope=col>mnthborn</th><th scope=col>yearborn</th><th scope=col>age</th><th scope=col>electric</th><th scope=col>radio</th><th scope=col>tv</th><th scope=col>bicycle</th><th scope=col>educ</th><th scope=col>ceb</th><th scope=col>agefbrth</th><th scope=col>...</th><th scope=col>heduc</th><th scope=col>agesq</th><th scope=col>urban</th><th scope=col>urb_educ</th><th scope=col>spirit</th><th scope=col>protest</th><th scope=col>catholic</th><th scope=col>frsthalf</th><th scope=col>educ0</th><th scope=col>evermarr</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td> 5 </td><td>64 </td><td>24 </td><td>1 </td><td>1 </td><td>1 </td><td>1 </td><td>12 </td><td>0 </td><td>NA </td><td>... </td><td>NA </td><td> 576</td><td>1 </td><td>12 </td><td>0 </td><td>0 </td><td>0 </td><td>1 </td><td>0 </td><td>0 </td></tr>\n",
"\t<tr><td> 1 </td><td>56 </td><td>32 </td><td>1 </td><td>1 </td><td>1 </td><td>1 </td><td>13 </td><td>3 </td><td>25 </td><td>... </td><td>12 </td><td>1024</td><td>1 </td><td>13 </td><td>0 </td><td>0 </td><td>0 </td><td>1 </td><td>0 </td><td>1 </td></tr>\n",
"\t<tr><td> 7 </td><td>58 </td><td>30 </td><td>1 </td><td>0 </td><td>0 </td><td>0 </td><td> 5 </td><td>1 </td><td>27 </td><td>... </td><td> 7 </td><td> 900</td><td>1 </td><td> 5 </td><td>1 </td><td>0 </td><td>0 </td><td>0 </td><td>0 </td><td>1 </td></tr>\n",
"\t<tr><td>11 </td><td>45 </td><td>42 </td><td>1 </td><td>0 </td><td>1 </td><td>0 </td><td> 4 </td><td>3 </td><td>17 </td><td>... </td><td>11 </td><td>1764</td><td>1 </td><td> 4 </td><td>0 </td><td>0 </td><td>0 </td><td>0 </td><td>0 </td><td>1 </td></tr>\n",
"\t<tr><td> 5 </td><td>45 </td><td>43 </td><td>1 </td><td>1 </td><td>1 </td><td>1 </td><td>11 </td><td>2 </td><td>24 </td><td>... </td><td>14 </td><td>1849</td><td>1 </td><td>11 </td><td>0 </td><td>1 </td><td>0 </td><td>1 </td><td>0 </td><td>1 </td></tr>\n",
"\t<tr><td> 8 </td><td>52 </td><td>36 </td><td>1 </td><td>0 </td><td>0 </td><td>0 </td><td> 7 </td><td>1 </td><td>26 </td><td>... </td><td> 9 </td><td>1296</td><td>1 </td><td> 7 </td><td>0 </td><td>0 </td><td>0 </td><td>0 </td><td>0 </td><td>1 </td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\begin{tabular}{r|lllllllllllllllllllllllllll}\n",
" mnthborn & yearborn & age & electric & radio & tv & bicycle & educ & ceb & agefbrth & ... & heduc & agesq & urban & urb\\_educ & spirit & protest & catholic & frsthalf & educ0 & evermarr\\\\\n",
"\\hline\n",
"\t 5 & 64 & 24 & 1 & 1 & 1 & 1 & 12 & 0 & NA & ... & NA & 576 & 1 & 12 & 0 & 0 & 0 & 1 & 0 & 0 \\\\\n",
"\t 1 & 56 & 32 & 1 & 1 & 1 & 1 & 13 & 3 & 25 & ... & 12 & 1024 & 1 & 13 & 0 & 0 & 0 & 1 & 0 & 1 \\\\\n",
"\t 7 & 58 & 30 & 1 & 0 & 0 & 0 & 5 & 1 & 27 & ... & 7 & 900 & 1 & 5 & 1 & 0 & 0 & 0 & 0 & 1 \\\\\n",
"\t 11 & 45 & 42 & 1 & 0 & 1 & 0 & 4 & 3 & 17 & ... & 11 & 1764 & 1 & 4 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\n",
"\t 5 & 45 & 43 & 1 & 1 & 1 & 1 & 11 & 2 & 24 & ... & 14 & 1849 & 1 & 11 & 0 & 1 & 0 & 1 & 0 & 1 \\\\\n",
"\t 8 & 52 & 36 & 1 & 0 & 0 & 0 & 7 & 1 & 26 & ... & 9 & 1296 & 1 & 7 & 0 & 0 & 0 & 0 & 0 & 1 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"mnthborn | yearborn | age | electric | radio | tv | bicycle | educ | ceb | agefbrth | ... | heduc | agesq | urban | urb_educ | spirit | protest | catholic | frsthalf | educ0 | evermarr | \n",
"|---|---|---|---|---|---|\n",
"| 5 | 64 | 24 | 1 | 1 | 1 | 1 | 12 | 0 | NA | ... | NA | 576 | 1 | 12 | 0 | 0 | 0 | 1 | 0 | 0 | \n",
"| 1 | 56 | 32 | 1 | 1 | 1 | 1 | 13 | 3 | 25 | ... | 12 | 1024 | 1 | 13 | 0 | 0 | 0 | 1 | 0 | 1 | \n",
"| 7 | 58 | 30 | 1 | 0 | 0 | 0 | 5 | 1 | 27 | ... | 7 | 900 | 1 | 5 | 1 | 0 | 0 | 0 | 0 | 1 | \n",
"| 11 | 45 | 42 | 1 | 0 | 1 | 0 | 4 | 3 | 17 | ... | 11 | 1764 | 1 | 4 | 0 | 0 | 0 | 0 | 0 | 1 | \n",
"| 5 | 45 | 43 | 1 | 1 | 1 | 1 | 11 | 2 | 24 | ... | 14 | 1849 | 1 | 11 | 0 | 1 | 0 | 1 | 0 | 1 | \n",
"| 8 | 52 | 36 | 1 | 0 | 0 | 0 | 7 | 1 | 26 | ... | 9 | 1296 | 1 | 7 | 0 | 0 | 0 | 0 | 0 | 1 | \n",
"\n",
"\n"
],
"text/plain": [
" mnthborn yearborn age electric radio tv bicycle educ ceb agefbrth ... heduc\n",
"1 5 64 24 1 1 1 1 12 0 NA ... NA \n",
"2 1 56 32 1 1 1 1 13 3 25 ... 12 \n",
"3 7 58 30 1 0 0 0 5 1 27 ... 7 \n",
"4 11 45 42 1 0 1 0 4 3 17 ... 11 \n",
"5 5 45 43 1 1 1 1 11 2 24 ... 14 \n",
"6 8 52 36 1 0 0 0 7 1 26 ... 9 \n",
" agesq urban urb_educ spirit protest catholic frsthalf educ0 evermarr\n",
"1 576 1 12 0 0 0 1 0 0 \n",
"2 1024 1 13 0 0 0 1 0 1 \n",
"3 900 1 5 1 0 0 0 0 1 \n",
"4 1764 1 4 0 0 0 0 0 1 \n",
"5 1849 1 11 0 1 0 1 0 1 \n",
"6 1296 1 7 0 0 0 0 0 1 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<ol class=list-inline>\n",
"\t<li>4361</li>\n",
"\t<li>27</li>\n",
"</ol>\n"
],
"text/latex": [
"\\begin{enumerate*}\n",
"\\item 4361\n",
"\\item 27\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"1. 4361\n",
"2. 27\n",
"\n",
"\n"
],
"text/plain": [
"[1] 4361 27"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fertil2 <- data\n",
"head(fertil2)\n",
"dim(fertil2)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<thead><tr><th scope=col>variable</th><th scope=col>label</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td>mnthborn </td><td>month woman born </td></tr>\n",
"\t<tr><td>yearborn </td><td>year woman born </td></tr>\n",
"\t<tr><td>age </td><td>age in years </td></tr>\n",
"\t<tr><td>electric </td><td>=1 if has electricity </td></tr>\n",
"\t<tr><td>radio </td><td>=1 if has radio </td></tr>\n",
"\t<tr><td>tv </td><td>=1 if has tv </td></tr>\n",
"\t<tr><td>bicycle </td><td>=1 if has bicycle </td></tr>\n",
"\t<tr><td>educ </td><td>years of education </td></tr>\n",
"\t<tr><td>ceb </td><td>children ever born </td></tr>\n",
"\t<tr><td>agefbrth </td><td>age at first birth </td></tr>\n",
"\t<tr><td>children </td><td>number of living children </td></tr>\n",
"\t<tr><td>knowmeth </td><td>=1 if know about birth control</td></tr>\n",
"\t<tr><td>usemeth </td><td>=1 if ever use birth control </td></tr>\n",
"\t<tr><td>monthfm </td><td>month of first marriage </td></tr>\n",
"\t<tr><td>yearfm </td><td>year of first marriage </td></tr>\n",
"\t<tr><td>agefm </td><td>age at first marriage </td></tr>\n",
"\t<tr><td>idlnchld </td><td>'ideal' number of children </td></tr>\n",
"\t<tr><td>heduc </td><td>husband's years of education </td></tr>\n",
"\t<tr><td>agesq </td><td>age^2 </td></tr>\n",
"\t<tr><td>urban </td><td>=1 if live in urban area </td></tr>\n",
"\t<tr><td>urb_educ </td><td>urban*educ </td></tr>\n",
"\t<tr><td>spirit </td><td>=1 if religion == spirit </td></tr>\n",
"\t<tr><td>protest </td><td>=1 if religion == protestant </td></tr>\n",
"\t<tr><td>catholic </td><td>=1 if religion == catholic </td></tr>\n",
"\t<tr><td>frsthalf </td><td><span style=white-space:pre-wrap>=1 if mnthborn &lt;= 6 </span></td></tr>\n",
"\t<tr><td>educ0 </td><td>=1 if educ == 0 </td></tr>\n",
"\t<tr><td>evermarr </td><td>=1 if ever married </td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\begin{tabular}{r|ll}\n",
" variable & label\\\\\n",
"\\hline\n",
"\t mnthborn & month woman born \\\\\n",
"\t yearborn & year woman born \\\\\n",
"\t age & age in years \\\\\n",
"\t electric & =1 if has electricity \\\\\n",
"\t radio & =1 if has radio \\\\\n",
"\t tv & =1 if has tv \\\\\n",
"\t bicycle & =1 if has bicycle \\\\\n",
"\t educ & years of education \\\\\n",
"\t ceb & children ever born \\\\\n",
"\t agefbrth & age at first birth \\\\\n",
"\t children & number of living children \\\\\n",
"\t knowmeth & =1 if know about birth control\\\\\n",
"\t usemeth & =1 if ever use birth control \\\\\n",
"\t monthfm & month of first marriage \\\\\n",
"\t yearfm & year of first marriage \\\\\n",
"\t agefm & age at first marriage \\\\\n",
"\t idlnchld & 'ideal' number of children \\\\\n",
"\t heduc & husband's years of education \\\\\n",
"\t agesq & age\\textasciicircum{}2 \\\\\n",
"\t urban & =1 if live in urban area \\\\\n",
"\t urb\\_educ & urban*educ \\\\\n",
"\t spirit & =1 if religion == spirit \\\\\n",
"\t protest & =1 if religion == protestant \\\\\n",
"\t catholic & =1 if religion == catholic \\\\\n",
"\t frsthalf & =1 if mnthborn <= 6 \\\\\n",
"\t educ0 & =1 if educ == 0 \\\\\n",
"\t evermarr & =1 if ever married \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"variable | label | \n",
"|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
"| mnthborn | month woman born | \n",
"| yearborn | year woman born | \n",
"| age | age in years | \n",
"| electric | =1 if has electricity | \n",
"| radio | =1 if has radio | \n",
"| tv | =1 if has tv | \n",
"| bicycle | =1 if has bicycle | \n",
"| educ | years of education | \n",
"| ceb | children ever born | \n",
"| agefbrth | age at first birth | \n",
"| children | number of living children | \n",
"| knowmeth | =1 if know about birth control | \n",
"| usemeth | =1 if ever use birth control | \n",
"| monthfm | month of first marriage | \n",
"| yearfm | year of first marriage | \n",
"| agefm | age at first marriage | \n",
"| idlnchld | 'ideal' number of children | \n",
"| heduc | husband's years of education | \n",
"| agesq | age^2 | \n",
"| urban | =1 if live in urban area | \n",
"| urb_educ | urban*educ | \n",
"| spirit | =1 if religion == spirit | \n",
"| protest | =1 if religion == protestant | \n",
"| catholic | =1 if religion == catholic | \n",
"| frsthalf | =1 if mnthborn <= 6 | \n",
"| educ0 | =1 if educ == 0 | \n",
"| evermarr | =1 if ever married | \n",
"\n",
"\n"
],
"text/plain": [
" variable label \n",
"1 mnthborn month woman born \n",
"2 yearborn year woman born \n",
"3 age age in years \n",
"4 electric =1 if has electricity \n",
"5 radio =1 if has radio \n",
"6 tv =1 if has tv \n",
"7 bicycle =1 if has bicycle \n",
"8 educ years of education \n",
"9 ceb children ever born \n",
"10 agefbrth age at first birth \n",
"11 children number of living children \n",
"12 knowmeth =1 if know about birth control\n",
"13 usemeth =1 if ever use birth control \n",
"14 monthfm month of first marriage \n",
"15 yearfm year of first marriage \n",
"16 agefm age at first marriage \n",
"17 idlnchld 'ideal' number of children \n",
"18 heduc husband's years of education \n",
"19 agesq age^2 \n",
"20 urban =1 if live in urban area \n",
"21 urb_educ urban*educ \n",
"22 spirit =1 if religion == spirit \n",
"23 protest =1 if religion == protestant \n",
"24 catholic =1 if religion == catholic \n",
"25 frsthalf =1 if mnthborn <= 6 \n",
"26 educ0 =1 if educ == 0 \n",
"27 evermarr =1 if ever married "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"desc"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"0.140201927489674"
],
"text/latex": [
"0.140201927489674"
],
"text/markdown": [
"0.140201927489674"
],
"text/plain": [
"[1] 0.1402019"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mean(fertil2$electric,na.rm = TRUE)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"With Electric\"\n"
]
},
{
"data": {
"text/html": [
"1.89852700490998"
],
"text/latex": [
"1.89852700490998"
],
"text/markdown": [
"1.89852700490998"
],
"text/plain": [
"[1] 1.898527"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"1.80332510471238"
],
"text/latex": [
"1.80332510471238"
],
"text/markdown": [
"1.80332510471238"
],
"text/plain": [
"[1] 1.803325"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1] \"Without Electric\"\n"
]
},
{
"data": {
"text/html": [
"2.32772884974646"
],
"text/latex": [
"2.32772884974646"
],
"text/markdown": [
"2.32772884974646"
],
"text/plain": [
"[1] 2.327729"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"2.27744277380652"
],
"text/latex": [
"2.27744277380652"
],
"text/markdown": [
"2.27744277380652"
],
"text/plain": [
"[1] 2.277443"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fertil2_w_elec <- filter(fertil2, electric == 1)\n",
"fertil2_wo_elec <- filter(fertil2, electric == 0)\n",
"print(\"With Electric\")\n",
"mean(fertil2_w_elec$children)\n",
"sd(fertil2_w_elec$children)\n",
"print(\"Without Electric\")\n",
"mean(fertil2_wo_elec$children)\n",
"sd(fertil2_wo_elec$children)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = children ~ electric, data = fertil2)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-2.3277 -1.8985 -0.3277 1.6723 10.6723 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) 2.32773 0.03622 64.265 < 2e-16 ***\n",
"electric -0.42920 0.09673 -4.437 9.35e-06 ***\n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 2.217 on 4356 degrees of freedom\n",
" (3 observations deleted due to missingness)\n",
"Multiple R-squared: 0.004499,\tAdjusted R-squared: 0.004271 \n",
"F-statistic: 19.69 on 1 and 4356 DF, p-value: 9.35e-06\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fit2 <- lm(data = fertil2, children ~ electric)\n",
"summary(fit2)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = children ~ electric + age + agesq + educ + urban + \n",
" spirit + protest + catholic, data = fertil2)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-5.9514 -0.7041 -0.0101 0.7213 7.4435 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) -4.3147175 0.2433391 -17.731 < 2e-16 ***\n",
"electric -0.3057189 0.0690241 -4.429 9.69e-06 ***\n",
"age 0.3418668 0.0165186 20.696 < 2e-16 ***\n",
"agesq -0.0027596 0.0002722 -10.139 < 2e-16 ***\n",
"educ -0.0762130 0.0064608 -11.796 < 2e-16 ***\n",
"urban -0.2034131 0.0465864 -4.366 1.29e-05 ***\n",
"spirit 0.1404603 0.0558052 2.517 0.0119 * \n",
"protest 0.0753961 0.0652158 1.156 0.2477 \n",
"catholic 0.1173539 0.0834249 1.407 0.1596 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 1.451 on 4349 degrees of freedom\n",
" (3 observations deleted due to missingness)\n",
"Multiple R-squared: 0.574,\tAdjusted R-squared: 0.5733 \n",
"F-statistic: 732.6 on 8 and 4349 DF, p-value: < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fit3 <- lm(data = fertil2, children ~ electric + age + agesq + educ + urban + spirit + protest + catholic)\n",
"summary(fit3)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = children ~ electric + age + agesq + educ + urban + \n",
" spirit + protest + catholic + electric * educ, data = fertil2)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-5.9411 -0.7018 -0.0090 0.7235 7.4540 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) -4.3599481 0.2457539 -17.741 < 2e-16 ***\n",
"electric -0.1290919 0.1514041 -0.853 0.3939 \n",
"age 0.3433507 0.0165560 20.739 < 2e-16 ***\n",
"agesq -0.0027732 0.0002724 -10.182 < 2e-16 ***\n",
"educ -0.0720929 0.0071844 -10.035 < 2e-16 ***\n",
"urban -0.2096981 0.0468288 -4.478 7.73e-06 ***\n",
"spirit 0.1353488 0.0559367 2.420 0.0156 * \n",
"protest 0.0708878 0.0653010 1.086 0.2777 \n",
"catholic 0.1158265 0.0834262 1.388 0.1651 \n",
"electric:educ -0.0215641 0.0164523 -1.311 0.1900 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 1.451 on 4348 degrees of freedom\n",
" (3 observations deleted due to missingness)\n",
"Multiple R-squared: 0.5742,\tAdjusted R-squared: 0.5733 \n",
"F-statistic: 651.5 on 9 and 4348 DF, p-value: < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fit3 <- lm(data = fertil2, children ~ electric + age + agesq + educ + urban + spirit + protest + catholic + electric*educ)\n",
"summary(fit3)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"Call:\n",
"lm(formula = children ~ electric + age + agesq + educ + urban + \n",
" spirit + protest + catholic + electric_educ_7, data = fertil2_new)\n",
"\n",
"Residuals:\n",
" Min 1Q Median 3Q Max \n",
"-5.9411 -0.7018 -0.0090 0.7235 7.4540 \n",
"\n",
"Coefficients:\n",
" Estimate Std. Error t value Pr(>|t|) \n",
"(Intercept) -4.3599481 0.2457539 -17.741 < 2e-16 ***\n",
"electric -0.2800405 0.0717451 -3.903 9.63e-05 ***\n",
"age 0.3433507 0.0165560 20.739 < 2e-16 ***\n",
"agesq -0.0027732 0.0002724 -10.182 < 2e-16 ***\n",
"educ -0.0720929 0.0071844 -10.035 < 2e-16 ***\n",
"urban -0.2096981 0.0468288 -4.478 7.73e-06 ***\n",
"spirit 0.1353488 0.0559367 2.420 0.0156 * \n",
"protest 0.0708878 0.0653010 1.086 0.2777 \n",
"catholic 0.1158265 0.0834262 1.388 0.1651 \n",
"electric_educ_7 -0.0215641 0.0164523 -1.311 0.1900 \n",
"---\n",
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
"\n",
"Residual standard error: 1.451 on 4348 degrees of freedom\n",
" (3 observations deleted due to missingness)\n",
"Multiple R-squared: 0.5742,\tAdjusted R-squared: 0.5733 \n",
"F-statistic: 651.5 on 9 and 4348 DF, p-value: < 2.2e-16\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fertil2_new <- mutate(fertil2, educ_7 = (educ - 7)) %>% mutate(electric_educ_7 = electric * educ_7)\n",
"fit3 <- lm(data = fertil2_new, children ~ electric + age + agesq + educ + urban + spirit + protest + catholic + electric_educ_7)\n",
"summary(fit3)"
]
}
],
"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.4.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment