Reading in Data
setwd("~/Dropbox/Works/Class/Case_studies_in_stat/201502/")
library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
monty.default<-read.xlsx("class_roll.xlsx", 1, startRow=2, colIndex=c(4:5, 10:14))
monty.default
## 학.직.번 이름 Monty_Hall Wason_1 Wason_2 Default HT
## 1 20093302 강준호 stay 2 Beer-17 있다 앞면
## 2 20123402 강혜빈 stay 3 3장 없다 앞면
## 3 20103404 김대한 change A-2 Beer-17 있다 뒷면
## 4 20133404 김소연 stay A-2-B Beer-17 있다 앞면
## 5 20123407 김연빈 stay <NA> Beer-17 있다 뒷면
## 6 20103413 김형섭 change A-2-B 31-Coke-17 없다 뒷면
## 7 20093319 김효현 stay A-3 Beer-17 있다 뒷면
## 8 20103415 김희현 stay A-B Beer 있다 뒷면
## 9 20103416 남순창 stay B-3 Beer-17 없다 앞면
## 10 20135124 남희정 change A-2 17 없다 뒷면
## 11 20133415 박경미 stay A-2-B-3 Beer-31 없다 뒷면
## 12 20123419 성홍주 stay A-2 Beer-17 있다 뒷면
## 13 20113420 손수정 change A Beer 없다 뒷면
## 14 20103422 신혁재 change A-3 Beer-17 있다 뒷면
## 15 20103423 신희명 <NA> <NA> <NA> <NA> <NA>
## 16 20133421 양한솔 stay A-3 Beer-17 있다 앞면
## 17 20113424 유계현 stay B 31-17 있다 뒷면
## 18 20123423 윤나현 stay A-3 Coke-17 없다 뒷면
## 19 20123425 이다은 stay <NA> Beer-17 없다 앞면
## 20 20123426 이란영 stay A-2-B 5명 없다 앞면
## 21 20133424 이민하 stay A-2-B-3 17 없다 앞면
## 22 20123429 이승훈 stay 3장 Beer-17 없다 뒷면
## 23 20133427 이은정 stay A-3 Beer-17 없다 뒷면
## 24 20113431 이흥배 change 3장 Beer 있다 앞면
## 25 20073164 장인섭 stay A-2 Beer-17 있다 앞면
## 26 20123434 정선경 stay 2장 2장 있다 뒷면
## 27 20127115 조덕주 stay A-3 Coke-17 없다 앞면
## 28 20105176 조형래 stay A-2 Beer-17 있다 앞면
## 29 20093378 최재혁 <NA> <NA> <NA> <NA> <NA>
## 30 20123437 최지영 stay B-2 Beer-Coke-17 없다 앞면
## 31 20123438 최혜정 stay A-3 Beer-31 있다 앞면
## 32 20083174 한대성 stay A-3 Beer-17 없다 뒷면
character
중 factor
성격인 변수들을 재설정
options(width=132)
monty.default$Monty_Hall <- factor(monty.default$Monty_Hall)
monty.default$Default <- factor(monty.default$Default)
monty.default$HT <- factor(monty.default$HT)
- Wason Selection 문제의 응답을 “Right/Wrong”을
label
로 갖는 factor
로 재코딩.
monty.default$Wason_1.f <- factor(ifelse(monty.default$Wason_1 == "A-3", "Right", "Wrong"))
monty.default$Wason_2.f <- factor(ifelse(monty.default$Wason_2 == "Beer-17", "Right", "Wrong"))
str(monty.default)
## 'data.frame': 32 obs. of 9 variables:
## $ 학.직.번 : num 20093302 20123402 20103404 20133404 20123407 ...
## $ 이름 : chr "강준호" "강혜빈" "김대한" "김소연" ...
## $ Monty_Hall: Factor w/ 2 levels "change","stay": 2 2 1 2 2 1 2 2 2 1 ...
## $ Wason_1 : chr "2" "3" "A-2" "A-2-B" ...
## $ Wason_2 : chr "Beer-17" "3장" "Beer-17" "Beer-17" ...
## $ Default : Factor w/ 2 levels "없다","있다": 2 1 2 2 2 1 2 2 1 1 ...
## $ HT : Factor w/ 2 levels "뒷면","앞면": 2 2 1 2 1 1 1 1 2 1 ...
## $ Wason_1.f : Factor w/ 2 levels "Right","Wrong": 2 2 2 2 NA 2 1 2 2 2 ...
## $ Wason_2.f : Factor w/ 2 levels "Right","Wrong": 1 2 1 1 1 2 1 2 1 2 ...
(results <- monty.default[, c(7, 3, 8:9, 6)])
## HT Monty_Hall Wason_1.f Wason_2.f Default
## 1 앞면 stay Wrong Right 있다
## 2 앞면 stay Wrong Wrong 없다
## 3 뒷면 change Wrong Right 있다
## 4 앞면 stay Wrong Right 있다
## 5 뒷면 stay <NA> Right 있다
## 6 뒷면 change Wrong Wrong 없다
## 7 뒷면 stay Right Right 있다
## 8 뒷면 stay Wrong Wrong 있다
## 9 앞면 stay Wrong Right 없다
## 10 뒷면 change Wrong Wrong 없다
## 11 뒷면 stay Wrong Wrong 없다
## 12 뒷면 stay Wrong Right 있다
## 13 뒷면 change Wrong Wrong 없다
## 14 뒷면 change Right Right 있다
## 15 <NA> <NA> <NA> <NA> <NA>
## 16 앞면 stay Right Right 있다
## 17 뒷면 stay Wrong Wrong 있다
## 18 뒷면 stay Right Wrong 없다
## 19 앞면 stay <NA> Right 없다
## 20 앞면 stay Wrong Wrong 없다
## 21 앞면 stay Wrong Wrong 없다
## 22 뒷면 stay Wrong Right 없다
## 23 뒷면 stay Right Right 없다
## 24 앞면 change Wrong Wrong 있다
## 25 앞면 stay Wrong Right 있다
## 26 뒷면 stay Wrong Wrong 있다
## 27 앞면 stay Right Wrong 없다
## 28 앞면 stay Wrong Right 있다
## 29 <NA> <NA> <NA> <NA> <NA>
## 30 앞면 stay Wrong Wrong 없다
## 31 앞면 stay Right Wrong 있다
## 32 뒷면 stay Right Right 없다
str(results)
## 'data.frame': 32 obs. of 5 variables:
## $ HT : Factor w/ 2 levels "뒷면","앞면": 2 2 1 2 1 1 1 1 2 1 ...
## $ Monty_Hall: Factor w/ 2 levels "change","stay": 2 2 1 2 2 1 2 2 2 1 ...
## $ Wason_1.f : Factor w/ 2 levels "Right","Wrong": 2 2 2 2 NA 2 1 2 2 2 ...
## $ Wason_2.f : Factor w/ 2 levels "Right","Wrong": 1 2 1 1 1 2 1 2 1 2 ...
## $ Default : Factor w/ 2 levels "없다","있다": 2 1 2 2 2 1 2 2 1 1 ...
- Monty Hall 문제의 응답을 앞/뒷면 별로
table()
로 요약
tapply(results$Monty_Hall, results$HT, table)
## $뒷면
##
## change stay
## 5 11
##
## $앞면
##
## change stay
## 1 13
options(digits=2)
tapply(results$Monty_Hall, results$HT, function(x) prop.table(table(x)))
## $뒷면
## x
## change stay
## 0.31 0.69
##
## $앞면
## x
## change stay
## 0.071 0.929
- data frame 으로 정리할 수 있도록
aggregate()
적용
aggregate(results$Monty_Hall, list(HT=results$HT), table)
## HT x.change x.stay
## 1 뒷면 5 11
## 2 앞면 1 13
aggregate(results$Monty_Hall, list(HT=results$HT), function(x) prop.table(table(x)))
## HT x.change x.stay
## 1 뒷면 0.312 0.688
## 2 앞면 0.071 0.929
- Wason Selection 문제의 응답을앞/뒷면 별로
table()
로 요약
tapply(results$Wason_1.f, results$HT, table)
## $뒷면
##
## Right Wrong
## 5 10
##
## $앞면
##
## Right Wrong
## 3 10
tapply(results$Wason_2.f, results$HT, table)
## $뒷면
##
## Right Wrong
## 8 8
##
## $앞면
##
## Right Wrong
## 7 7
- data frame 으로 정리할 수 있도록
aggregate()
적용. 두 방법의 차이에 유의
aggregate(results$Wason_1.f, list(HT=results$HT), table)
## HT x.Right x.Wrong
## 1 뒷면 5 10
## 2 앞면 3 10
aggregate(results$Wason_1.f, list(HT=results$HT), function(x) prop.table(table(x)))
## HT x.Right x.Wrong
## 1 뒷면 0.33 0.67
## 2 앞면 0.23 0.77
aggregate(Wason_2.f~HT, data=results, table)
## HT Wason_2.f.Right Wason_2.f.Wrong
## 1 뒷면 8 8
## 2 앞면 7 7
aggregate(Wason_2.f~HT, data=results, function(x) prop.table(table(x)))
## HT Wason_2.f.Right Wason_2.f.Wrong
## 1 뒷면 0.5 0.5
## 2 앞면 0.5 0.5
- 초기값 문제의 응답을 앞/뒷면 별로
table()
로 요약
tapply(results$Default, results$HT, table)
## $뒷면
##
## 없다 있다
## 8 8
##
## $앞면
##
## 없다 있다
## 7 7
- data frame 으로 정리할 수 있도록
aggregate()
적용
aggregate(results$Default, list(HT=results$HT), table)
## HT x.없다 x.있다
## 1 뒷면 8 8
## 2 앞면 7 7
aggregate(results$Default, list(HT=results$HT), function(x) prop.table(table(x)))
## HT x.없다 x.있다
## 1 뒷면 0.5 0.5
## 2 앞면 0.5 0.5
ddply()
를 사용할 수 있도록 plyr
package 등록
library(plyr)
ddply(data.frame(results$Default), .(HT = results$HT), table)
## HT 없다 있다
## 1 뒷면 8 8
## 2 앞면 7 7
## 3 <NA> 0 0
ddply(data.frame(results$Default), .(HT = results$HT), function(x) prop.table(table(x)))
## HT 없다 있다
## 1 뒷면 0.5 0.5
## 2 앞면 0.5 0.5
## 3 <NA> NaN NaN
save.image(file="monty_0910.rda")