28. R T๊ฒ์ ์ฐ์ต๋ฌธ์
01. ์ฐ๋ฆฌ๋๋ผ ์ ์ฒด ์คํ๊ต 2ํ๋ ์ฌํ์ ํ๊ท ํค๊ฐ 148.5cm๋ก ์๋ ค์ ธ ์๋ ์ํ์์ A์คํ๊ต 2ํ๋ ์ ์ฒด 500๋ช ์ ๋์์ผ๋ก 10%์ธ 50๋ช ์ ํ๋ณธ์ผ๋ก ์ ์ ํ์ฌ ํ๋ณธํ๊ท ์ ์ฅ์ ๊ณ์ฐํ๊ณ , ๋ชจ์ง๋จ์ ํ๊ท ๊ณผ ์ฐจ์ด๊ฐ ์๋์ง๋ฅผ ๊ฒ์ ํ์์ค.(๋จ์ผํ๋ณธ T๊ฒ์ )
setwd('C:/ITWILL/2_Rwork/data')
๋จ๊ณ1 : ๋ฐ์ดํฐ์ ๊ฐ์ ธ์ค๊ธฐ
stheight<- read.csv("student_height.csv")
stheight
height <- stheight$height
head(height)
๋จ๊ณ2 : ๊ธฐ์ ํต๊ณ๋/๊ฒฐ์ธก์น ํ์ธ
ength(height) #50
summary(height) # 149.4
x <- na.omit(height)
x # ์ ์ ๋ฐ์ดํฐ
mean(x) # 149.4 -> ํ๊ท ์ ์ฅ
๋จ๊ณ3 : ์ ๊ท์ฑ ๊ฒ์ - ๊ธฐ๋ณธ๊ฐ์
shapiro.test(x) # p-value = 0.0001853 < 0.05 : ๊ธฐ๊ฐ
hist(x) # ์ผ์ชฝ์ผ๋ก ๊ธฐ์ธ์ด์ง
๋จ๊ณ4 : ๊ฐ์ค๊ฒ์ - ์์ธก๊ฒ์ : ๋น๋ชจ์๊ฒ์
wilcox.test(x, mu=148.5)
# V = 826, p-value = 0.067 > 0.05
[ํด์ค] ๋ชจํ๊ท ๊ณผ ์ฐจ์ด๊ฐ ์๋ค.
02. ๊ต์ก๋ฐฉ๋ฒ์ ๋ฐ๋ผ ์ํ์ฑ์ ์ ์ฐจ์ด๊ฐ ์๋์ง ๊ฒ์ ํ์์ค.(๋
๋ฆฝํ๋ณธ T๊ฒ์ )
์กฐ๊ฑด1) ๋ณ์ : method : ๊ต์ก๋ฐฉ๋ฒ, score : ์ํ์ฑ์
์กฐ๊ฑด2) ๋ชจ๋ธ : ๊ต์ก๋ฐฉ๋ฒ(๋ฒ์ฃผํ๋ณ์) -> ์ํ์ฑ์ (์ฐ์ํ๋ณ์)
์กฐ๊ฑด3) ์ ์ฒ๋ฆฌ : ๊ฒฐ์ธก์น ์ ๊ฑฐ : ํ๊ท ์ผ๋ก ๋์ฒด
๋จ๊ณ1. ์ค์ตํ์ผ ๊ฐ์ ธ์ค๊ธฐ
Data <- read.csv("twomethod.csv", header=TRUE)
head(Data) #3๊ฐ ๋ณ์ ํ์ธ -> id method score
๋จ๊ณ2. ๋ ์ง๋จ subset ์์ฑ
unique(Data$method) # 1 2
table(Data$method)
๋ณ์ ์ ํ -> ์๋ธ์
์์ฑ
data_df <- Data[c('method', 'score')]
data_df
๋จ๊ณ3. ๋ฐ์ดํฐ ๋ถ๋ฆฌ
1) ์ง๋จ(๊ต์ก๋ฐฉ๋ฒ)์ผ๋ก ๋ถ๋ฆฌ
method1 <- subset(data_df, method == 1)
method2 <- subset(data_df, method == 2)
dim(method1) # 24 2
dim(method2) # 39 2
2) ๊ต์ก๋ฐฉ๋ฒ์์ ์ํ์ฑ์ ์ถ์ถ
score1 <- method1$score
score2 <- method2$score
๋จ๊ณ4 : ๋ถํฌ๋ชจ์ ๊ฒ์ : ๋ฑ๋ถ์ฐ์ฑ ๊ฒ์ (์ฐ์ํ๋ณ์)
var.test(score1, score2) # p-value = 0.8494
๋จ๊ณ5: ๊ฐ์ค๊ฒ์
t.test(score1, score2) # ๊ท๋ฌด๊ฐ์ค ๊ธฐ๊ฐ
# t = -5.6056(์ ๋๊ฐ), df = 43.705, p-value = 1.303e-06=0.000001303
๋ฐฉํฅ์ฑ์ด ์๋ ์ฐ๊ตฌ๊ฐ์ค ๊ฒ์ (๊ธฐ๊ฐ) : score1 > score2
t.test(score1, score2, alter="greater", conf.int=TRUE, conf.level=0.95)
๋ฐฉํฅ์ฑ์ด ์๋ ์ฐ๊ตฌ๊ฐ์ค ๊ฒ์ (์ฑํ) : score1 < score2
t.test(score1, score2, alter="less", conf.int=TRUE, conf.level=0.95)
03.datas๋ฅผ ๋์์ผ๋ก ์ฐ๋ น๋ณ(age) ๋ง์กฑ๋(satis)์ ์ฐจ์ด๊ฐ ์๋์ง ๊ฒ์ ํ์์ค. (์ผ์๋ฐฐ์น ๋ถ์ฐ๋ถ์ : ๋ชจ์ ๊ฒ์ )
๋จ๊ณ1 : dataset ์์ฑ
20๋ ๋ง์กฑ๋(10์ ๋ง์กฑ)
age20 <- rep(20, 10)
satis20 <- c(5,7,10,6,8,3,9,5,6,5)
df1 <- data.frame(age=age20, satis=satis20)
30๋ ๋ง์กฑ๋
age30 <- rep(30, 10)
satis30 <- c(8,7,10,6,8,5,9,7,6,6)
df2 <- data.frame(age=age30, satis=satis30)
40๋ ๋ง์กฑ๋
age40 <- rep(40, 10)
satis40 <- c(8,9,10,6,8,7,9,7,9,8)
df3 <- data.frame(age=age40, satis=satis40)
DataFrame ์์ฑ
datas <- rbind(df1, df2, df3)
datas # age satis
str(datas)
๋
๋ฆฝ๋ณ์ ์์ธํ ๋ณํ : ์ง๋จ๋ณ์ ์์ฑ(์ซ์๋ณ์ : ์ฌํ๊ฒ์ ์ ์ค๋ฅ)
datas$age <- as.factor(datas$age)
str(datas) # $ age : Factor w/ 3 levels
๋จ๊ณ2 : ๋ฑ๋ถ์ฐ์ฑ ๊ฒ์ : ์ฐ๋ น์ ๋ฐ๋ฅธ ๋ง์กฑ๋์ ๋ถ์ฐ ์ฐจ์ด
bartlett.test(satis ~ age, data = datas) # p-value = 0.2494
๋จ๊ณ3 : ๋ถ์ฐ๋ถ์
model <- aov(satis ~ age, data = datas)
๋จ๊ณ4. ๋ถ์๋ถ์ ๊ฒฐ๊ณผ ํด์
summary(model)
# age 2 14.47 7.233 2.607 0.0922 .
[ํด์ค] ์ฐ๋ น๋ณ(20,30,40) ๋ง์กฑ๋์ ์ฐจ์ด๊ฐ ์๋ค.
๋จ๊ณ5. ์ฌํ๊ฒ์
TukeyHSD(model)
diff lwr upr p adj
30-20 0.8 -1.0468164 2.646816 0.5379671 -> ์ฐจ์ด ์์
40-20 1.7 -0.1468164 3.546816 0.0756158 -> ์ฐจ์ด ์์
40-30 0.9 -0.9468164 2.746816 0.4586358 -> ์ฐจ์ด ์์
plot(TukeyHSD(model))
[ํด์ค] 3์ง๋จ ๋ชจ๋ ์ ๋ขฐ๊ตฌ๊ฐ์ 0์ ํฌํจํ๊ณ ์์
04.airquality๋ฅผ ๋์์ผ๋ก ์๋ณ(Month)๋ก ์ค์กด๋(Ozone)์ ์ฐจ์ด๊ฐ ์๋์ง ๊ฒ์ ํ์์ค. (์ผ์๋ฐฐ์น ๋ถ์ฐ๋ถ์ : ๋น๋ชจ์ ๊ฒ์ )
data(airquality)
str(airquality)
# $ Ozone -> y : ์ฐ์ํ ๋ณ์
# $ Month -> x : ์ง๋จ๋ณ์
table(airquality$Month) # 5 6 7 8 9
๋จ๊ณ 1: ์ ์ฒ๋ฆฌ(๊ฒฐ์ธก์น ์ ๊ฑฐ)
summary(airquality) # ๊ฒฐ์ธก์น ๋ฐ๊ฒฌ
dataset <- na.omit(airquality) # ๊ฒฐ์ธก์น ์ ๊ฑฐ
dataset$Month <- as.factor(dataset$Month)
str(dataset) # $ Month : Factor w/ 5 levels
๋จ๊ณ 2: ๋ฑ๋ถ์ฐ์ฑ ๊ฒ์
bartlett.test(Ozone ~ Month, data = dataset)
* p-value = 0.007395 : ๋น๋ชจ์ ๊ฒ์
๋จ๊ณ 3: ๋ถ์ฐ๋ถ์(๋ชจ์ vs ๋น๋ชจ์) & ํด์
kruskal.test(Ozone ~ Month, data = dataset)
* p-value = 2.742e-05 : ์์ด๋ ํ ์ง๋จ ์ด์ ํ๊ท ์ฐจ์ด
๋จ๊ณ 4: ์ฌํ๊ฒ์ : ์ง๋จ๋ณ ํ๊ท (dplyr ํจํค์ง ์ด์ฉ)
install.packages('dplyr')
library(dplyr)
์๋ณ ์ค์กด๋์ ํ๊ท
dataset %>% group_by(Month) %>% summarise(avg = mean(Ozone))
Month avg
<fct> <dbl>
1 5 24.1
2 6 29.4
3 7 59.1
4 8 60
5 9 31.4
[ํด์ค] 8์์ ํ๊ท ์ค์กด๋์ด ๊ฐ์ฅ๋ง๊ณ , 5์์ด ๊ฐ์ฅ ์๋ค.