# I chose to look at the data Iris which is already in R
data("iris")
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
attach(iris)
pedal.mod<-lm(Sepal.Length~Sepal.Width,data = iris)
# I want to see how the length and width of the flower species are related
confint(pedal.mod,level = .95)
##                 2.5 %     97.5 %
## (Intercept)  5.579865 7.47258038
## Sepal.Width -0.529820 0.08309785
#finding a confidence interval with alpha of .05
plot(Sepal.Length,Sepal.Width)

# this gives a scatter plot of the two variables with length being the predictor
newd<-data.frame(Sepal.Width=5)
# define new variable to predict the length given the width
summary(pedal.mod)
## 
## Call:
## lm(formula = Sepal.Length ~ Sepal.Width, data = iris)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.5561 -0.6333 -0.1120  0.5579  2.2226 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   6.5262     0.4789   13.63   <2e-16 ***
## Sepal.Width  -0.2234     0.1551   -1.44    0.152    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8251 on 148 degrees of freedom
## Multiple R-squared:  0.01382,    Adjusted R-squared:  0.007159 
## F-statistic: 2.074 on 1 and 148 DF,  p-value: 0.1519
#statistical summary of the linear model for the sepal length
(pred <- predict(pedal.mod, newd, interval="predict") )
##        fit      lwr      upr
## 1 5.409417 3.668536 7.150298
# create prediction intervals for several wait times with a single call to predict
(conf <- predict(pedal.mod, newd, interval="confidence") )
##        fit      lwr      upr
## 1 5.409417 4.799366 6.019468
conf %*% c(0, -1, 1)  #conf interval width
##       [,1]
## 1 1.220102
pred %*% c(0, -1, 1)  #pred interval width
##       [,1]
## 1 3.481762
conf[1] == pred[1]
## [1] TRUE