#Demonstrates how 95% confidence interval and p-value are computed x <- c(2.4,3.2,5.1,6.7,2.4,2.7) mean(x) sd(x) t.test(x) #one-sample t-test #Check results from one-sample t-test using manual compuation x1 <- mean(x)+qt(0.025,5)*sd(x)/sqrt(6) x2 <- mean(x)+qt(0.975,5)*sd(x)/sqrt(6) obs <- mean(x)/(sd(x)/sqrt(6)) p <- 2*(1-pt(mean(x)/(sd(x)/sqrt(6)),5)) #Two-sample t-test y <- c(3.4,2.6,5.4,1.3,6.7,7) #Check if variance of sample x and sample y is the same var.test(x,y) #If variance is the same, we will use equal variance. Otherwise use unequal variance. #Read Literacy data setwd("C:/G_MWU/Taiwan/DrTam/2014/NovClass/Class2") Lit <- read.csv("Literacy2.csv") head(Lit) LitF <- Lit[Lit\$gender=="F",] LitM <- Lit[Lit\$gender=="M",] R_F <- LitF\$reading R_M <- LitM\$reading t.test(R_F,R_M) #Test the whole sample R_F_100 <- sample(R_F,100) #Select only 100 female students R_M_100 <- sample(R_M,100) #Select only 100 male students t.test(R_F_100,R_M_100) #Carry out t-test for the sample of two hundred students #Define function to compute Cohen's d: effect size cohens_d <- function(x, y) { lx <- length(x)- 1 ly <- length(y)- 1 md <- abs(mean(x) - mean(y)) ## mean difference (numerator) csd <- lx * var(x) + ly * var(y) csd <- csd/(lx + ly) csd <- sqrt(csd) ## common sd computation cd <- md/csd ## cohen's d cd } e1 <- cohens_d(R_F_100,R_M_100) e2 <- cohens_d(R_F, R_M) #Simulate drawing samples to establish the sampling distribution of the sample mean # and the sampling distribution of the sample variance samplesize <- 1000 # size of each sample NS <- 2000 #Number of samples samplemean <- rep(0,NS) # set up initial values for an array of sample means samplevar <- rep(0,NS) # set up initial values for an array of samplde variance for (r in 1:NS) { onesample <- rnorm(samplesize,0,1) samplemean[r] <- mean(onesample) samplevar[r] <- var(onesample) } hist(samplemean) hist(samplevar)