# Compare tests with last-observation-caried forward

#________________________________________________________Create dataset

createdata <- function(n0, m0, sd0, n1, m1, sd1) {

DV <- rnorm(n0, mean=m0, sd=sd0)

IV <- rnorm(n1, mean=m1, sd=sd1)

tmpdata <- data.frame( cbind(DV, IV) )

return(tmpdata)}

 

#________________________________________________________Carry data

# set nr-records (last measurement carried forward)

carrydata <- function(dataset, nrec) {

tmpdata <- dataset

i <- 1 

while(i <= nrec ) {

tmpdata[i,2] <- tmpdata[i,1] 

         i <- i+1 }

return(tmpdata)

}

 

#________________________________________________________t-tests n-times

#________________________________________________________Last carried forward

testn <- 1000

datapvalue<- vector(length=testn)

 

i <- 1 

while(i <= testn ) {

#Ho is false

x <- createdata(26, 30.45, 9.61, 26, 35.81, 8.88)

#Ho is true

#x <- createdata(26, 30.45, 9.61, 26, 30.45, 9.61)

#Last measure carried forward

z <- carrydata(createdata(13, 30.45, 9.61, 13, 30.45, 9.61), 18) 

#Sampled

#z <- createdata(13, 30.45, 9.61, 13, 30.45, 9.61) 

 

Newdata <- rbind(x,z)

result <- t.test(Newdata$DV, Newdata$IV,paired=TRUE)

        datapvalue[i] <- result$p.value

        i <- i+1 }

 

#________________________________________________________all data

testn <- 1000

datapvalue<- vector(length=testn)

 

i <- 1 

while(i <= testn ) {

#Ho is false

Newdata <- createdata(39, 30.45, 9.61, 39, 35.81, 8.88)

#Ho is true

#Newdata <- createdata(39, 30.45, 9.61, 39, 30.45, 9.61)

result <- t.test(Newdata$DV, Newdata$IV,paired=TRUE)

        datapvalue[i] <- result$p.value

        i <- i+1 }

 

#________________________________________________________print testvalues

test <- data.frame(datapvalue)

names(test ) <- "p"

# > .05 en < .01

i <- 1 

while(i <= testn ) {

test$cat <- ifelse(test$p > 0.05, 0,

                           ifelse( test$p < 0.01, 2, 1) )      

      i <- i+1 }

 

slices <- table( test$cat ) 

lbls <- c("NS", "<.05", "<.01")

pct <- round(slices/sum(slices)*100)

lbls <- paste(lbls, pct) # add percents to labels 

lbls <- paste(lbls,"%",sep="") # ad % to labels 

pie(slices,labels = lbls, col=rainbow(length(lbls)), main="Pie Chart of P-values")