This data is a random slice of some data I am working with at the moment that is similar to yours. Note that this data has no missind data but yours likely does.
# read in data
data <- read.table("/Users/phparker/Desktop/example.txt")
# look at number of people in treatment and control groups
table(data$group, dnn = "group")
## group
## 0 1
## 58 22
names(data)
## [1] "group" "T1stress" "T1tiring" "T1importance"
## [5] "T1commitment" "T2stress" "T2tiring" "T2importance"
## [9] "T2commitment"
Here we use apply to the function mean for each row(participant) for the items in each scale. na.rm=TRUE will calculate a mean even if the participant misses one or more questions.We assign this (<- means assignment) to a newvariable/column in our dataset.
# T1 - Pre treatment
data$T1goal.strain <- apply(data[, c("T1stress", "T1tiring")], 1, mean, na.rm = TRUE)
data$T1goal.commit <- apply(data[, c("T1importance", "T1commitment")], 1, mean,
na.rm = TRUE)
# T2 - Post treatment
data$T2goal.strain <- apply(data[, c("T2stress", "T2tiring")], 1, mean, na.rm = TRUE)
data$T2goal.commit <- apply(data[, c("T2importance", "T2commitment")], 1, mean,
na.rm = TRUE)
Before doing statistical analysis it is critical to look at the data. I will start with some scatter plots for the pre vs post variables and then some so-called boxplots plots for each variable across the two groups.
library(lattice)
# scatter plots
plot(data$T1goal.strain, data$T2goal.strain, ylab = "Pre", xlab = "Post", main = "Goal Strain")
plot(data$T1goal.commit, data$T2goal.commit, ylab = "Pre", xlab = "Post", main = "Goal Commit")
# pre test plots
bwplot(group ~ T1goal.strain, ylab = "Group", xlab = "Goal Strain", main = "Pre test",
data = data)
bwplot(group ~ T1goal.commit, ylab = "Group", xlab = "Goal Commitment", main = "Pre test",
data = data)
# post test plots
bwplot(group ~ T2goal.strain, ylab = "Group", xlab = "Goal Strain", main = "Post test",
data = data)
bwplot(group ~ T2goal.commit, ylab = "Group", xlab = "Goal Commitment", main = "Post test",
data = data)
# Pre test
t.test(T1goal.strain ~ group, data = data)
##
## Welch Two Sample t-test
##
## data: T1goal.strain by group
## t = -0.2222, df = 41.21, p-value = 0.8252
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.7345 0.5889
## sample estimates:
## mean in group 0 mean in group 1
## 4.995 5.068
t.test(T1goal.commit ~ group, data = data)
##
## Welch Two Sample t-test
##
## data: T1goal.commit by group
## t = 1.002, df = 29.91, p-value = 0.3242
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.2232 0.6534
## sample estimates:
## mean in group 0 mean in group 1
## 6.056 5.841
# Post test
t.test(T2goal.strain ~ group, data = data)
##
## Welch Two Sample t-test
##
## data: T2goal.strain by group
## t = 2.257, df = 33.32, p-value = 0.0307
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.08476 1.63033
## sample estimates:
## mean in group 0 mean in group 1
## 5.191 4.334
t.test(T2goal.commit ~ group, data = data)
##
## Welch Two Sample t-test
##
## data: T2goal.commit by group
## t = 0.5684, df = 35.19, p-value = 0.5734
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.3084 0.5482
## sample estimates:
## mean in group 0 mean in group 1
## 6.057 5.937
# Model for Goal Strain
M_GS <- lm(T2goal.strain ~ as.factor(group) + T1goal.strain, data = data)
# check assumptions visually
plot(M_GS)
# see results
summary(M_GS)
##
## Call:
## lm(formula = T2goal.strain ~ as.factor(group) + T1goal.strain,
## data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.546 -0.865 0.024 1.042 3.201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.003 0.603 6.64 4e-09 ***
## as.factor(group)1 -0.875 0.347 -2.52 0.014 *
## T1goal.strain 0.238 0.115 2.07 0.042 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.38 on 77 degrees of freedom
## Multiple R-squared: 0.119, Adjusted R-squared: 0.096
## F-statistic: 5.19 on 2 and 77 DF, p-value: 0.00765
# Model for Goal Commit
M_GC <- lm(T2goal.commit ~ as.factor(group) + T1goal.commit, data = data)
# check assumptions visually
plot(M_GC)
# see results
summary(M_GC)
##
## Call:
## lm(formula = T2goal.commit ~ as.factor(group) + T1goal.commit,
## data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.869 -0.515 0.186 0.688 1.423
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.4194 0.7337 6.02 5.5e-08 ***
## as.factor(group)1 -0.0618 0.1993 -0.31 0.757
## T1goal.commit 0.2704 0.1199 2.25 0.027 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.789 on 77 degrees of freedom
## Multiple R-squared: 0.0661, Adjusted R-squared: 0.0419
## F-statistic: 2.73 on 2 and 77 DF, p-value: 0.0718