forked from jaimyoung/ipds-kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_basic_analysis.Rmd
216 lines (152 loc) · 4.27 KB
/
4_basic_analysis.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
title: "4_basic_analysis"
author: "Bill Chung"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gridExtra)
library(gapminder)
library(gapminder)
library(dplyr)
library(patchwork)
```
## Basic analysis
- see chapter 7 of the book by Dr.Kwon
```{r}
par(mfrow=c(1,1))
mpg <- tbl_df(mpg)
mpg
## quantative data
summary(mpg$hwy)
mean(mpg$hwy)
median(mpg$hwy)
range(mpg$hwy)
quantile(mpg$hwy)
```
# t-test
```{r}
#7.3.1
hwy <- mpg$hwy
n <- length(hwy)
mu0 <- 22.9
#one tail
t.test(hwy, mu=mu0, alternative = "greater")
#two tail, this is the default case
#unless you have pretty good reason, avoid one tail
t.test(hwy)
```
# Categorical value
```{r}
set.seed(1606)
n <- 100
p <- 0.5
x <- rbinom(n, 1, p)
x <- factor(x, levels = c(0,1), labels = c("no", "yes"))
x
table(x)
prop.table(table(x))
barplot(table(x))
binom.test(x=length(x[x=='yes']), n = length(x), p = 0.5, alternative = "two.sided")
binom.test(x=5400, n = 10000)
n <- c(100, 1000, 2000, 10000, 1e6)
data.frame(n=n, moe=round(1.96 * sqrt(1/(4 * n)),4))
curve(1.96 * sqrt(1/(4 * x)), 10, 10000, log='x')
grid()
n <- c(100, 1000, 2000, 10000, 1e6)
data.frame(n=n, moe=round(1.96 * sqrt(1/(4 * n)),4))
curve(1.96 * sqrt(1/(4 * x)), 10, 10000, log='x')
```
```{r}
# 7.6. 수량형 X, 수량형 Y의 분석
ggplot(mpg, aes(cty, hwy)) + geom_jitter() + geom_smooth(method="lm")
cor(mpg$cty, mpg$hwy)
with(mpg, cor(cty, hwy))
with(mpg, cor(cty, hwy, method = "kendall"))
with(mpg, cor(cty, hwy, method = "spearman"))
```
```{r}
# 7.6.3. 선형회귀모형 적합
(hwy_lm <- lm(hwy ~ cty, data=mpg))
summary(hwy_lm)
predict(hwy_lm)
resid(hwy_lm)
predict(hwy_lm, newdata = data.frame(cty=c(10, 20, 30)))
opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(hwy_lm, las = 1) # Residuals, Fitted, ...
```
```{r}
# 7.6.6. 로버스트 선형 회귀분석
library(MASS)
set.seed(123) # make reproducible
lqs(stack.loss ~ ., data = stackloss) # 로버스트
lm(stack.loss ~ ., data = stackloss) # 보통 선형모형
```
```{r}
# 7.6.7. 비선형/비모수적 방법, 평활법과 LOESS
plot(hwy ~ displ, data=mpg)
mpg_lo <- loess(hwy ~ displ, data=mpg)
mpg_lo
summary(mpg_lo)
xs <- seq(2,7,length.out = 100)
mpg_pre <- predict(mpg_lo, newdata=data.frame(displ=xs), se=TRUE)
lines(xs, mpg_pre$fit)
lines(xs, mpg_pre$fit - 1.96*mpg_pre$se.fit, lty=2)
lines(xs, mpg_pre$fit + 1.96*mpg_pre$se.fit, lty=2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth()
```
```{r}
# 7.7. 범주형 x, 수량형 y
mpg %>% ggplot(aes(class, hwy)) + geom_boxplot()
(hwy_lm2 <- lm(hwy ~ class, data=mpg))
summary(hwy_lm2)
predict(hwy_lm2, newdata=data.frame(class="pickup"))
opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(hwy_lm2, las = 1) # Residuals, Fitted, ...
par(opar)
```
```{r}
# 7.8. 수량형 x, 범주형 y (성공-실패)
library(gridExtra)
p1 <- ggplot(data.frame(x=c(0, 1)), aes(x)) +
stat_function(fun=function(x) log(x/(1-x))) + ylab('logit(x)') +
ggtitle("Logit function")
p2 <- ggplot(data.frame(y=c(-6, 6)), aes(y)) +
stat_function(fun=function(y) 1/(1+exp(-y))) + ylab('logistic(y)') +
ggtitle("Logistic function")
p1 | p2
```
# Challenger
```{r}
chall <- read.csv('https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/challenger.csv')
chall <- tbl_df(chall)
glimpse(chall)
p1 <- chall %>% ggplot(aes(temperature, distress_ct)) +
geom_point()
p2 <- chall %>% ggplot(aes(factor(distress_ct), temperature)) +
geom_boxplot()
p1|p2
(chall_glm <-
glm(cbind(distress_ct, o_ring_ct - distress_ct) ~
temperature, data=chall, family='binomial'))
summary(chall_glm)
predict(chall_glm, data.frame(temperature=30))
exp(3.45) / (exp(3.45) +1)
predict(chall_glm, data.frame(temperature=30), type='response')
logistic <- function(x){exp(x)/(exp(x)+1)}
plot(c(20,85), c(0,1), type = "n", xlab = "temperature",
ylab = "prob")
tp <- seq(20, 85, 1)
chall_glm_pred <-
predict(chall_glm,
data.frame(temperature = tp),
se.fit = TRUE)
lines(tp, logistic(chall_glm_pred$fit))
lines(tp, logistic(chall_glm_pred$fit - 1.96 * chall_glm_pred$se.fit), lty=2)
lines(tp, logistic(chall_glm_pred$fit + 1.96 * chall_glm_pred$se.fit), lty=2)
abline(v=30, lty=2, col='blue')
```