forked from davezes/Stats_141SL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_01_power_f-test_anova_01.R
123 lines (66 loc) · 2.27 KB
/
_01_power_f-test_anova_01.R
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
options(stringsAsFactors=FALSE, width=350)
rm(list=ls())
####### make sure in working directory
source("___f_funs.R")
xbool_save_file <- FALSE
############# simple one-way ANOVA F-test
############# SIMULATION
############# try comparing this result with that using G*Power
set.seed(777)
nn <- 300000
n <- 100 ### must be divisible by total number of groups -- even occupancy
x1dom <- c("A", "B")
x2dom <- c("c", "d")
x1 <- rep(x1dom, each=n/length(x1dom))
x2 <- rep(x2dom, n/length(x2dom))
xAmask <- x1 %in% c("A")
xBmask <- x1 %in% c("B")
xgrandmu <- 0
xgrandsigma <- 1
xsigma <- xgrandsigma
H0ABdelta <- 1 / xsigma #####
#H0ABdelta <- 0.5 / xsigma #####
xsigB <-
sd(
c(-H0ABdelta / 2, H0ABdelta / 2, 0)
)
xsigB
##### same as
##### sqrt( sum( c(-H0ABdelta / 2, H0ABdelta / 2)^2 ) / 2 )
ff <- xsigB / xsigma ; ff ### f in G*Power
xfvals <- rep(NA, nn)
for(ii in 1:nn) {
y <- rnorm(n, xgrandmu, xsigma)
y[ xAmask ] <- y[ xAmask ] + H0ABdelta / 2
y[ xBmask ] <- y[ xBmask ] - H0ABdelta / 2
var(y)
ybargrand <- mean(y)
yAmean <- mean(y[ xAmask ]) ; yAmean
yBmean <- mean(y[ xBmask ]) ; yBmean
SSb <- sum(xAmask) * ( yAmean - ybargrand )^2 + sum(xBmask) * ( yBmean - ybargrand )^2 ; SSb
SSe <- sum( (y[ xAmask ] - yAmean)^2 ) + sum( (y[ xBmask ] - yBmean)^2 ) ; SSe
SStot <- sum( (y - ybargrand)^2 ) ; SStot
SSb + SSe
MSb <- SSb / (2-1)
MSe <- SSe / (n-2)
#### s2pooled <- ((n1-1)*var(x1) + (n2-1)*var(x2)) / (n1 + n2 - 2)
fval <- MSb / MSe
xfvals[ii] <- fval
}
#### MSb / MStot
mean(xfvals)
hist(xfvals)
xalpha <- 0.01 #### here
fcut <- qf(xalpha, df1=1, df2=98, lower.tail=FALSE) ; fcut
###### empiric estimate of test power
sum(xfvals > fcut) / nn
d <- density(xfvals)
xlim <- c( min( c(0, xfvals) ), max( c(5, max(xfvals) ) ) ) ; xdom <- seq(xlim[1], xlim[2], length=1000)
if(xbool_save_file) {
png(file.path("~", "Desktop", "f_power_anova_example_01.png"), width=1500, height=480, pointsize=24)
}
par(mar=c(4,4,2,1))
plot(xdom, df(xdom, df1=1, df2=98), main="ANOVA, 2 Groups, Sim Example", col="#AA0000", lwd=2, type="l", ylab="density", xlab="F")
points(d, col="#0000AA", lwd=2, type="l")
abline(v=fcut)
if(xbool_save_file) { dev.off() }