1 data input

# input data
dta <- read.table("adas.txt", h = T, na.strings='.')
# see the data structure
str(dta)
## 'data.frame':    80 obs. of  8 variables:
##  $ Treatment: chr  "L" "L" "L" "L" ...
##  $ PID      : int  1 5 8 12 13 15 19 21 24 28 ...
##  $ adas02   : int  22 34 40 24 29 31 22 43 18 25 ...
##  $ adas04   : int  30 35 41 NA 26 36 27 49 28 24 ...
##  $ adas06   : int  NA 46 41 21 29 41 28 42 29 27 ...
##  $ adas08   : int  33 37 46 28 26 46 24 48 NA 18 ...
##  $ adas10   : int  28 31 52 30 NA 52 27 48 25 21 ...
##  $ adas12   : int  30 35 48 27 36 57 28 46 28 22 ...
# show the first six data
head(dta)
##   Treatment PID adas02 adas04 adas06 adas08 adas10 adas12
## 1         L   1     22     30     NA     33     28     30
## 2         L   5     34     35     46     37     31     35
## 3         L   8     40     41     41     46     52     48
## 4         L  12     24     NA     21     28     30     27
## 5         L  13     29     26     29     26     NA     36
## 6         L  15     31     36     41     46     52     57

2 table

pacman::p_load(dplyr, furniture)

dta %>% 
  dplyr::group_by(Treatment) %>%
  dplyr::select(starts_with("adas")) %>%
  furniture::table1(digits=2, total=FALSE, test=F, output="html")
## Adding missing grouping variables: `Treatment`
## Using dplyr::group_by() groups: Treatment
H L P
n = 18 n = 17 n = 21
adas02
30.22 (8.56) 32.82 (7.51) 29.62 (6.78)
adas04
32.44 (7.33) 35.12 (8.64) 33.43 (6.42)
adas06
33.56 (8.56) 37.65 (9.09) 34.86 (6.43)
adas08
33.33 (9.06) 36.41 (11.09) 36.38 (6.34)
adas10
33.72 (7.44) 38.35 (11.19) 37.57 (7.03)
adas12
34.28 (7.10) 39.18 (10.77) 39.05 (8.80)

3 cor

knitr::kable(cor(dta[,3:8], use="pair"))
adas02 adas04 adas06 adas08 adas10 adas12
adas02 1.0000000 0.8954000 0.7544220 0.7740050 0.7006653 0.7496129
adas04 0.8954000 1.0000000 0.8144181 0.7936916 0.7006978 0.7400989
adas06 0.7544220 0.8144181 1.0000000 0.8628966 0.6716075 0.7615946
adas08 0.7740050 0.7936916 0.8628966 1.0000000 0.8472388 0.8337049
adas10 0.7006653 0.7006978 0.6716075 0.8472388 1.0000000 0.9153379
adas12 0.7496129 0.7400989 0.7615946 0.8337049 0.9153379 1.0000000

#scatter plot matrix

library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
scatterplotMatrix(~ adas02 + adas04 + adas06 + adas08 + adas10 + adas12, 
                  data=dta, col=8,
                  smooth=FALSE, regLine=FALSE,
                  ellipse=list(levels=c(.975),
                               fill=TRUE, 
                               fill.alpha=.1))

4 Wide to long format

dta2<-read.table("adas.txt", h=T,na.strings='.')
head(dta2)
##   Treatment PID adas02 adas04 adas06 adas08 adas10 adas12
## 1         L   1     22     30     NA     33     28     30
## 2         L   5     34     35     46     37     31     35
## 3         L   8     40     41     41     46     52     48
## 4         L  12     24     NA     21     28     30     27
## 5         L  13     29     26     29     26     NA     36
## 6         L  15     31     36     41     46     52     57
dta2 <- mutate(dta2, 
               Treatment = factor(Treatment),
               PID = factor(PID),
               Baseline = adas02)

colnames(dta2)<-c("Treatment", "PID", "2", "4", "6", "8", "10" , "12", "Baseline")
str(dta2)
## 'data.frame':    80 obs. of  9 variables:
##  $ Treatment: Factor w/ 3 levels "H","L","P": 2 2 2 2 2 2 2 2 2 2 ...
##  $ PID      : Factor w/ 80 levels "1","2","3","4",..: 1 5 8 12 13 15 19 21 24 28 ...
##  $ 2        : int  22 34 40 24 29 31 22 43 18 25 ...
##  $ 4        : int  30 35 41 NA 26 36 27 49 28 24 ...
##  $ 6        : int  NA 46 41 21 29 41 28 42 29 27 ...
##  $ 8        : int  33 37 46 28 26 46 24 48 NA 18 ...
##  $ 10       : int  28 31 52 30 NA 52 27 48 25 21 ...
##  $ 12       : int  30 35 48 27 36 57 28 46 28 22 ...
##  $ Baseline : int  22 34 40 24 29 31 22 43 18 25 ...
library(tidyr)
dta2L <- gather(dta2, Time, adas, "2":"12")

dta2L <- mutate(dta2L,
                Baseline  = as.numeric(Baseline),
                adas = as.numeric(adas),
                Time_f = factor(as.numeric(Time)-2),
                Time = factor (Time))

str(dta2L)
## 'data.frame':    480 obs. of  6 variables:
##  $ Treatment: Factor w/ 3 levels "H","L","P": 2 2 2 2 2 2 2 2 2 2 ...
##  $ PID      : Factor w/ 80 levels "1","2","3","4",..: 1 5 8 12 13 15 19 21 24 28 ...
##  $ Baseline : num  22 34 40 24 29 31 22 43 18 25 ...
##  $ Time     : Factor w/ 6 levels "10","12","2",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ adas     : num  22 34 40 24 29 31 22 43 18 25 ...
##  $ Time_f   : Factor w/ 6 levels "0","2","4","6",..: 1 1 1 1 1 1 1 1 1 1 ...

5 line chart

library(ggplot2)
p <- ggplot(dta2L, aes(Time_f , adas, 
                       group = Treatment, 
                       linetype = Treatment, 
                       shape = Treatment)) +
  stat_summary(fun = mean, geom = "line") +
  stat_summary(fun = mean, geom = "point") +
  stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2) +
  scale_shape_manual(values=c(1, 2, 7)) +
  scale_y_continuous(breaks=seq(20, 60, 10))+
  labs(x = "Time", y = "ADAS", linetype = "Treatment", shape = "Treatment") +
  theme_minimal() +
  theme(legend.position=c(.15,.85))
suppressWarnings(suppressMessages(print(p)))