data_string <- "OBS ID Y TIME
1 1 26.5 0
2 1 14.8 1
3 1 19.5 4
4 1 21.0 6
5 2 25.8 0
6 2 23.0 1
7 2 19.1 4
8 2 23.2 6
9 3 20.4 0
10 3 2.8 1
11 3 3.2 4
12 3 9.4 6"
df <- read.table(textConnection(data_string), header = TRUE)
print(df)
## OBS ID Y TIME
## 1 1 1 26.5 0
## 2 2 1 14.8 1
## 3 3 1 19.5 4
## 4 4 1 21.0 6
## 5 5 2 25.8 0
## 6 6 2 23.0 1
## 7 7 2 19.1 4
## 8 8 2 23.2 6
## 9 9 3 20.4 0
## 10 10 3 2.8 1
## 11 11 3 3.2 4
## 12 12 3 9.4 6
#Summary Statistics by Time
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df_summary<-df%>%
group_by(TIME)%>%
summarize(mean_Y=mean(Y))
print(df_summary)
## # A tibble: 4 × 2
## TIME mean_Y
## <int> <dbl>
## 1 0 24.2
## 2 1 13.5
## 3 4 13.9
## 4 6 17.9
#Summary Statistics by ID
df_summary_2<-df%>%
group_by(ID)%>%
summarize(mean_Y=mean(Y))
print(df_summary_2)
## # A tibble: 3 × 2
## ID mean_Y
## <int> <dbl>
## 1 1 20.4
## 2 2 22.8
## 3 3 8.95
library(dplyr)
# Summarize the data to get the mean of 'Y' for each 'ID'
df_summary_2 <- df %>%
group_by(ID) %>%
summarize(mean_Y = mean(Y))
# Create a boxplot of the mean_Y values
boxplot(Y ~ ID, data = df, main = "Boxplot of Mean Y by ID", ylab = "Mean Y",col=c('red','green','black'))
#Conduct Normality test
# Conduct Shapiro-Wilk test for normality for each group defined by ID
shapiro_results <- by(df$Y, df$ID, shapiro.test)
# Output the results
print(shapiro_results)
## df$ID: 1
##
## Shapiro-Wilk normality test
##
## data: dd[x, ]
## W = 0.98569, p-value = 0.9345
##
## ------------------------------------------------------------
## df$ID: 2
##
## Shapiro-Wilk normality test
##
## data: dd[x, ]
## W = 0.93983, p-value = 0.6533
##
## ------------------------------------------------------------
## df$ID: 3
##
## Shapiro-Wilk normality test
##
## data: dd[x, ]
## W = 0.85228, p-value = 0.2336
##Variance test
# Conduct Bartlett's test for homogeneity of variances for each group defined by ID
# Conduct Bartlett's test for homogeneity of variances
variance_results <- bartlett.test(Y ~ ID, data = df)
# Output the results
print(variance_results)
##
## Bartlett test of homogeneity of variances
##
## data: Y by ID
## Bartlett's K-squared = 2.8185, df = 2, p-value = 0.2443
#Conduct Anova
model=lm(Y~factor(TIME),data=df)
summary_model=summary(model)
summary_model
##
## Call:
## lm(formula = Y ~ factor(TIME), data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.733 -4.992 1.917 5.208 9.467
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 24.233 4.617 5.249 0.000775 ***
## factor(TIME)1 -10.700 6.529 -1.639 0.139883
## factor(TIME)4 -10.300 6.529 -1.578 0.153319
## factor(TIME)6 -6.367 6.529 -0.975 0.358058
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.996 on 8 degrees of freedom
## Multiple R-squared: 0.3023, Adjusted R-squared: 0.04066
## F-statistic: 1.155 on 3 and 8 DF, p-value: 0.3847
# Perform one-way ANOVA
anova_result <- aov(Y ~ factor(TIME), data = df)
# Print the ANOVA summary
sanova<-summary(anova_result)
sanova
## Df Sum Sq Mean Sq F value Pr(>F)
## factor(TIME) 3 221.6 73.88 1.155 0.385
## Residuals 8 511.5 63.94