Repeated Measures Design : Adjacent Differences
################################################################################
## General Linear Hypothesis - Repeated Measures Design : Adjacent Differences
################################################################################
# Loading the original data
memoryraw <- read.csv("memory.csv")
summary(memoryraw)
## id Trial1 Trial2 Trial3
## Min. : 1.00 Min. :12.00 Min. :11.0 Min. :11.00
## 1st Qu.: 3.25 1st Qu.:13.00 1st Qu.:14.0 1st Qu.:12.50
## Median : 5.50 Median :15.50 Median :15.0 Median :15.00
## Mean : 5.50 Mean :15.40 Mean :15.4 Mean :15.30
## 3rd Qu.: 7.75 3rd Qu.:17.75 3rd Qu.:18.0 3rd Qu.:17.75
## Max. :10.00 Max. :19.00 Max. :20.0 Max. :20.00
# Subsetting data to only include the necessary columns
memory <- memoryraw[,colnames(memoryraw) %in% c("Trial1","Trial2","Trial3")]
summary(memory)
## Trial1 Trial2 Trial3
## Min. :12.00 Min. :11.0 Min. :11.00
## 1st Qu.:13.00 1st Qu.:14.0 1st Qu.:12.50
## Median :15.50 Median :15.0 Median :15.00
## Mean :15.40 Mean :15.4 Mean :15.30
## 3rd Qu.:17.75 3rd Qu.:18.0 3rd Qu.:17.75
## Max. :19.00 Max. :20.0 Max. :20.00
# Obtaining the values for n (number of observations) and p(number of parameters)
(n <- nrow(memory))
## [1] 10
(p <- ncol(memory))
## [1] 3
# Generating the sample mean vector
(memory_means <- colMeans(memory))
## Trial1 Trial2 Trial3
## 15.4 15.4 15.3
(memory_means <- as.data.frame(memory_means))
## memory_means
## Trial1 15.4
## Trial2 15.4
## Trial3 15.3
# Generating the sample variance vector
(memory_var <- sapply(memory,function(x)var(x)))
## Trial1 Trial2 Trial3
## 7.822222 9.377778 9.788889
(memory_var <- as.data.frame(memory_var))
## memory_var
## Trial1 7.822222
## Trial2 9.377778
## Trial3 9.788889
# Using manually generated function
ADiffT2Test <- function(nxpdata){
nxpdata <- as.matrix(nxpdata)
n <- nrow(nxpdata)
p <- ncol(nxpdata)
samplemean <- colMeans(nxpdata)
samplevar <- cov(nxpdata)
# Automating the creation of the A Matrix
pstart <-1
Amatrix <- NULL
while (pstart<p) {
Amatrixrow <- rep(0,p)
Amatrixrow[pstart] <- 1
Amatrixrow[pstart+1] <- -1
Amatrix <-as.matrix(rbind(Amatrix,Amatrixrow))
pstart=pstart+1
}
Amatrix
T2 <- n * t(Amatrix%*%samplemean) %*% solve(Amatrix%*%samplevar%*%t(Amatrix)) %*% (Amatrix%*%samplemean)
Fstat <- ((n-p+1)*T2)/((p-1)*(n-1))
pval <- 1-pf(Fstat,df1=p-1,df2=n-p+1)
return(data.frame(T2=as.numeric(T2),
T2df1 = p-1,
T2df2 = n-1,
Fstat=as.numeric(Fstat),
Fdf1=p-1,
Fdf2=n-p+1,
p.value=formatC((as.numeric(pval)),format="e",digits=2),
row.names=""))
}
(memory_ADiffT2TestRFunction <- ADiffT2Test(memory))
## T2 T2df1 T2df2 Fstat Fdf1 Fdf2 p.value
## 0.08320493 2 9 0.03697997 2 8 9.64e-01
Repeated Measures Design : Common Comparison
################################################################################
## General Linear Hypothesis - Repeated Measures Design : Common Comparison
################################################################################
# Loading the original data
memoryraw <- read.csv("memory.csv")
summary(memoryraw)
## id Trial1 Trial2 Trial3
## Min. : 1.00 Min. :12.00 Min. :11.0 Min. :11.00
## 1st Qu.: 3.25 1st Qu.:13.00 1st Qu.:14.0 1st Qu.:12.50
## Median : 5.50 Median :15.50 Median :15.0 Median :15.00
## Mean : 5.50 Mean :15.40 Mean :15.4 Mean :15.30
## 3rd Qu.: 7.75 3rd Qu.:17.75 3rd Qu.:18.0 3rd Qu.:17.75
## Max. :10.00 Max. :19.00 Max. :20.0 Max. :20.00
# Subsetting data to only include the necessary columns
memory <- memoryraw[,colnames(memoryraw) %in% c("Trial1","Trial2","Trial3")]
summary(memory)
## Trial1 Trial2 Trial3
## Min. :12.00 Min. :11.0 Min. :11.00
## 1st Qu.:13.00 1st Qu.:14.0 1st Qu.:12.50
## Median :15.50 Median :15.0 Median :15.00
## Mean :15.40 Mean :15.4 Mean :15.30
## 3rd Qu.:17.75 3rd Qu.:18.0 3rd Qu.:17.75
## Max. :19.00 Max. :20.0 Max. :20.00
# Obtaining the values for n (number of observations) and p(number of parameters)
(n <- nrow(memory))
## [1] 10
(p <- ncol(memory))
## [1] 3
# Generating the sample mean vector
(memory_means <- colMeans(memory))
## Trial1 Trial2 Trial3
## 15.4 15.4 15.3
(memory_means <- as.data.frame(memory_means))
## memory_means
## Trial1 15.4
## Trial2 15.4
## Trial3 15.3
# Generating the sample variance vector
(memory_var <- sapply(memory,function(x)var(x)))
## Trial1 Trial2 Trial3
## 7.822222 9.377778 9.788889
(memory_var <- as.data.frame(memory_var))
## memory_var
## Trial1 7.822222
## Trial2 9.377778
## Trial3 9.788889
# Using manually generated function
CCompT2Test <- function(nxpdata){
nxpdata <- as.matrix(nxpdata)
n <- nrow(nxpdata)
p <- ncol(nxpdata)
samplemean <- colMeans(nxpdata)
samplevar <- cov(nxpdata)
# Automating the creation of the A Matrix
pcontrol <- 3
Amatrixcontrol <- rep(-1,p-1)
pstart <-1
Amatrix <- NULL
while (pstart<p) {
Amatrixcol <- rep(0,p-1)
Amatrixcol[pstart] <- 1
Amatrix <-as.matrix(cbind(Amatrix,Amatrixcol))
pstart=pstart+1
}
if(pcontrol==1) {
Amatrix <-as.matrix(cbind(Amatrixcontrol,Amatrix))
} else if ( pcontrol==p ) {
Amatrix <-as.matrix(cbind(Amatrix,Amatrixcontrol))
} else {
Amatrix <-as.matrix(cbind(Amatrix[,1:pcontrol-1],Amatrixcontrol,Amatrix[,pcontrol:(p-1)]))
}
T2 <- n * t(Amatrix%*%samplemean) %*% solve(Amatrix%*%samplevar%*%t(Amatrix)) %*% (Amatrix%*%samplemean)
Fstat <- ((n-p+1)*T2)/((p-1)*(n-1))
pval <- 1-pf(Fstat,df1=p-1,df2=n-p+1)
return(data.frame(T2=as.numeric(T2),
T2df1 = p-1,
T2df2 = n-1,
Fstat=as.numeric(Fstat),
Fdf1=p-1,
Fdf2=n-p+1,
p.value=formatC((as.numeric(pval)),format="e",digits=2),
row.names=""))
}
(memory_CCompT2TestRFunction <- CCompT2Test(memory))
## T2 T2df1 T2df2 Fstat Fdf1 Fdf2 p.value
## 0.08320493 2 9 0.03697997 2 8 9.64e-01
Test For Symmetry
################################################################################
## General Linear Hypothesis - Test For Symmetry
################################################################################
# Loading the original data
coupleraw <- read.csv("couple.csv")
summary(coupleraw)
## id Q1H Q2H Q3H
## Min. : 1.00 Min. :2.00 Min. :3.000 Min. :3.000
## 1st Qu.: 8.25 1st Qu.:3.25 1st Qu.:4.000 1st Qu.:3.000
## Median :15.50 Median :4.00 Median :4.000 Median :4.000
## Mean :15.50 Mean :3.90 Mean :4.333 Mean :3.967
## 3rd Qu.:22.75 3rd Qu.:4.00 3rd Qu.:5.000 3rd Qu.:4.750
## Max. :30.00 Max. :5.00 Max. :5.000 Max. :5.000
## Q4H Q1W Q2W Q3W
## Min. :3.0 Min. :2.000 Min. :4.000 Min. :3.00
## 1st Qu.:4.0 1st Qu.:3.250 1st Qu.:4.000 1st Qu.:4.00
## Median :4.5 Median :4.000 Median :5.000 Median :4.00
## Mean :4.4 Mean :3.833 Mean :4.633 Mean :4.10
## 3rd Qu.:5.0 3rd Qu.:4.000 3rd Qu.:5.000 3rd Qu.:4.75
## Max. :5.0 Max. :5.000 Max. :5.000 Max. :5.00
## Q4W
## Min. :4.000
## 1st Qu.:4.000
## Median :5.000
## Mean :4.533
## 3rd Qu.:5.000
## Max. :5.000
# Subsetting data to only include the necessary columns
couple <- coupleraw[,colnames(coupleraw) %in% c("Q1H","Q2H","Q3H","Q4H","Q1W","Q2W","Q3W","Q4W")]
summary(couple)
## Q1H Q2H Q3H Q4H
## Min. :2.00 Min. :3.000 Min. :3.000 Min. :3.0
## 1st Qu.:3.25 1st Qu.:4.000 1st Qu.:3.000 1st Qu.:4.0
## Median :4.00 Median :4.000 Median :4.000 Median :4.5
## Mean :3.90 Mean :4.333 Mean :3.967 Mean :4.4
## 3rd Qu.:4.00 3rd Qu.:5.000 3rd Qu.:4.750 3rd Qu.:5.0
## Max. :5.00 Max. :5.000 Max. :5.000 Max. :5.0
## Q1W Q2W Q3W Q4W
## Min. :2.000 Min. :4.000 Min. :3.00 Min. :4.000
## 1st Qu.:3.250 1st Qu.:4.000 1st Qu.:4.00 1st Qu.:4.000
## Median :4.000 Median :5.000 Median :4.00 Median :5.000
## Mean :3.833 Mean :4.633 Mean :4.10 Mean :4.533
## 3rd Qu.:4.000 3rd Qu.:5.000 3rd Qu.:4.75 3rd Qu.:5.000
## Max. :5.000 Max. :5.000 Max. :5.00 Max. :5.000
# Obtaining the values for n (number of observations) and p(number of parameters)
(n <- nrow(couple))
## [1] 30
(p <- ncol(couple)/2)
## [1] 4
# Generating the sample mean vector
(couple_means <- colMeans(couple))
## Q1H Q2H Q3H Q4H Q1W Q2W Q3W Q4W
## 3.900000 4.333333 3.966667 4.400000 3.833333 4.633333 4.100000 4.533333
(couple_means <- as.data.frame(couple_means))
## couple_means
## Q1H 3.900000
## Q2H 4.333333
## Q3H 3.966667
## Q4H 4.400000
## Q1W 3.833333
## Q2W 4.633333
## Q3W 4.100000
## Q4W 4.533333
# Generating the sample variance vector
(couple_var <- sapply(couple,function(x)var(x)))
## Q1H Q2H Q3H Q4H Q1W Q2W Q3W
## 0.5758621 0.4367816 0.5850575 0.4551724 0.4885057 0.2402299 0.4379310
## Q4W
## 0.2574713
(couple_var <- as.data.frame(couple_var))
## couple_var
## Q1H 0.5758621
## Q2H 0.4367816
## Q3H 0.5850575
## Q4H 0.4551724
## Q1W 0.4885057
## Q2W 0.2402299
## Q3W 0.4379310
## Q4W 0.2574713
# Using manually generated function
TSymmT2Test <- function(nxpdata){
nxpdata <- as.matrix(nxpdata)
n <- nrow(nxpdata)
p <- ncol(nxpdata)/2
samplemean <- colMeans(nxpdata)
samplevar <- cov(nxpdata)
# Automating the creation of the A Matrix
pstart <-1
Amatrix <- NULL
while (pstart<(p+1)) {
Amatrixrow <- rep(0,2*p)
Amatrixrow[pstart] <- 1
Amatrixrow[pstart+p] <- -1
Amatrix <-as.matrix(rbind(Amatrix,Amatrixrow))
pstart=pstart+1
}
T2 <- n * t(Amatrix%*%samplemean) %*% solve(Amatrix%*%samplevar%*%t(Amatrix)) %*% (Amatrix%*%samplemean)
Fstat <- ((n-p)*T2)/((p)*(n-1))
pval <- 1-pf(Fstat,df1=p,df2=n-p)
return(data.frame(T2=as.numeric(T2),
T2df1 = p,
T2df2 = n-1,
Fstat=as.numeric(Fstat),
Fdf1=p,
Fdf2=n-p,
p.value=formatC((as.numeric(pval)),format="e",digits=2),
row.names=""))
}
(couple_TSymmT2TestRFunction <- TSymmT2Test(couple))
## T2 T2df1 T2df2 Fstat Fdf1 Fdf2 p.value
## 13.12784 4 29 2.942447 4 26 3.94e-02