17장 테이블(Tables)

작성자안재형|작성시간09.12.15|조회수1,605 목록 댓글 2

### Table 만들기

# respire.csv를 이용

respire = read.csv("U:/data/respire.csv")
respire

    treat outcome count
1 placebo       1    16
2 placebo       0    48
3    test          1    40
4    test          0    20

 

resp = xtabs(count~treat+outcome, data=respire)

resp

             outcome
treat          0  1
  placebo 48 16
  test       20 40

 

# respire2.csv를 이용

respire2 = read.csv("U:/data/data/respire2.csv")
resp = xtabs(~treat+outcome, data=respire2)

 

# 테이블을 matrix로 입력하기(가장 간단)

resp =  matrix(c(48,20,16,40), ncol=2)

 

### 카이제곱 검정
chisq.test(resp)

        Pearson's Chi-squared test with Yates' continuity correction

data:  resp
X-squared = 20.0589, df = 1, p-value = 7.51e-06

 

### Fisher의 Exact Test

 

# 데이터 읽기

> tea=read.csv("tea.csv")
> tea

  poured guessed count
1   milk    milk     3
2   milk     tea     1
3    tea    milk     1
4    tea     tea     3

 

# 테이블 만들기

> teat=xtabs(count~poured+guessed,data=tea)
> teat
      guessed
poured milk tea
  milk    3   1
  tea     1   3

 

# Fisher의 Exact Test

> fisher.test(teat)

 Fisher's Exact Test for Count Data

data:  teat
p-value = 0.4857
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
   0.2117329 621.9337505
sample estimates:
odds ratio
  6.408309

 

 

# 간단히 테이블을 matrix로 입력하여 분석하기

matrix(c(3,1,1,3),ncol=2)

     [,1] [,2]
[1,]    3    1
[2,]    1    3

 

> fisher.test(matrix(c(3,1,1,3),ncol=2))

 Fisher's Exact Test for Count Data

data:  matrix(c(3, 1, 1, 3), ncol = 2)
p-value = 0.4857
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
   0.2117329 621.9337505
sample estimates:
odds ratio
  6.408309

 

# 카이제곱 검정을 이용하면 Warning message가 뜬다.

> chisq.test(teat)

 Pearson's Chi-squared test with Yates' continuity correction

data:  teat
X-squared = 0.5, df = 1, p-value = 0.4795

Warning message:
In chisq.test(teat) : Chi-squared approximation may be incorrect

 

 

# 도수가 크면 Fisher의 Exact Test는 카이제곱 검정과 거의 같은 결과를 준다.

fisher.test(resp)

        Fisher's Exact Test for Count Data

data:  resp
p-value = 4.754e-06
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
  2.575436 14.136206
sample estimates:
odds ratio
  5.902133

 

### Cochran-Armitage Trend Test

prop.trend.test(c(13,7,21), c(42,14,28))

        Chi-squared Test for Trend in Proportions

data:  c(13, 7, 21) out of c(42, 14, 28) ,
 using scores: 1 2 3
X-squared = 13.0139, df = 1, p-value = 0.0003092

 

# unfavorable의 도수를 사용해도 동일한 결과

prop.trend.test(c(29,7,7),c(42,14,28)) 

  Chi-squared Test for Trend in Proportions

data:  c(29, 7, 7) out of c(42, 14, 28) ,
 using scores: 1 2 3
X-squared = 13.0139, df = 1, p-value = 0.0003092

 

## 카이제곱 검정을 사용해야하는 경우

# trend test

prop.trend.test(c(13,10,10),c(42,14,28))

 Chi-squared Test for Trend in Proportions

data:  c(13, 10, 10) out of c(42, 14, 28) ,
 using scores: 1 2 3
X-squared = 0.3872, df = 1, p-value = 0.5338

 

# 테이블을 매트릭스로 입력

matrix(c(13,29,10,4,10,18),ncol=3)

     [,1] [,2] [,3]
[1,]   13   10   10
[2,]   29    4   18

 

chisq.test(matrix(c(13,29,10,4,10,18),ncol=3))

 Pearson's Chi-squared test

data:  matrix(c(13, 29, 10, 4, 10, 18), ncol = 3)
X-squared = 7.4367, df = 2, p-value = 0.02427

 

 

### McNemar's test

mcnemar.test(matrix(c(5,15,5,7),ncol=2))

 McNemar's Chi-squared test with continuity correction

data:  matrix(c(5, 15, 5, 7), ncol = 2)
McNemar's chi-squared = 4.05, df = 1, p-value = 0.04417

 

### Simulation

set.seed(1234)
r2dtable(n=1,c(33,51),c(42,14,28))

[[1]]
     [,1] [,2] [,3]
[1,]   17    4   12
[2,]   25   10   16

 

 

 

 

 

 

### 연습

# 1

prop.trend.test(c(31,40,6),c(44,52,15))

 Chi-squared Test for Trend in Proportions

data:  c(31, 40, 6) out of c(44, 52, 15) ,
 using scores: 1 2 3
X-squared = 2.1821, df = 1, p-value = 0.1396

 

# 2

mcnemar.test(matrix(c(10,5,10,20),ncol=2))

 McNemar's Chi-squared test with continuity correction

data:  matrix(c(10, 5, 10, 20), ncol = 2)
McNemar's chi-squared = 1.0667, df = 1, p-value = 0.3017

 

 

다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
  • 작성자Charles | 작성시간 21.05.09 'R을 이용한 누구나 하는 통계분석' 책을 사서, 17장과 18장을 연습하려는데
    respire.csv, respire2.csv 데이터를 여기 카페에서 찾을 수가 없네요. 책 머리말에는 모든 데이터가 본 카페에 있다는데...
  • 답댓글 작성자안재형 작성자 본인 여부 작성자 | 작성시간 21.05.10 여기 data.zip에 다 있습니다.
    https://cafe.daum.net/biometrika/P8TH/25
댓글 전체보기
맨위로

카페 검색

카페 검색어 입력폼