Matching Problems

options(digits=3)
n.matching<-c(0,1,2,4)
o.matching<-c(22,12,15,1)
p.matching<-c(9,8,6,1)/24
matching<-data.frame(n.matching, o.matching, p.matching)
matching
##   n.matching o.matching p.matching
## 1          0         22     0.3750
## 2          1         12     0.3333
## 3          2         15     0.2500
## 4          4          1     0.0417
mean.matching<-sum(n.matching*o.matching/sum(o.matching))
mean.matching
## [1] 0.92
chisq.test.matching<-chisq.test(x=o.matching,p=p.matching)
## Warning in chisq.test(x = o.matching, p = p.matching): Chi-squared
## approximation may be incorrect
chisq.test.matching
## 
##  Chi-squared test for given probabilities
## 
## data:  o.matching
## X-squared = 2.93, df = 3, p-value = 0.402
chisq.test.matching$statistic
## X-squared 
##      2.93
chisq.test.matching$parameter
## df 
##  3
chisq.test.matching$p.value
## [1] 0.402
chisq.test.matching$method
## [1] "Chi-squared test for given probabilities"
chisq.test.matching$data.name
## [1] "o.matching"
chisq.test.matching$observed
## [1] 22 12 15  1
chisq.test.matching$expected
## [1] 18.75 16.67 12.50  2.08
chisq.test.matching$residuals
## [1]  0.751 -1.143  0.707 -0.751
chisq.test.matching$stdres
## [1]  0.949 -1.400  0.816 -0.767
sum(o.matching)
## [1] 50
e.matching<-50*p.matching
e.matching
## [1] 18.75 16.67 12.50  2.08
(o.matching-e.matching)**2/e.matching
## [1] 0.563 1.307 0.500 0.563
sum((o.matching-e.matching)**2/e.matching)
## [1] 2.93
chisq.matching<-sum((o.matching-e.matching)**2/e.matching)
chisq.matching
## [1] 2.93
p.value<-1-pchisq(chisq.matching, df=3)
p.value
## [1] 0.402
matching<-data.frame(n.matching=n.matching, o.matching=o.matching, p.matching=p.matching)
matching
##   n.matching o.matching p.matching
## 1          0         22     0.3750
## 2          1         12     0.3333
## 3          2         15     0.2500
## 4          4          1     0.0417
str(matching)
## 'data.frame':    4 obs. of  3 variables:
##  $ n.matching: num  0 1 2 4
##  $ o.matching: num  22 12 15 1
##  $ p.matching: num  0.375 0.3333 0.25 0.0417
matching.2<-matching
matching.2
##   n.matching o.matching p.matching
## 1          0         22     0.3750
## 2          1         12     0.3333
## 3          2         15     0.2500
## 4          4          1     0.0417
matching.2[5,]<-matching.2[3,]+matching.2[4,]
matching.2
##   n.matching o.matching p.matching
## 1          0         22     0.3750
## 2          1         12     0.3333
## 3          2         15     0.2500
## 4          4          1     0.0417
## 5          6         16     0.2917
matching.2<-matching.2[-(3:4),]
matching.2
##   n.matching o.matching p.matching
## 1          0         22      0.375
## 2          1         12      0.333
## 5          6         16      0.292
matching.2$n.matching<-factor(matching.2$n.matching, levels=c(0,1,6),labels=c("0","1","2 or 4"))
chisq.test(x=matching.2$o.matching,p=matching.2$p.matching)
## 
##  Chi-squared test for given probabilities
## 
## data:  matching.2$o.matching
## X-squared = 2.01, df = 2, p-value = 0.3665
chisq.matching.B<-chisq.test(x=o.matching, p=p.matching, simulate.p.value=T, B=2000)
chisq.matching.B
## 
##  Chi-squared test for given probabilities with simulated p-value
##  (based on 2000 replicates)
## 
## data:  o.matching
## X-squared = 2.93, df = NA, p-value = 0.4013

Lottery Data의 Uniformity Test

lottery<-read.table("lottery.txt",header=TRUE)
head(lottery)
##   lottery.number lottery.payoff
## 1            810            190
## 2            156            120
## 3            140            286
## 4            542            184
## 5            507            384
## 6            972            324
attach(lottery)
summary(lottery.number)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       0     230     440     472     734     999
sd(lottery.number)
## [1] 294

One Sample Test

lottery.t.test<-t.test(lottery.number, mu=499.5)
lottery.t.test$statistic
##     t 
## -1.48
lottery.t.test$method
## [1] "One Sample t-test"
mean.number<-mean(lottery$lottery.number)
sd.number<-sd(lottery$lottery.number)
lottery.t<-(mean.number-499.5)/(sd.number/sqrt(254))
lottery.t
## [1] -1.48
pt(lottery.t, df=253)*2
## [1] 0.141
pnorm(lottery.t)*2
## [1] 0.14

카이제곱 적합도 검정

h10<-hist(lottery.number)

h10$counts
##  [1] 26 32 33 22 29 21 23 20 21 27
chisq.test(h10$counts)
## 
##  Chi-squared test for given probabilities
## 
## data:  h10$counts
## X-squared = 7.97, df = 9, p-value = 0.5373
chisq.test(h10$counts)$expected
##  [1] 25.4 25.4 25.4 25.4 25.4 25.4 25.4 25.4 25.4 25.4

opar<-par(no.readonly=TRUE)
par(mfrow=c(2,4))
h9<-hist(lottery.number, breaks=seq(0,999, by=111))
h8<-hist(lottery.number, breaks=seq(0,1000, by=125))
h7<-hist(lottery.number, breaks=seq(0,1001, by=143))
h6<-hist(lottery.number, breaks=seq(0,1002, by=167))
h5<-hist(lottery.number, breaks=seq(0,1000, by=200))
h4<-hist(lottery.number, breaks=seq(0,1000, by=250))
h3<-hist(lottery.number, breaks=seq(0,999, by=333))
h2<-hist(lottery.number, breaks=seq(0,1000, by=500))

lapply(list(h9$counts, h8$counts, h7$counts, h6$counts, h5$counts, h4$counts, h3$counts, h2$counts), chisq.test)
## [[1]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[1L]]
## X-squared = 9.76, df = 8, p-value = 0.282
## 
## 
## [[2]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[2L]]
## X-squared = 4.52, df = 7, p-value = 0.7183
## 
## 
## [[3]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[3L]]
## X-squared = 5.55, df = 6, p-value = 0.4753
## 
## 
## [[4]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[4L]]
## X-squared = 5.28, df = 5, p-value = 0.3832
## 
## 
## [[5]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[5L]]
## X-squared = 2.73, df = 4, p-value = 0.6036
## 
## 
## [[6]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[6L]]
## X-squared = 3.95, df = 3, p-value = 0.2666
## 
## 
## [[7]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[7L]]
## X-squared = 3.65, df = 2, p-value = 0.1616
## 
## 
## [[8]]
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[8L]]
## X-squared = 3.54, df = 1, p-value = 0.05979
lapply(list(h9=h9$counts, h8=h8$counts, h7=h7$counts, h6=h6$counts, h5=h5$counts, h4=h4$counts, h3=h3$counts, h2=h2$counts), chisq.test)
## $h9
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[1L]]
## X-squared = 9.76, df = 8, p-value = 0.282
## 
## 
## $h8
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[2L]]
## X-squared = 4.52, df = 7, p-value = 0.7183
## 
## 
## $h7
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[3L]]
## X-squared = 5.55, df = 6, p-value = 0.4753
## 
## 
## $h6
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[4L]]
## X-squared = 5.28, df = 5, p-value = 0.3832
## 
## 
## $h5
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[5L]]
## X-squared = 2.73, df = 4, p-value = 0.6036
## 
## 
## $h4
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[6L]]
## X-squared = 3.95, df = 3, p-value = 0.2666
## 
## 
## $h3
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[7L]]
## X-squared = 3.65, df = 2, p-value = 0.1616
## 
## 
## $h2
## 
##  Chi-squared test for given probabilities
## 
## data:  X[[8L]]
## X-squared = 3.54, df = 1, p-value = 0.05979
detach()