Exercises on Slide 21-22

Ex. 1 Create a vector of the positive odd integers less than 100

x <- seq(from=1,to=99, by=2)
x
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99

Ex. 2 Remove the values greater than 60 and less than 80.

y <- x[(x<=60)|(x>=80)]
y
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 81 83 85 87 89 91 93 95 97 99

Ex. 3 Find the variance of the remaining set of values.

var(y)
## [1] 931.2821

Ex. 4 Create a data frame called cone with two elements:

R <- c(2.27, 1.98, 1.69, 1.88, 1.64, 2.14)
H <- c(8.28, 8.04, 9.06, 8.70, 7.58, 8.34)

Recall the volume of a cone with radius \(R\) and height \(H\) is given by \(\frac{1}{3} \pi R^2 H\). Make the third elmement as \(V\), which is the volume of the cone.

cone <- data.frame(R=R,H=H)
cone$V <- pi*(R^2)*H/3
cone
##      R    H        V
## 1 2.27 8.28 44.67974
## 2 1.98 8.04 33.00768
## 3 1.69 9.06 27.09756
## 4 1.88 8.70 32.20057
## 5 1.64 7.58 21.34939
## 6 2.14 8.34 39.99652

Ex. 5 Find Find the intercept and the slope for the following data using the matrix formula \((X^\prime X)^{-1}X^\prime y\).

x <- c(3.15, -0.55, -0.35, 0.16)
y <- c(2.93, -0.35, -0.25, -0.12)
lm(y~x)
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##    0.001483     0.914551
lsfit(x,y)$coef
##   Intercept           X 
## 0.001483229 0.914550657
X <- cbind(1,x)
X
##            x
## [1,] 1  3.15
## [2,] 1 -0.55
## [3,] 1 -0.35
## [4,] 1  0.16
solve(t(X)%*%(X))%*%t(X)%*%y
##          [,1]
##   0.001483229
## x 0.914550657

Ex. 6 In the R package MASS there is a dataset called cats. Run the following commands. Have a look at the dataset. The variables Bwt and Hwt give the weight of the body (kg) and the heart (g), respectively.

library(MASS)
## Warning: package 'MASS' was built under R version 3.1.3
data(cats)
head(cats)
##   Sex Bwt Hwt
## 1   F 2.0 7.0
## 2   F 2.0 7.4
## 3   F 2.0 9.5
## 4   F 2.1 7.2
## 5   F 2.1 7.3
## 6   F 2.1 7.6

Ex. 7 Find the numbers of male and female cats in the dataset by using the table function.

attach(cats)
table(Sex)
## Sex
##  F  M 
## 47 97

Ex. 8 Compare the means and standard deviations of the body weights for male and female cats.

mean(cats[Sex=='F',3])
## [1] 9.202128
sd(cats[Sex=='F',3])
## [1] 1.357666

Ex. 9 Find the proportions of male and female cats whose body weights are over 2.5 kilograms.

Ex. 10 Create two lists (catsM and catsF) that consist only male and females cats in the data.

catsM <- cats[Sex=='M',]
catsF <- cats[Sex=='F',]
length(which(catsM$Bwt>2.5))/nrow(catsM)
## [1] 0.742268
length(which(catsF$Bwt>2.5))/nrow(catsF)
## [1] 0.2340426

Exercises on Slide 21-22

I only show the binomial case below. The code for normal and exponential distributions is similar.

set.seed(1)
x <- rbinom(10000,1,prob=0.1)
x.mean.10 <- rep(0,2000)
x.mean.20 <- rep(0,2000)
x.mean.100 <- rep(0,2000)
for (i in 1:2000){
  x.subset <- sample(x,size=10,replace=T)
  x.mean.10[i] <- mean(x.subset)
  x.subset <- sample(x,size=20,replace=T)
  x.mean.20[i] <- mean(x.subset)
  x.subset <- sample(x,size=100,replace=T)
  x.mean.100[i] <- mean(x.subset)
}
par(mfrow=c(2,2),cex.main=2,cex.lab=1.8,cex.axis=1.5)
hist(x,freq=F)
hist(x.mean.10,nclass=7,freq=F,xlab="Sample mean",main="Sampling distribution of mean (n=10)")
hist(x.mean.20,nclass=20,freq=F,xlab="Sample mean",main="n=20")
hist(x.mean.100,nclass=50,freq=F,xlab="Sample mean",main="n=100")