-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
186 lines (139 loc) · 4.58 KB
/
functions.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Load required libraries
library(ggplot2)
library(dplyr)
library(MASS)
library(tidyverse)
library(pwr)
library(pwrss)
library(gridExtra)
# Function to generate data for higher temperature group
high_temp <- function() {
data.frame(
development_time = rnorm(sample_size, mean_high_temp, sd = sqrt(var_high_temp)),
group = "Higher Temperature"
)
}
#print(high_temp())
# Function to generate data for lower temperature group
low_temp <- function() {
data.frame(
development_time = rnorm(sample_size, mean_low_temp, sd = sqrt(var_low_temp)),
group = "Lower Temperature"
)
}
#print(low_temp())
# Combine the data frames
df <- rbind(high_temp(), low_temp())
# Function to perform ANOVA and generate box plot
anova_boxplot <- function() {
anova_result <- aov(development_time ~ group, data = df)
print(summary(anova_result))
boxplot <- ggplot(df, aes(x = group, y = development_time, fill = group)) +
geom_boxplot() +
labs(title = "Development Time of Insects at Different Temperatures",
x = "Temperature Group",
y = "Development Time (days)") +
theme_minimal()
#print(boxplot)
}
# Call the function to perform ANOVA and create a box plot
anova_boxplot()
multiple_iterations <- function() {
results <- list()
num_iterations <- 6
for (i in 1:num_iterations) {
high_temp_data <- high_temp() # Fixed function call
low_temp_data <- low_temp() # Fixed function call
df <- rbind(high_temp_data, low_temp_data)
anova_result <- aov(development_time ~ group, data = df)
results[[i]] <- list(
summary = summary(anova_result),
data = df
)
}
# Print summaries of ANOVA results for each iteration
for (i in 1:num_iterations) {
cat("Iteration", i, ":\n")
#print(results[[i]]$summary)
}
# Generate plots showing the density of development_time by group for each iteration
plot_list <- lapply(results, function(res) {
ggplot(res$data, aes(x = development_time, fill = group)) + # Fixed variable name
geom_density(alpha = 0.5) +
labs(title = "Development Time Density by Group",
x = "Development Time",
y = "Density") +
theme_minimal()
})
gridExtra::grid.arrange(grobs = plot_list, ncol = 2)
}
multiple_iterations()
# Function to calculate minimum detectable effect size (Cohen's d)
min_effect_size <- function() {
alpha <- 0.05
n <- sample_size
sd1 <- sqrt(var_high_temp)
mean1 <- mean_high_temp
sd2 <- sqrt(var_low_temp)
mean2 <- mean_low_temp
d_values <- seq(0.01, 1, by = 0.01)
significant_effects <- numeric(length(d_values))
for (i in 1:length(d_values)) {
d <- d_values[i]
means_diff <- d * sqrt((sd1^2 + sd2^2) / 2)
t_test_result <- t.test(rnorm(n, mean1, sd1), rnorm(n, mean2, sd2), var.equal = TRUE)
p_value <- t_test_result$p.value
significant_effects[i] <- p_value
}
min_d <- d_values[which(significant_effects < alpha)[1]]
print(paste("Minimum detectable effect size (Cohen's d):", min_d))
}
min_effect_size()
# Function to calculate minimum sample size for given effect sizes
min_sample_size <- function() {
alpha <- 0.05
power <- 0.8
sd1 <- sqrt(var_high_temp)
mean1 <- mean_high_temp
sd2 <- sqrt(var_low_temp)
mean2 <- mean_low_temp
# Calculate Statistical Power
result <- pwr.t.test(d = (mean1 - mean2) / sqrt((sd1^2 + sd2^2) / 2), sig.level = alpha, power = power)
# Print Statistical Power
print(result)
# Calculate Minimum Required Sample Size and print
print(result$n)
}
# Call the function
min_sample_size()
ci_diff <- function() {
sd1 <- sqrt(var_high_temp)
mean1 <- mean_high_temp
sd2 <- sqrt(var_low_temp)
mean2 <- mean_low_temp
t.test(rnorm(sample_size, mean1, sd1), rnorm(sample_size, mean2, sd2), var.equal = TRUE)$conf.int
}
multiple_iterations <- function(num_iterations) {
results <- list()
for (i in 1:num_iterations) {
# Generate new data for each iteration
high_temp_data <- high_temp()
low_temp_data <- low_temp()
df <- rbind(high_temp_data, low_temp_data)
# Perform ANOVA and store summary statistics
anova_result <- aov(development_time ~ group, data = df)
results[[i]] <- summary(anova_result)
}
return(results)
}
# Function to print summary statistics from multiple iterations
print_summary_statistics <- function(summary_stats) {
for (i in 1:length(summary_stats)) {
cat("Iteration", i, ":\n")
print(summary_stats[[i]])
}
}
# Perform multiple iterations of analysis
summary_stats <- multiple_iterations(num_iterations)
# Print summary statistics
print_summary_statistics(summary_stats)