For this project, I am working with the faithful dataset, which is available in R. The data comes from the Old Faithful geyser in Yellowstone National Park and records how the geyser behaves over time. Each observation includes two pieces of information:
How long an eruption lasted (in minutes)
How long people had to wait until the next eruption (in minutes)
My goal is to explore whether these two variables are connected. In other words, does the length of an eruption tell us anything about how long the waiting time will be afterward?
To answer this question, I will look at the basic structure of the data, create visualizations, and build simple regression models. Through this analysis, I hope to understand the pattern between the eruption duration and the waiting time in a clear and meaningful way.
Hypothesis: I think that longer eruptions lead to longer waiting times. It seems logical that if the geyser erupts for a long time, it might need more time to build up pressure before the next eruption.
Below are the steps I followed:
##Loading and inspecting the data##
data("faithful")
head(faithful)
## eruptions waiting
## 1 3.600 79
## 2 1.800 54
## 3 3.333 74
## 4 2.283 62
## 5 4.533 85
## 6 2.883 55
summary(faithful)
## eruptions waiting
## Min. :1.600 Min. :43.0
## 1st Qu.:2.163 1st Qu.:58.0
## Median :4.000 Median :76.0
## Mean :3.488 Mean :70.9
## 3rd Qu.:4.454 3rd Qu.:82.0
## Max. :5.100 Max. :96.0
The dataset has 272 observations. Eruptions last about 1.6 to 5.1 minutes, and waiting times range from 43 to 96 minutes.
This plot shows how the two variables relate. The regression line helps show the trend.
install.packages("ggplot2")
library(ggplot2) # for data visualization
ggplot(faithful,
aes
(x = eruptions,
y = waiting)) +
geom_point(alpha = 0.6) +
geom_smooth(method = "lm", se = TRUE) +
labs(
x = "Eruption Duration (minutes)",
y = "Waiting Time (minutes)",
title = "Relationship Between Eruption Duration and Waiting Time"
)
## `geom_smooth()` using formula = 'y ~ x'
There is clearly a positive pattern.In simple words, when the eruption lasts longer, the time before the next eruption also gets longer. The dots are spread a bit around the line, which means the connection is clear but not perfect, there are some differences in the data.
This model tests whether eruption duration predicts waiting time.
lm1 = lm(waiting ~ eruptions, data = faithful)
summary(lm1)
##
## Call:
## lm(formula = waiting ~ eruptions, data = faithful)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.0796 -4.4831 0.2122 3.9246 15.9719
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 33.4744 1.1549 28.98 <2e-16 ***
## eruptions 10.7296 0.3148 34.09 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.914 on 270 degrees of freedom
## Multiple R-squared: 0.8115, Adjusted R-squared: 0.8108
## F-statistic: 1162 on 1 and 270 DF, p-value: < 2.2e-16
confint(lm1)
## 2.5 % 97.5 %
## (Intercept) 31.20069 35.74810
## eruptions 10.10996 11.34932
The slope, shown by the eruption coefficient of 10.73, means that for every additional minute of eruption, the waiting time increases by about 10.7 minutes. The p-value (< 2e-16) shows that this relationship is statistically significant.
The confidence intervals show that the intercept is between 31.20 and 35.75, and the slope is between 10.11 and 11.35. This means the waiting time increases by about 10 to 11 minutes for each extra minute of eruption, confirming a strong and significant positive relationship.
This code produces residual diagnostics for the first model (linear). It helps us check normality, equal variance, and outliers.
install.packages("ggfortify")
library(ggfortify) # to use autopilot() function for diagnostic plots
autoplot(lm1)
The plot shows that the data doesn’t follow a straight-line pattern and the spread of points changes across the graph. This means the model doesn’t fit the data perfectly. To fix this, we can add a polynomial term, which helps the line bend and match the data better.
#Polynomial Regression
lm2 = lm(waiting ~ poly(eruptions, 2), data = faithful)
summary(lm2)
##
## Call:
## lm(formula = waiting ~ poly(eruptions, 2), data = faithful)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.6471 -4.0904 0.2855 3.5204 14.6787
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 70.8971 0.3503 202.41 < 2e-16 ***
## poly(eruptions, 2)1 201.6029 5.7767 34.90 < 2e-16 ***
## poly(eruptions, 2)2 -21.6025 5.7767 -3.74 0.000225 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.777 on 269 degrees of freedom
## Multiple R-squared: 0.8208, Adjusted R-squared: 0.8194
## F-statistic: 616 on 2 and 269 DF, p-value: < 2.2e-16
autoplot(lm2)
After adding the polynomial term, the model improved. The residuals are now more evenly spread, showing better linearity and less heteroscedasticity. The Q-Q plot looks closer to normal, and there are no major outliers, meaning the model fits the data much better.
The analysis of the faithful dataset shows a strong positive link between eruption duration and waiting time. When the eruption lasts longer, the waiting time before the next one also increases. The simple linear model proved this relationship, but the residual plots showed that the data wasn’t perfectly linear. After adding a polynomial term, the model became a better fit, with residuals more evenly spread and closer to normal. Overall, the polynomial model gave a clearer and more accurate picture of the Old Faithful geyser’s behavior.
By doing this project the most challenging part was finding the proper data. I have worked with one data for very log time, but it was very hard and challenging to continue it without instructor assistance. This project even though is not very challenging requires much time and attention to details. One important thing that I have learnt while doing this project is that the R is so comprehensive and offers solution to any issue that might arise with the data but for that one need to have strong expertise in it.