15/05/2025, 21:22 R scripts model regression and plots - Colab
help(package = "datasets")
getwd()
'/content'
list.files()
'sample_data'
data <- BOD
head(data)
A data.frame: 6 × 2
Time demand
<dbl> <dbl>
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
BOD
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 1/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
A data.frame: 6 × 2
Time demand
<dbl> <dbl>
1 8.3
2 10.3
3 19.0
4 16.0
5 15.6
7 19.8
str(BOD)
'data.frame': 6 obs. of 2 variables:
$ Time : num 1 2 3 4 5 7
$ demand: num 8.3 10.3 19 16 15.6 19.8
- attr(*, "reference")= chr "A1.4, p. 270"
summary(BOD)
Time demand
Min. :1.000 Min. : 8.30
1st Qu.:2.250 1st Qu.:11.62
Median :3.500 Median :15.80
Mean :3.667 Mean :14.83
3rd Qu.:4.750 3rd Qu.:18.25
Max. :7.000 Max. :19.80
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 2/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
names(BOD)
'Time' · 'demand'
In this exercise, we made exploratory data analysis wiith BOD.csv datasets. we loaded data set from sample data set from R official site.
1. viewed datasets by BOD
2. STRUCTURE OF DATASET by names(BOD)
3. Summary statistics by summary(BOD)
4. column names by names(BOD)
plot(BOD, type = "o", col = "blue", main = "Biochemical Oxygen Demand", xlab = "Time (days)", ylab = "Demand (mg/L)")
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 3/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
BOD$Demand2 <- BOD$demand^2
head(BOD)
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 4/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
A data.frame: 6 × 3
Time demand Demand2
<dbl> <dbl> <dbl>
1 1 8.3 68.89
2 2 10.3 106.09
3 3 19.0 361.00
4 4 16.0 256.00
5 5 15.6 243.36
6 7 19.8 392.04
subset(BOD, demand > 10)
A data.frame: 5 × 3
Time demand Demand2
<dbl> <dbl> <dbl>
2 2 10.3 106.09
3 3 19.0 361.00
4 4 16.0 256.00
5 5 15.6 243.36
6 7 19.8 392.04
Start coding or generate with AI.
Linear Model fitting
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 5/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
Double-click (or enter) to edit
model <- lm(demand ~ Time, data = BOD)
summary(model)
Call:
lm(formula = demand ~ Time, data = BOD)
Residuals:
1 2 3 4 5 6
-1.9429 -1.6643 5.3143 0.5929 -1.5286 -0.7714
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8.5214 2.6589 3.205 0.0328 *
Time 1.7214 0.6387 2.695 0.0544 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.085 on 4 degrees of freedom
Multiple R-squared: 0.6449, Adjusted R-squared: 0.5562
F-statistic: 7.265 on 1 and 4 DF, p-value: 0.05435
plot(BOD)
abline(model, col = "red")
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 6/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
# Ensure you have a numeric dataset, like 'Time' and 'demand'
plot(BOD$Time, BOD$demand2, type = "o", col = "orange",
main = "Biochemical Oxygen Demand", xlab = "Time (days)", ylab = "Demand2 (mg/L)")
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 7/8
15/05/2025, 21:22 R scripts model regression and plots - Colab
# Predict values from the model
BOD$predicted_demand2 <- predict(model, BOD)
# Plot original vs predicted values
plot(BOD$Time, BOD$demand2, col = "blue", pch = 16, main = "Original vs Predicted Demand2",
xlab = "Time", ylab = "Demand2")
lines(BOD$Time, BOD$predicted_demand2, col = "red", lwd = 2)
https://colab.research.google.com/drive/1S_vF-23aGuJdaZWyBimJK-AndpjYG4gQ#printMode=true 8/8