IQ and Behavior Problem

設定路徑以及讀取資料
setwd("/Users/tayloryen/Desktop/大學/成大課業/大四下/資料管理/0312/HW/hw06")
dta <- read.table("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"
查看變項“BP”是否為向量格式
is.vector(dta$BP)
## [1] TRUE
選取資料第一列
dta[1, ]
##   Dep  IQ BP
## 1   N 103  4
選資料中IQ變相的第一到第三項
dta[1:3, "IQ"]
## [1] 103 124 124
查看BP最少的四筆
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
畫直方圖
with(dta, hist(IQ, xlab = "IQ", main = ""))

畫箱形圖
boxplot(BP ~ Dep, data = dta, 
        xlab = "Depression", 
        ylab = "Behavior problem score")

畫散點圖
plot(IQ ~ BP, data = dta, pch = 20, col = dta$Dep, 
     xlab = "Behavior problem score", ylab = "IQ")
grid()

畫兩條回歸線
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)

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

兩組小孩在IQ表現上有顯著差異,但在BP上面沒有顯著差異

options(digits = 4, show.signif.stars = F)
summary(aov(IQ~Dep,data=dta))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dep          1   1731    1731    6.07  0.016
## Residuals   92  26238     285
options(digits = 4, show.signif.stars = F)
summary(aov(BP~Dep,data=dta))
##             Df Sum Sq Mean Sq F value Pr(>F)
## Dep          1     33    32.6    3.28  0.074
## Residuals   92    915     9.9

Was there any evidence of a relationship between IQ and behavioral problems?

從迴歸係數了解到「IQ每增加一個單位,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
## 
## 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

The End