-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_variable.Rmd
174 lines (117 loc) · 4.38 KB
/
single_variable.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
title: "single var"
author: "liuc"
date: '2022-07-12'
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## 单因素统计分析
单因素统计分析,既是只有一个变量分组的情况,类似于`y ~ x`,X的分组既可以是一组也可以是两组、多组。
### 单因素定性数据(名义变量)
##### *对于单组数据*
clopper pearson Binomial Test in r:
适用于n < 30的情况,其零假设为:true probability of success is not equal to 0.5,备择假设为:
```{r}
library(DescTools)
# 7 为success,21为trials
BinomCI(7, 21,
conf.level = 0.95,
method = "clopper-pearson")
binom.test(x=7, n=21) # same result
observed = c(7, 14)
total = sum(observed)
BinomCI(observed, total,
conf.level = 0.95,
method = "clopper-pearson")
```
单组数据n>30时可以考虑 *Z-test*
One Sample Z-Test in R
```{r}
library(BSDA)
#enter IQ levels for 20 patients
data = c(88, 92, 94, 94, 96, 97, 97, 97, 99, 99,
105, 109, 109, 109, 110, 112, 112, 113, 114, 115)
#perform one sample z-test
z.test(data, mu=100, sigma.x=15)
```
Two Sample Z-Test in R
```{r}
library(BSDA)
#enter IQ levels for 20 individuals from each city
cityA = c(82, 84, 85, 89, 91, 91, 92, 94, 99, 99,
105, 109, 109, 109, 110, 112, 112, 113, 114, 114)
cityB = c(90, 91, 91, 91, 95, 95, 99, 99, 108, 109,
109, 114, 115, 116, 117, 117, 128, 129, 130, 133)
#perform two sample z-test
z.test(x=cityA, y=cityB, mu=0, sigma.x=15, sigma.y=15)
```
One Proportion Z-Test in R
```{r}
# If n ≤ 30: binom.test(x, n, p = 0.5, alternative = “two.sided”)
# If n> 30: prop.test(x, n, p = 0.5, alternative = “two.sided”, correct=TRUE)
prop.test(x=64, n=100, p=0.60, alternative="two.sided")
```
##### 单因素成组数据
既是X的分组大于或等于两组的情况。如上述的Two Sample Z-Test in R。
分组变量为有序多分类,响应变量为二分类时,除了可采用卡方检验外还可以采用Cochran-Armitage Test for Trend。
趋势性分析一般在实验组别中存在着计量梯度或暴露水平梯度时应用。
```{r}
library(DescTools)
dose <- matrix(c(10,9,10,7, 0,1,0,3), byrow=TRUE, nrow=2, dimnames=list(resp=0:1, dose=0:3))
Desc(dose)
CochranArmitageTest(dose)
CochranArmitageTest(dose, alternative="one.sided")
# 对于结果的阐释
```
_Mantel Haenszel_ 分层卡方
for 3-Dimensional Tables. The data are stratified so that each chi-square table is within one group or time.
One assumption of the test is that there are no three-way interactions in the data. This is confirmed with a non-significant result from a test such as the Woolf test or Breslow–Day test.
Post-hoc analysis can include looking at the individual chi-square, Fisher exact, or G-test for association for each time or group.
```{r}
Input = ("
County Sex Result Count
Bloom Female Pass 9
Bloom Female Fail 5
Bloom Male Pass 7
Bloom Male Fail 17
Cobblestone Female Pass 11
Cobblestone Female Fail 4
Cobblestone Male Pass 9
Cobblestone Male Fail 21
Dougal Female Pass 9
Dougal Female Fail 7
Dougal Male Pass 19
Dougal Male Fail 9
Heimlich Female Pass 15
Heimlich Female Fail 8
Heimlich Male Pass 14
Heimlich Male Fail 17
")
Data = read.table(textConnection(Input),header=TRUE)
### Order factors otherwise R will alphabetize them
Data$County = factor(Data$County,
levels=unique(Data$County))
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Data$Result = factor(Data$Result,
levels=unique(Data$Result))
Table = xtabs(Count ~ Sex + Result + County,
data=Data)
ftable(Table)
stats::mantelhaen.test(Table,
alternative = "two.sided",
correct = TRUE, exact = FALSE, conf.level = 0.95)
vcd::woolf_test(Table)
### Woolf test for homogeneity of odds ratios across strata.
### If significant, C-M-H test is not appropriate
rcompanion::groupwiseCMH(Table,
group = 3,
fisher = TRUE,
gtest = FALSE,
chisq = FALSE,
method = "fdr",
correct = "none",
digits = 3)
```