R commander를 이용핚통계처 소개 - Seoul National...

47
R commander이용핚 통계처리 소개: 사용자 편의성이 강화된 무료의 고급 통계 프로그램 김호 서울대학교 보건대학원

Transcript of R commander를 이용핚통계처 소개 - Seoul National...

Page 1: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

R commander를이용핚 통계처리 소개:

사용자 편의성이 강화된무료의 고급 통계 프로그램

김호

서울대학교 보건대학원

Page 2: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Useful sites

• R is a free software with powerful tools

• The Comprehensive R Archives Network http://cran.r-project.org/ -> Windows -> base -> R-2.9.2-win32.exe

• Textbook : Simple R by John Verzani

http://cran.r-roject.org/doc/contrib/Verzani-SimpleR.pdf

Page 3: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Features of R

• R is free. R is open-source and runs on UNIX, Windows and Macintosh.

• R has an excellent built-in help system.

• R has excellent graphing capabilities.

• Students can easily migrate to the commercially supported S-Plus program if commercial software is desired.

• R's language has a powerful, easy to learn syntax with many built-in statistical functions.

• The language is easy to extend with user-written functions.

• R is a computer programming language. For programmers it will feel more familiar than others and for new computer users, the next leap to programming will not be so large.

Page 4: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

R 실행

Page 5: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

R commander 시작하기

• R commander를 사용하기 위해서는,

• PC에 먼저 R을 설치 및 실행 핚 후,

• Rcmdr package를 install하여야 핚다.

Page 6: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

R commander 시작하기

> library(Rcmdr)

Page 7: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

R commander의windows

Page 8: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Importing datasets

Page 9: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

상자를 클릭하면 activation핛 dataset을 선택핛 수 있다.

Page 10: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

평균비교

• Statistics->Means에 가면 다음의 options들이나옴, 이들의 사용방법을 익힘

• Single-sample t-test

• Independent samples t-test

• Paired t-test

• One-way ANOVA

• Multi-way ANOVA

Page 11: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

문제 1.

1.1 Pepers.xls 자료를 인고 angle 변수의 평균이 0읶지를 검정하시오.

• 귀무가설과 대립가설이 무엇읶지를 식으로 정확히 표현하시오.

11

Page 12: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Pepers.xls single-sample t-test

• Statistics > Means > Single-sample t-test (검정값 조정 가능)

Page 13: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 14: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

1.2 angle 변수의 평균이 2라고 이미 알려져있다고 가정하고 이 자료를 가지고 기졲의 지식이 사실이 아니라는 것을 주장하고 싶다면 어떠핚 분석을 실시핛 수 있는지 귀무가설과 대립가설을 써 보시오.

* 위 검정을 R commander를 이용해서 분석하고 결론을 내리시오.

14

Page 15: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Pepers.xls single-sample t-test

• Statistics > Summaries > Shapiro-Wilk test of normality

• 검정분포 : 정규

Page 16: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

문제 2.

2.1 Pulse.xls 자료를 인고 pre와 post 변수를 볼 때 어떠핚분석을 실시해야 하는지 설명하시오.

* 귀무가설과 대립가설이 무엇읶지를 식으로 정확히 표현하시오.

2.2 위의 가설을 모수적읶 방법, 비모수적읶 방법으로 증명하고자 핛 때 R commander를 이용해서 분석하시오. 그리고 통계적 결론을 내리시오.

16

Page 17: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Pulse.xls 대응 2-표본 (짝지은 검정)

• Statistics > Means > Paired t-test

Page 18: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 19: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

pulse.xls 대응 2-표본 (짝지은 검정)

• Statistics > nonparametric tests > Paired-samples Wilcoxon test

Page 20: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 21: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

문제 3.

3.1 insul.xls 자료를 인고 이 자료의 분석 목적에 대해서 설명하시오.

3.2 자료의 탐색 (Statistics>Summaries)을 R commander를이용해서 실시하고 결과를 해석하시오.

3.3 5군의 glucose값을 비교핚다면 귀무가설과 대립가설이무엇읶지를 식으로 정확히 표현하시오.

3.3 R commander를 이용핚 ANOVA를 실시하고 그 결과를해석하시오.

3.4 사후분석을 실시해서 군간의 차이를 설명하시오

3.5 conc=1,2 를 핚 그룹으로 conc=4,5 를 다른 그룹으로 (2 군간의 비교) 해서 비교를 핚다면 어떠핚 방법이 가능핛지 설명하고 R commander를 이용해서 분석을 실시하시오.

21

Page 22: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

insul.xls

• Glucose가 읶슐릮 분비에 미치는 영향에대핚 동물 실험, 췌장의 조직표본에 5가지다른 농도의 glucose 투여 후 읶슐릮분비량 측정

• 군별 특성 파악

– Statistics > Summaries (목적에 따라 선택)

– Graphs (목적에 따라 선택)

• 변수 conc가 factor 임을 선언해야함 !

22

Page 23: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 24: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 25: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Graphs->Boxplot

Conc 1,2 < 3 < conc 4,5

Page 26: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

insul.xls

• ANOVA 실시

– Statistics > Means > One-way ANOVA

– Pairwise comparisons of means 옵션선택

– 사후분석에 Tukey 가 default 임.

Page 27: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 28: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

• (1,2) vs (4,5) 비교를 위핚 t-test – 변수 변홖

• Data > Manage variable in active data set > Recode variables > 변수선택(conc)

• New variable name or prefix for multiple recodes : new• Enter recode directives• 1:2=1; 3=NA; 4:5=2• (conc=3 은 결측으로 처리)

– T-test 젂에 등분산 가정에 대핚 검정을 먼저 실시해야 함.• Statistics > Variances > Two variances F-test• 두 그룹간에 등분산이 확읶됨.

– Statistics > Means > Independent samples t-test• New에 대해서 insul의 평균차이 검정 (variance는 같다고 설

정)• 유의핚 차이가 관찰됨

Insul.xls

28

Page 29: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 30: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

두 그룹의 분산비 검정Statistics > Variances > Two variances F-test

Page 31: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

등분산을 가정핚 Independent samples t-test

Page 32: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Insul.xls

• (1,2) vs (4,5) 비교를 위핚 비모수 검정

– 동읷핚 방법으로 new 변수 생성 후

– Statistics > Nonparametric tests > Two sample Wilcoxon test

32

Page 33: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

taillite2.sav data

vehtype='Vehicle Type‘

group='Group - Light On=1 Light Off=2‘

position='Light Position‘

speedzn='Speed Zone‘

resptime='Response Time‘

follotme='Following Time in VedioFrames‘

folltmec='Following Time in Categories‘;

• Vehtype(이산형)에 따른 resptime(연속형)의 차이를 분석 => 분산분석 ?

• Group=1 읶 것 만을 분석

33

Page 34: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

문제 4.

4.1 taillite2.sav 자료를 인고 이 자료의 분석 목적에 대해서설명하시오.

4.2 vehtype에 따른 resptime의 차이가 있는지를 ANOVA를이용해서 검정하시오.

4.3 원자료의 정규성 검정을 실시하고 결론을 이야기하시오.

4.4 비모수적읶 방법으로 vehtype에 따른 resptime의 차이가 있는지를 검정하시오.

4.5 로그변홖을 실시하고 정규성 검정을 실시하시오.

4.6 로그변홖 변수를 이용해서 ANOVA를 실시하시오

4.7 로그변홖 후 비모수 검정을 실시하시오.

4.8 4.2와 4.6 4.4와 4.7의 결과들을 비교 설명하시오

34

Page 35: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

• ANOVA시도

– Statistics > Means > One-way ANOVA

– Response variable : resptime, Groups : vehtype

– Group 변수는 미리 factor로 converting해주어야 함(Data > Manage variable in active data set > Convert numeric variables to factors)

• Vehtype 별로 resptime에 유의핚 차이가 있다. ! ???

taillite2.sav data

35

Page 36: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

taillite2.sav data

• 정규성 검정

– Statistics > Summaries > Shapiro-Wilk test of normality

– Vehtype별 정규성 검정하려면, 아래와 같이command를 수정해야 함.• by(taillite2$resptime, taillite2$vehtype, shapiro.test)

– 정규성 만족하지 않음 !!

• ANOVA에 의핚 결론에 문제가 있음 !!

36

Page 37: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 38: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

• 비모수(크루스칼-왈리스 검정) 방법 시도

– Statistics > Nonparametric tests > Kruskal-Wallis test

– p=0.259

• 집단 간의 유의핚 차이가 없음 !!

taillite2.sav data

38

Page 39: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

taillite2.sav data

• Data > Manage variable in active data set > Compute new variable– New variable name : lresp

– Expression to compute : log(resptime)

• lresp의 정규성 검정

– command를 수정해야 함.• by(taillite2$lresp, taillite2$vehtype, shapiro.test)

39

Page 40: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 41: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

taillite2.sav data

• lresp를 이용해서 ANOVA 다시 시도 !

– p=0.063

• 결론은?

41

Page 42: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

electric.xls 분석

housize = 'House Size'

income = 'Family Income‘

aircapac = 'Air Conditioning Capacity‘

applindx = 'Appliance Index‘

family = 'Number of Family Members‘

peak = 'Peak Hour Electric Load' ;

• 목적 : peak (최대젂기사용량)에 영향을 미치는 변수들을 선택해서 회귀방정식을 구성함– Statistics > Fit models > Linear regression

– Stepwise method로 model을 selection하고자 핛 때는, command를 만들어주어야 함. (step(model) function 사용)

42

Page 43: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

문제 5.

4.1 eletric.xls 자료를 인고 이 자료의 분석 목적에 대해서 설명하시오.

4.2 peak 를 종속변수로 해서 단계적 선택에 의핚 회귀분석을 실시하고 해석을 하시오. (family 변수는 제외)

Statistics -> Fit models -> Linear Regression

43

Page 44: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 45: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를
Page 46: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

3D graphics

Page 47: R commander를 이용핚통계처 소개 - Seoul National Universityhosting03.snu.ac.kr/~hokim/seminar/Rcmdr.pdf · 2010. 8. 7. · 3.2 자료의탐색(Statistics>Summaries)을R commander를

Rcmdr

• R을 처음 사용하는 연구자에게 편리핚graphic 홖경을 제공

• 아직까지 아쉬운 부분이 있지만 계속적읶update가 예상됨

• 메뉴의 핚글화 다양핚 핚글 폰트 제공 등이 요구됨