library(readxl)
data <- read_excel("C:/Users/aiman/Documents/LAST DEGREE/SMS/lab report/lab report 1/revision lab report/algeas.xlsx")
data
Q1
variable to choose
# Correlation of each variable with Population
cor_population <- cor(data)[, "Population"]
round(cor_population, 3)
Light Nitrate Iron Phosphate Temperature pH CO2
0.006 -0.001 0.001 0.017 0.009 0.008 -0.015
Population
1.000
Phosphate has the strongest correlation with Population(Correlation: r = 0.017, which is the strongest among your predictors, although it is still extremely weak). ,therefore we choose Phosphate for our linear regression analysis for exploratory analysis.
Response variable (Y): Population Exploratory variable (X): Phosphate
x <- data$Phosphate
y <- data$Population
plot(x, y,
main = "Scatter Plot of Phosphate and Algae Population",
xlab = "Phosphate (mg/L)",
ylab = "Algae Population",
pch = 19,
frame = FALSE)
Interpretation: The scatter plot shows the relationship between Phosphate and algae Population. The points are widely scattered and do not show a clear linear pattern, indicating that the linear relationship between Phosphate and Population is very weak.
A. Correlation coefficient
cor_phosphate <- cor(data$Population,data$Phosphate)
cor_phosphate
[1] 0.01699214
Interpretation: The Pearson correlation coefficient between Phosphate and algae Population is r = 0.017. This indicates a very weak positive linear relationship between Phosphate and algae Population.
B. Coefficient of determination, R²
simple_model <- lm(Population ~ Phosphate,data = data)
r_squared <- summary(simple_model)$r.squared
r_squared
[1] 0.0002887329
Interpretation: The coefficient of determination is approximately R² = 0.000289, which means that only approximately 0.0289% of the variation in algae Population is explained by Phosphate. Therefore, Phosphate provides very little explanatory power for predicting algae Population in this dataset.
model <- lm(Population ~ Phosphate,data = data)
summary(model)
Call:
lm(formula = Population ~ Phosphate, data = data)
Residuals:
Min 1Q Median 3Q Max
-3204.5 -1106.1 403.6 1217.5 2124.3
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3113.98 31.94 97.493 <2e-16 ***
Phosphate 452.38 269.14 1.681 0.0928 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1460 on 9782 degrees of freedom
Multiple R-squared: 0.0002887, Adjusted R-squared: 0.0001865
F-statistic: 2.825 on 1 and 9782 DF, p-value: 0.09283
Equation: Population = 3113.98 + 452.38(Phosphate) Intercept = 3113.985 The intercept is 3113.985, which represents the predicted algae Population when the Phosphate concentration is zero. Slope = 452.385 The slope is 452.385, indicating that for every one-unit increase in Phosphate concentration, the predicted algae Population increases by approximately 452.385 units, on average.
H₀: β₁ = 0, indicating that there is no significant linear relationship between Phosphate and algae Population. H₁: β₁ ≠ 0, indicating that there is a significant linear relationship between Phosphate and algae Population. p-value = 0.093 Since p-value(0.093) > (alpha=0.05),we fail to reject H₀ At alpha = 0.05, there is insufficient statistical evidence to conclude that Phosphate has a significant linear relationship with algae Population.
A.Linearity
plot(simple_model,
which = 1,
main = "Residuals vs Fitted Values")
Linearity: The Residuals vs Fitted plot shows the residuals scattered around the horizontal zero line without a strong systematic curved pattern. Therefore, there is no strong evidence of a violation of the linearity assumption.
B. Independence
plot(residuals(simple_model),
type = "p",
pch = 19,
main = "Residuals vs Observation Order",
xlab = "Observation Order",
ylab = "Residuals")
abline(h = 0)
Interpretation: The residuals do not show a clear systematic pattern when plotted against observation order. In addition, the Durbin-Watson statistic is approximately 1.98, which is close to 2, suggesting that the independence assumption is reasonable.
C. Normality
plot(simple_model,which = 2,main = "Normal Q-Q Plot")
Interpretation: The Normal Q-Q plot shows that the residual points do not closely follow the diagonal reference line. Instead, the points form a clear S-shaped pattern, with substantial deviations at both tails. Therefore, the residuals do not appear to be normally distributed, indicating that the normality assumption is not fully satisfied.
D. Equal Variance
plot(simple_model,
which = 3,
main = "Scale-Location Plot")
Interpretation: The Scale-Location plot shows that the residuals have a relatively consistent spread across the fitted values. The red trend line is approximately horizontal, and there is no clear funnel-shaped pattern. Therefore, the equal variance assumption appears to be reasonably satisfied.
Q2
Since Phosphate and CO2 have the two largest absolute correlations with Population in Q1 results,so we choose it as exploratory variables,X.
Response variable (Y): Population X₁: Phosphate X₂: CO2
i)Scatter Diagram
pairs(data[, c("Population", "Phosphate", "CO2")],
pch = 19)
Interpretation: The scatter plot matrix shows the relationships between Population, Phosphate and CO₂. The plots show no clear strong linear pattern between Population and either Phosphate or CO₂.
A. Correlation coefficient
corr <- round(cor(data[, c("Population","Phosphate","CO2")]), 4)
corr
Population Phosphate CO2
Population 1.0000 0.0170 -0.0149
Phosphate 0.0170 1.0000 -0.0116
CO2 -0.0149 -0.0116 1.0000
Interpretation: For Phosphate, The correlation coefficient of 0.0170 indicates a very weak positive linear relationship between Phosphate and algae Population.For CO2,The correlation coefficient of −0.0149 indicates a very weak negative linear relationship between CO₂ and algae Population.
B. Coefficient of determination, R²
multiple_model <- lm(Population ~ Phosphate + CO2,data = data)
summary(multiple_model)
Call:
lm(formula = Population ~ Phosphate + CO2, data = data)
Residuals:
Min 1Q Median 3Q Max
-3231.8 -1104.9 404.7 1216.2 2154.5
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3170.440 50.334 62.988 <2e-16 ***
Phosphate 447.862 269.146 1.664 0.0961 .
CO2 -9.316 6.420 -1.451 0.1468
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1460 on 9781 degrees of freedom
Multiple R-squared: 0.0005039, Adjusted R-squared: 0.0002996
F-statistic: 2.466 on 2 and 9781 DF, p-value: 0.085
Interpretation: The Multiple R-squared value of 0.0005039 indicates that approximately 0.05% of the variation in algae Population is explained jointly by Phosphate and CO₂.
summary(multiple_model)
Call:
lm(formula = Population ~ Phosphate + CO2, data = data)
Residuals:
Min 1Q Median 3Q Max
-3231.8 -1104.9 404.7 1216.2 2154.5
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3170.440 50.334 62.988 <2e-16 ***
Phosphate 447.862 269.146 1.664 0.0961 .
CO2 -9.316 6.420 -1.451 0.1468
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1460 on 9781 degrees of freedom
Multiple R-squared: 0.0005039, Adjusted R-squared: 0.0002996
F-statistic: 2.466 on 2 and 9781 DF, p-value: 0.085
# Display coefficients
coef(multiple_model)
(Intercept) Phosphate CO2
3170.440271 447.862281 -9.316069
Eqution: Population = 3170.440 + 447.862(Phospate) - 9.316(CO2) Intercept = 3170.440 The intercept of 3170.440 represents the predicted algae Population when both Phosphate and CO₂ are zero. Phosphate coefficient = 447.862 Holding CO₂ constant, a one-unit increase in Phosphate is associated with an estimated 447.862 unit increase in algae Population. CO₂ coefficient = −9.316 Holding Phosphate constant, a one-unit increase in CO₂ is associated with an estimated 9.316 unit decrease in algae Population.
summary(multiple_model)
Call:
lm(formula = Population ~ Phosphate + CO2, data = data)
Residuals:
Min 1Q Median 3Q Max
-3231.8 -1104.9 404.7 1216.2 2154.5
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3170.440 50.334 62.988 <2e-16 ***
Phosphate 447.862 269.146 1.664 0.0961 .
CO2 -9.316 6.420 -1.451 0.1468
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1460 on 9781 degrees of freedom
Multiple R-squared: 0.0005039, Adjusted R-squared: 0.0002996
F-statistic: 2.466 on 2 and 9781 DF, p-value: 0.085
H₀: β₁ = β₂ = 0 H₁: At least one of β₁, β₂ ≠ 0 p-value = 0.085 Since p-value(0.085) > (alpha=0.05),we fail to reject H₀ At alpha = 0.05 ,there is insufficient evidence to conclude that Phosphate and CO₂ jointly have a significant linear relationship with algae Population.
A.Linearity
plot(multiple_model,
which = 1,
main = "Multiple Regression: Residuals vs Fitted")
Interpretation: The residuals are randomly scattered around the zero line without a clear curved pattern. Therefore, the linearity assumption is reasonably satisfied for the multiple linear regression model.
B. Independence
plot(residuals(multiple_model),
type = "p",
pch = 19,
main = "Multiple Regression: Residuals vs Observation Order",
xlab = "Observation Order",
ylab = "Residuals")
abline(h = 0)
Interpretation: The residuals appear to be randomly scattered around zero across the observation order without a clear systematic pattern. Therefore, the independence assumption is reasonably satisfied.
C. Normality
plot(multiple_model,
which = 2,
main = "Multiple Regression: Normal Q-Q Plot")
Interpretation: The points in the normal Q-Q plot deviate substantially from the diagonal reference line, particularly at the lower and upper tails. This indicates that the residuals are not normally distributed. Therefore, the normality assumption is not satisfied for the multiple linear regression model.
D. Equal Variance
plot(multiple_model,
which = 3,
main = "Multiple Regression: Scale-Location Plot")
Interpretation: The residuals show a relatively consistent spread across the fitted values, and the smooth red line remains approximately horizontal. There is no clear funnel-shaped pattern. Therefore, the equal variance (homoscedasticity) assumption is reasonably satisfied.