This chunk of code shows how to do a simple confidence interval in R.
data(women)
attach(women)
mod <- lm(weight ~ height, data = women )
summary(women)
## height weight
## Min. :58.0 Min. :115.0
## 1st Qu.:61.5 1st Qu.:124.5
## Median :65.0 Median :135.0
## Mean :65.0 Mean :136.7
## 3rd Qu.:68.5 3rd Qu.:148.0
## Max. :72.0 Max. :164.0
confint(mod, level=.9)
## 5 % 95 %
## (Intercept) -98.030599 -77.002734
## height 3.288603 3.611397
This shows that no matter which way the variables are listed the correlation is still the same.As you can see there is a strong positive linear relationship.
cor(height, weight)
## [1] 0.9954948
cor(weight, height)
## [1] 0.9954948
Next we looked at the difference between confidence intervals and predicting intervals. This was new to me and an important point I learned was that predicting intervals are always larger than confidence intervals. We see this to be true in the next section of code. I also checked that the two are centered at the same place.
newdata <- data.frame(height = 52)
weight.mod <- lm(weight ~ height)
(predy <- predict(weight.mod, newdata, interval="predict") )
## fit lwr upr
## 1 91.88333 87.6255 96.14116
(confy <- predict(weight.mod, newdata, interval="confidence") )
## fit lwr upr
## 1 91.88333 89.18613 94.58054
confy %*% c(0, -1, 1)
## [,1]
## 1 5.394408
predy %*% c(0, -1, 1)
## [,1]
## 1 8.515662
confy[1] == predy[1]
## [1] TRUE
The last thing we learned how to do was F-tests which I did an example of below. As you can see there is a strong linear relationship between height and weight.
weight <- rnorm(136.7, sd=15.49869)
height <- rnorm(65, sd=4.472136)
var.test(weight, height)
##
## F test to compare two variances
##
## data: weight and height
## F = 10.057, num df = 135, denom df = 64, p-value < 2.2e-16
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 6.478154 15.117040
## sample estimates:
## ratio of variances
## 10.05742