-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handout4Code_RMarkdown_students-1.Rmd
92 lines (64 loc) · 1.86 KB
/
Handout4Code_RMarkdown_students-1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Handout4Code_RMarkdown_students"
output: html_document
editor_options:
chunk_output_type: console
---
# Example 1: MaleBirths.xls
Import Data Into R from Excel file\
(1) Run command: install.packages("readxl") if have never done so\
(2) Save data file to same location as R script; then,\
(3) Run the following three commands
```{r}
library(readxl)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
MaleBirths=read_excel("MaleBirths.xls")
```
View data frame structure -*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
str(???)
View(???)
```
### 1(a & (e))
Fitted line plots for each country
```{r}
par(mfrow=c(1,3)) #1x3 plot grid
```
USA
```{r}
plot(usa~year, data=MaleBirths, ylab='male birth percentage',pch=19,ylim=c(50.8,51.6),main='USA')
fit_usa=lm(usa~year, data=MaleBirths)
abline(fit_usa,col='blue')
```
Canada
```{r}
plot(canada~year,data=MaleBirths,ylab='male birth percentage', pch=19,ylim=c(50.8,51.6),main='Canada')
fit_canada=lm(canada~year, data=MaleBirths)
abline(fit_canada,col='blue')
```
Netherlands
\-*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
plot(netherla~year,data=MaleBirths,ylab='???',pch=19,ylim=c(50.8,51.6),main='???')
fit_netherla=lm(netherla~year, data=MaleBirths)
abline(fit_netherla,col='blue')
```
### 1(f-h)
Model summary % male birth regressed on year each country
\-*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
summary(fit_usa)
summary(fit_canada)
summary(fit_netherla)
```
s each country
```{r}
c(summary(fit_usa)$sigma,summary(fit_canada)$sigma,summary(fit_netherla)$sigma)
```
R.squared each country
\-*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
c(summary(fit_usa)$r.squared,
summary(fit_canada)$r.squared,
summary(fit_netherla)$r.squared)
```