Clustered & longitudinal data
A total of 84 individual clients was treated by 17 therapists using either cognitive-behavioral or interpersonal therapy. The Beck Depression Inventory (BDI) was used to assess depression.
Source: Kenny, D.A., & Hoyt, W. (2009). Multiple levels of analysis in psychotherapy research, Psychotherapy Research, 19, 462-468.
# tools
#install.packages("dplyr")
require(dplyr) #library()和require()都可以載入套件,差別是在函數中,如果一個package不存在,執行到library將會出現error,require則會繼續執行。## 載入需要的套件:dplyr## 
## 載入套件:'dplyr'## 下列物件被遮斷自 'package:stats':
## 
##     filter, lag## 下列物件被遮斷自 'package:base':
## 
##     intersect, setdiff, setequal, union# input data
dta <- read.csv("http://davidakenny.net/papers/k&h/nimh.csv", header = T, row.names = 1)# check data structure
str(dta)## 'data.frame':    120 obs. of  8 variables:
##  $ patid    : int  1329 1349 1428 1502 1520 1342 1368 1401 1424 1460 ...
##  $ p_gender : int  1 2 2 2 2 1 1 2 2 1 ...
##  $ ypre     : int  26 31 24 25 24 19 41 14 20 12 ...
##  $ y        : int  1 4 NA 4 4 2 1 NA NA 13 ...
##  $ therapist: int  101 101 101 101 101 102 102 102 102 102 ...
##  $ t_gender : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ bdi_gain : num  -7.69 -6.22 NA -4.39 -4.08 ...
##  $ t        : chr  "IPT" "IPT" "IPT" "IPT" ...# keep only 4 col. of variables
dta <- select(dta, c(1, 4, 5, 8))# rename variables 
colnames(dta) <- c("Client", "BDI", "Therapist", "Treatment")# alter data type and removing missing observations
dta <- mutate(dta, Therapist = factor(Therapist)) %>%
       filter( BDI != "NA")# examine first 6 lines
head(dta)##   Client BDI Therapist Treatment
## 1   1329   1       101       IPT
## 2   1349   4       101       IPT
## 3   1502   4       101       IPT
## 4   1520   4       101       IPT
## 5   1342   2       102       IPT
## 6   1368   1       102       IPT# load package
#install.packages("ggplot2")
#install.packages("ggplot")
require(ggplot2)## 載入需要的套件:ggplot2require(ggplot)## 載入需要的套件:ggplot## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : 不存在叫 'ggplot' 這個名稱的套件# plot
ggplot(dta, aes(y = BDI, x = reorder(Therapist, BDI, mean))) +
  geom_point(color = "cyan3") +
  facet_grid(Treatment ~ .) +
  labs(x = "Therapist ID", y = "Beck Depression Inventory") +
  theme_bw()#The End