dta <- read.table("/Users/weisiang/Documents/DataManagement/week3/IQ_Beh.txt", header = T, row.names = 1)
str(dta)
## 'data.frame':    94 obs. of  3 variables:
##  $ Dep: Factor w/ 2 levels "D","N": 2 2 2 2 1 2 2 2 2 2 ...
##  $ IQ : int  103 124 124 104 96 92 124 99 92 116 ...
##  $ BP : int  4 12 9 3 3 3 6 4 3 9 ...
head(dta)
##   Dep  IQ BP
## 1   N 103  4
## 2   N 124 12
## 3   N 124  9
## 4   N 104  3
## 5   D  96  3
## 6   N  92  3
class(dta)
## [1] "data.frame"
dim(dta)
## [1] 94  3
names(dta)
## [1] "Dep" "IQ"  "BP"
is.vector(dta$BP)
## [1] TRUE
dta[1, ]
##   Dep  IQ BP
## 1   N 103  4
dta[1:3, "IQ"]
## [1] 103 124 124
tail(dta[order(dta$BP), ])
##    Dep  IQ BP
## 16   N  89 11
## 58   N 117 11
## 66   N 126 11
## 2    N 124 12
## 73   D  99 13
## 12   D  22 17
tail(dta[order(-dta$BP), ], 4)
##    Dep  IQ BP
## 77   N 124  1
## 80   N 121  1
## 24   N 106  0
## 75   N 122  0
# histogram of IQ
with(dta, hist(IQ, xlab = "IQ", main = ""))

# boxplot of behavior problem by depression status
boxplot(BP ~ Dep, data = dta, 
        xlab = "Depression", 
        ylab = "Behavior problem score")

# scatter plot
plot(IQ ~ BP, data = dta, pch = 20, col = dta$Dep, 
     xlab = "Behavior problem score", ylab = "IQ")
grid()

# two regression lines
plot(BP ~ IQ, data = dta, type = "n",
     ylab = "Behavior problem score", xlab = "IQ")
text(dta$IQ, dta$BP, labels = dta$Dep, cex = 0.5)
abline(lm(BP ~ IQ, data = dta, subset = Dep == "D"))
abline(lm(BP ~ IQ, data = dta, subset = Dep == "N"), lty = 2)

  1. Did the two groups of children have different IQ and/or behavioral problems?

經多變量分析後,發現兩群體的孩子在IQ及QP表現上達顯著差異。

options(digits = 4, show.signif.stars = T)
summary(manova(cbind(BP, IQ) ~ Dep, dta), test = "Wilks")
##           Df Wilks approx F num Df den Df Pr(>F)  
## Dep        1 0.928     3.53      2     91  0.033 *
## Residuals 92                                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. Was there any evidence of a relationship between IQ and behavioral problems?

迴歸分析發現,IQ與BP呈現負相關。IQ每增加1單位,則BP將會下降0.06792單位。

summary(lm(BP ~ IQ, data = dta))
## 
## Call:
## lm(formula = BP ~ IQ, data = dta)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -5.983 -2.356 -0.411  2.121  7.240 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  13.1828     2.0018    6.59  2.8e-09 ***
## IQ           -0.0679     0.0178   -3.81  0.00025 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.98 on 92 degrees of freedom
## Multiple R-squared:  0.136,  Adjusted R-squared:  0.127 
## F-statistic: 14.5 on 1 and 92 DF,  p-value: 0.000252