#What is the CO2 uptake in plants, given various factors affecting the plant, and what are the strength of these factors?
data("CO2")
head(CO2)
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
str(CO2)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame': 84 obs. of 5 variables:
## $ Plant : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
## $ Type : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
## $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
## $ conc : num 95 175 250 350 500 675 1000 95 175 250 ...
## $ uptake : num 16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
## - attr(*, "formula")=Class 'formula' language uptake ~ conc | Plant
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "outer")=Class 'formula' language ~Treatment * Type
## .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## - attr(*, "labels")=List of 2
## ..$ x: chr "Ambient carbon dioxide concentration"
## ..$ y: chr "CO2 uptake rate"
## - attr(*, "units")=List of 2
## ..$ x: chr "(uL/L)"
## ..$ y: chr "(umol/m^2 s)"
summary(CO2)
## Plant Type Treatment conc uptake
## Qn1 : 7 Quebec :42 nonchilled:42 Min. : 95 Min. : 7.70
## Qn2 : 7 Mississippi:42 chilled :42 1st Qu.: 175 1st Qu.:17.90
## Qn3 : 7 Median : 350 Median :28.30
## Qc1 : 7 Mean : 435 Mean :27.21
## Qc3 : 7 3rd Qu.: 675 3rd Qu.:37.12
## Qc2 : 7 Max. :1000 Max. :45.50
## (Other):42
library(ggplot2)
ggplot(CO2, aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = "lm") +
facet_wrap(~Type) +
labs(title = "CO2 Uptake vs Concentration", x = "Concentration", y = "Uptake")
## `geom_smooth()` using formula = 'y ~ x'

sum(is.na(CO2))
## [1] 0
CO2 <- na.omit(CO2)
library(caret)
## Loading required package: lattice
set.seed(123)
trainIndex <- createDataPartition(CO2$uptake, p = 0.8, list = FALSE, times = 1)
CO2Train <- CO2[trainIndex, ]
CO2Test <- CO2[-trainIndex, ]
model <- glm(uptake ~ conc + Treatment + Type, data = CO2Train)
train_control <- trainControl(method = "cv", number = 10)
model_cv <- train(uptake ~ conc + Treatment + Type, data = CO2Train, method = "lm", trControl = train_control)
summary(model_cv)
##
## Call:
## lm(formula = .outcome ~ ., data = dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.7011 -2.8947 0.8369 4.3378 10.9539
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.021502 1.677426 17.897 < 2e-16 ***
## conc 0.017680 0.002697 6.556 1.11e-08 ***
## Treatmentchilled -7.295361 1.511916 -4.825 9.03e-06 ***
## TypeMississippi -13.432816 1.507455 -8.911 8.09e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.193 on 64 degrees of freedom
## Multiple R-squared: 0.6761, Adjusted R-squared: 0.661
## F-statistic: 44.54 on 3 and 64 DF, p-value: 1.146e-15
predictions <- predict(model, CO2Test)
results <- data.frame(Actual = CO2Test$uptake, Predicted = predictions, Residuals = CO2Test$uptake - predictions)
MAE <- mean(abs(results$Actual - results$Predicted))
RMSE <- sqrt(mean((results$Actual - results$Predicted)^2))
cat("Mean Absolute Error: ", MAE, "\n")
## Mean Absolute Error: 4.729453
cat("Root Mean Squared Error: ", RMSE, "\n")
## Root Mean Squared Error: 6.28426
ggplot(results, aes(x = Actual, y = Predicted)) +
geom_point() +
geom_abline(slope = 1, intercept = 0, color = "red") +
labs(title = "Actual vs Predicted CO2 Uptake", x = "Actual Uptake", y = "Predicted Uptake")

plot_residuals_vs_predicted <- ggplot(results, aes(x = Predicted, y = Residuals)) +
geom_point(color = "aquamarine") +
geom_hline(yintercept = 0, color = "orange") + geom_smooth(method = "glm") +
labs(title = "Residuals vs Predicted CO2 Uptake", x = "Predicted Uptake", y = "Residuals") +
theme_minimal()
plot_residuals_vs_predicted
## `geom_smooth()` using formula = 'y ~ x'

#Summary
#The completed model indicates:
#All predictors (conc, Treatment, and Type) are highly significant.
#The intercept suggests that the baseline uptake (with all predictors at zero) is 30.021502.
#The concentration of CO2 has a positive effect on uptake.
#The "chilled" treatment and "Mississippi" type both have negative effects on uptake.
#The model explains approximately 67.61% of the variance in CO2 uptake, and the model fit is statistically significant.
#The results provide strong evidence that the concentration, treatment, and type significantly affect CO2 uptake.