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"
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)
由上圖可發現2組有差異
summary(lm(BP ~ IQ, data = dta))
##
## Call:
## lm(formula = BP ~ IQ, data = dta)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.9828 -2.3564 -0.4111 2.1210 7.2399
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.18280 2.00180 6.585 2.76e-09 ***
## IQ -0.06792 0.01783 -3.809 0.000252 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.983 on 92 degrees of freedom
## Multiple R-squared: 0.1362, Adjusted R-squared: 0.1268
## F-statistic: 14.51 on 1 and 92 DF, p-value: 0.0002518