I created a function that should evaluate the performance of two statistical tests. within this function I am using the call-function within the first function to create my data generations. (already sorry for the amount of code) Thank you for your help!
The problem arises in the first if else statement in the first for loop. the Error says:
Error in [.data.frame
(Design, RowDesign, ) :
object 'RowDesign' not found
Here is the Function:
SimStudy <- function(Design, RowDesign, K){
#Design = Design Matrix
#RowDesign = Number that indicates which row of Design Matrix is used
#K = number that indicates amount of generated data sets for each row
Results <- matrix(NA, nrow=K, ncol=4)
for(k in 1:K){
if(normal) {
SimuDat <- call("DataGeneration1", x=Design[RowDesign, ])
}else{
SimuDat <- call("DataGeneration2", x=Design[RowDesign, ])
}
#Analysis with both methods
#Analysis with Welchs´s T-test
Analysis_old <- Method_old(SimData=SimuDat)
#Analysis with Trimmed F-test
Analysis_new <- Method_new(SimData=SimuDat)
P_old <- Analysis_old$p.value
P_new <- Analysis_new$p.value
#store in Vector (optional)
ResultsAnalysis <- c(P_old, P_new)
#Evaluation
Hyp <- ifelse(Design[RowDesign,"ef"] == 0, TRUE, FALSE)
TypeIerror_old <- 0
TypeIerror_new <- 0
TypeIIerror_old <- 0
TypeIIerror_new <- 0
if(Hyp==TRUE) {#i do not need to write ==TRUE necessarily
#Type I error
if(P_old < 0.05){
TypeIerror_old <- TypeIerror_old + 1
}
if(P_new < 0.05){
TypeIerror_new <- TypeIerror_new + 1
}
#Type II error
if(P_old >= 0.05){
TypeIIerror_old <- TypeIIerror_old + 1
}
if(P_new >= 0.05){
TypeIIerror_new <- TypeIIerror_new + 1
}
}#end if hyp
Results[k, ] <- c(TypeIerror_old, TypeIerror_new, TypeIIerror_old, TypeIIerror_new)
}#end for 1
return(Results)
}
And this is the code part with which i call the function:
totalcells <- nrow(Design1)
magic_for(put,silent=TRUE)
for(i in 1:totalcells){
RowDesign <- i
MyResult <- SimStudy(Design=Design1, RowDesign=RowDesign, K=4)
}#end loop Sim All rows
Result <- magic_result()
Finally, the functions and packages necessary to run the code:
#Packages necessary
install.packages("stats")
install.packages("PearsonDS")
install.packages("dplyr")
install.packages("magicfor")
library(stats)
library(PearsonDS)
library(dplyr)
library(magicfor)
#Preperation to creat fulfactorial Design Matrix
samp1 <- c(1,2,3,4)
samp2 <- c(1,2,3,4)
ef <- c(0,0.2,0.5,0.8)
vari1 <- c(3)
vari2 <- c(3,9)
Design1 <- expand.grid(samp1=samp1, samp2=samp2, ef=ef, vari1=vari1, vari2=vari2)
#Functions necessary
DataGeneration1 <- function(samp1, samp2, ef, vari1, vari2){
gen1 <- rnorm(n=samp1, mean=1, sd=sqrt(vari1))
gen2 <- rnorm(n=samp2, mean=1+ef, sd=sqrt(vari2))
Y <- c(gen1, gen2)
group <- as.factor(c(rep(1, times=length(gen1)), rep(2, times=length(gen2))))
SimData <- data.frame(Y,group)
return(SimData)
}
DataGeneration2 <- function(samp1, samp2, ef, vari1, vari2){
gen1 <- rnorm(n=samp1, mean=1, sd=sqrt(vari1)+2)
gen2 <- rnorm(n=samp2, mean=1+ef, sd=sqrt(vari2)+1)
Y <- c(gen1, gen2)
group <- as.factor(c(rep(1, times=length(gen1)), rep(2, times=length(gen2))))
SimData <- data.frame(Y,group)
return(SimData)
}
Method_old<- function(SimData){
formula <- Y~group
res <- t.test(formula, data = SimData)
return <- res
}
Method_new<- function(SimData){
formula <- Y~group
res <- wilcox.test(formula, data = SimData)
return <- res
}