In this project we consider the classical iris data set
that can be found in the base R package datasets.
iris has 5 columns named
Sepal.Length, Sepal.Width, Petal.Length, Petal.Width,
Species and 150
observations.
To get help about columns, run ?iris.
The data set iris is
First, we compute some descriptive statistics with the
summary() function:
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
|---|---|---|---|---|---|
| Min. :4.30 | Min. :2.00 | Min. :1.00 | Min. :0.1 | setosa :50 | |
| 1st Qu.:5.10 | 1st Qu.:2.80 | 1st Qu.:1.60 | 1st Qu.:0.3 | versicolor:50 | |
| Median :5.80 | Median :3.00 | Median :4.35 | Median :1.3 | virginica :50 | |
| Mean :5.84 | Mean :3.06 | Mean :3.76 | Mean :1.2 | ||
| 3rd Qu.:6.40 | 3rd Qu.:3.30 | 3rd Qu.:5.10 | 3rd Qu.:1.8 | ||
| Max. :7.90 | Max. :4.40 | Max. :6.90 | Max. :2.5 |
Second, we use the function aggregate to get the
following table that shows the means of the \(4\) numerical variables for each
species.
| Species | Sepal Length | Sepal Width | Petal Length | Petal Width |
|---|---|---|---|---|
| setosa | 5.01 | 3.43 | 1.46 | 0.246 |
| versicolor | 5.94 | 2.77 | 4.26 | 1.326 |
| virginica | 6.59 | 2.97 | 5.55 | 2.026 |
We use the function cor() to get the Pearson’s
coefficients of correlation between all our numeric variables:
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | |
|---|---|---|---|---|
| Sepal.Length | 1.000 | -0.118 | 0.872 | 0.818 |
| Sepal.Width | -0.118 | 1.000 | -0.428 | -0.366 |
| Petal.Length | 0.872 | -0.428 | 1.000 | 0.963 |
| Petal.Width | 0.818 | -0.366 | 0.963 | 1.000 |
Here are 3 scatter plots that show the association between
Petal.Length and the other numerical variables.
Now, we would like to explain the variations in the length of the
sepal as a function of the length of the petal. To do so, we use the
following linear regression
lm(Sepal.Length ~ Petal.Length, data = iris). Here is the
summary of this model:
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| (Intercept) | 4.307 | 0.078 | 54.9 | 0.00e+00 |
| Petal.Length | 0.409 | 0.019 | 21.6 | 1.04e-47 |
The model’s equation is \[ \widehat{Sepal.Length} = 4.307+ 0.409 Petal.Length. \]
Here are the data with the regression line:
Finally, we use the plot(reg) command to get some
graphical representations of the residuals.
Residuals plot 1
Residuals plot 2