forked from nicebread/p-hacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreliability_test.R
executable file
·58 lines (39 loc) · 1.39 KB
/
reliability_test.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
library(ggplot2)
library(Cairo)
set.seed(1)
num <- 20 #number of people to simulate
rel <- .3 #the reliability of the observed extraversion score
item_max <- 5
item_min <- 0
item_mean <- (item_max - item_min) / 2 #mean of true extraversion
items <- 3
#generate the data using the random normal distribution
#first simulate true (latent) scores
true_scores <- rnorm(num, item_mean) #true trait extraversion is normally distributed with sigma=1
values <- true_e
groups <- rep("true", length(values))
set.seed(NULL)
sum_scores <- rep(0,num)
# sum up scores
for(g in 1:items) {
# create scores for each item
true_scores <- rnorm(num, item_mean) #true trait extraversion is normally distributed with sigma=1
item_scores <- ( sqrt(rel) * (true_e - item_mean) ) + ( sqrt(1-rel) * rnorm(num) ) + mean_E
# limit scores according to min and max value of item
item_scores[item_scores < 0] <- 0
item_scores[item_scores > 6] <- 6
# sum up scores
sum_scores <- sum_scores + ( sqrt(rel)*(true_e-mean_E) + sqrt(1-rel)*rnorm(num) + mean_E )
}
# compute average of scores
sum_scores <- sum_scores / items
values <- c(values, sum_scores)
groups <- c(groups, rep("with rel", num))
df <- data.frame(
group = groups,
val = values
)
g <- ggplot(df, aes(x = group, y = val), ymin=0, ymax=6) +
ylim(0,6) +
geom_boxplot()
print(g)