Exercise 1: ToothGrowth
Input data
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
List mean, sd and observations
## supp len
## 1 OJ 20.66333
## 2 VC 16.96333
## supp len
## 1 OJ 6.605561
## 2 VC 8.266029
## supp len
## 1 OJ 30
## 2 VC 30
t test
##
## Welch Two Sample t-test
##
## data: len by supp
## t = 1.9153, df = 55.309, p-value = 0.06063
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.1710156 7.5710156
## sample estimates:
## mean in group OJ mean in group VC
## 20.66333 16.96333
Results
Checking Normality
Exercise 2: 3 dice
create data
## [[1]]
## , , 1
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 3 4 5 6 7 8
## [2,] 4 5 6 7 8 9
## [3,] 5 6 7 8 9 10
## [4,] 6 7 8 9 10 11
## [5,] 7 8 9 10 11 12
## [6,] 8 9 10 11 12 13
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 4 5 6 7 8 9
## [2,] 5 6 7 8 9 10
## [3,] 6 7 8 9 10 11
## [4,] 7 8 9 10 11 12
## [5,] 8 9 10 11 12 13
## [6,] 9 10 11 12 13 14
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 5 6 7 8 9 10
## [2,] 6 7 8 9 10 11
## [3,] 7 8 9 10 11 12
## [4,] 8 9 10 11 12 13
## [5,] 9 10 11 12 13 14
## [6,] 10 11 12 13 14 15
##
## , , 4
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 6 7 8 9 10 11
## [2,] 7 8 9 10 11 12
## [3,] 8 9 10 11 12 13
## [4,] 9 10 11 12 13 14
## [5,] 10 11 12 13 14 15
## [6,] 11 12 13 14 15 16
##
## , , 5
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 7 8 9 10 11 12
## [2,] 8 9 10 11 12 13
## [3,] 9 10 11 12 13 14
## [4,] 10 11 12 13 14 15
## [5,] 11 12 13 14 15 16
## [6,] 12 13 14 15 16 17
##
## , , 6
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 8 9 10 11 12 13
## [2,] 9 10 11 12 13 14
## [3,] 10 11 12 13 14 15
## [4,] 11 12 13 14 15 16
## [5,] 12 13 14 15 16 17
## [6,] 13 14 15 16 17 18
the histogram plot and it’s just a probability histogram, not an empirical histogram.
Exercise 3: IQ and behavior problems
Load file
Show data structure
## '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 ...
List the first 6 rows
## 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
Show the last 6 row of data which ordered from large to small by column 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
Show the last 4 row of data which order from small to large by column BP
## Dep IQ BP
## 77 N 124 1
## 80 N 121 1
## 24 N 106 0
## 75 N 122 0
Plot a boxplot, x axis as depressive or no depressive group, y axis as Behavior problem score
Plot a scatter plot of Behavior problem score (as x axis) and IQ (as y axis)
plot(IQ ~ BP, data = dta, pch = 20, col = dta$Dep,
xlab = "Behavior problem score", ylab = "IQ")
grid()First, create a plot space.
Plot the data points as text (depressive as D and no depressive as N)
Plot the regression line of Depressive
Plot the regression line of no depressive
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)Exercise 4: US Births at 2015
Load data file
create a list of the Season, then summarize the number of births by season
Season <- c("Spring","Spring","Spring","Summer","Summer","Summer","Autumn","Autumn","Autumn","Winter","Winter","Winter")
dta$season <- Season
aggregate(birth ~ season, mean, data=dta)## season birth
## 1 Autumn 350907.3
## 2 Spring 317645.3
## 3 Summer 326430.0
## 4 Winter 331183.0
BTW, I think it’s not a good way to add season list.
Exercise 5:
Load file
Calculate mean reading time by each subjects
Show the rank by processing speed
dta <- read.table("/Users/haolunfu/Documents/資料管理/week3/readingtimes.txt", header = T)
dtac <- dta[,5:14]
dtacm <- apply(dtac, 2, mean)
order(dtacm)## [1] 9 3 4 2 1 10 8 7 6 5
Estimate, on average, how long does it take to read a word
## S01 S02 S03 S04 S05 S06 S07 S08
## 0.3563579 0.3218649 0.3285283 0.3283256 0.4939310 0.4616710 0.4297786 0.4474266
## S09 S10
## 0.2205573 0.3949539