2024-03-21

Introduction to Simple Linear Regression

Simple linear regression is a foundational statistical method that models the relationship between a dependent variable and an independent variable by fitting a linear equation to observed data. The primary purpose is to understand and quantify the relationship between these variables.

Why is it important?

  • Predictive Analysis: It helps predict the outcome of one variable based on the value of another.
  • Inference: Linear regression is used to infer the strength of the relationship between the dependent and independent variables.
  • Decision Making: In various fields, from business to engineering, it aids in making informed decisions.

Objective of Our Analysis

The primary objective of this analysis is to explore and model the relationship between the amount of fertilizer used and the subsequent growth of plants measured by their height. By doing so, we aim to achieve the following:

Understanding Key Relationships: - To quantify how plant height varies with changes in the amount of fertilizer used. - To identify the strength and direction of the association between these two variables.

Predictive Insights: - To use the linear model to predict plant height for given amounts of fertilizer, assisting in agricultural planning and optimization. - To estimate the expected increase in plant height resulting from incremental fertilizer use.

Theoretical Framework

In simple linear regression, our goal is to find the best-fitting straight line through the data points. The equation for this line is:

\[ y = \beta_0 + \beta_1x \]

where:

  • \(y\) is the dependent variable (the variable we are trying to predict or explain),
  • \(x\) is the independent variable (the variable we use for prediction),
  • \(\beta_0\) is the y-intercept of the regression line (the value of \(y\) when \(x = 0\)),
  • \(\beta_1\) is the slope of the regression line (the change in \(y\) for a one-unit change in \(x\)).

Multiple Linear Regression

Multiple Linear Regression extends the simple linear regression model to incorporate more than one independent variable, and is formulated as:

\[ Y = \beta_0 + \beta_1X_1 + \beta_2X_2 + \dots + \beta_pX_p + \varepsilon \]

  • \(Y\) is the dependent variable that we’re trying to explain or predict.
  • \(X_1, X_2, \dots, X_p\) are the independent variables (predictors).
  • \(\beta_0\) is the intercept term. It represents the value of \(Y\) when all the independent variables are zero.
  • \(\beta_1, \beta_2, \dots, \beta_p\) are the coefficients of the independent variables. Each coefficient estimates the change in \(Y\) associated with a one-unit change in the corresponding independent variable, holding all other variables constant.
  • \(\varepsilon\) is the error term, representing the variation in \(Y\) not explained by the independent variables.

Create sample data

fertilizer_used <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
plant_height <- c(10, 12, 14, 15, 17, 18, 20, 21, 23, 25)

fertilizer_used: A numeric vector representing the amount of fertilizer applied. It ranges from 1 to 10 units, assuming that each unit increase corresponds to an incremental application of fertilizer.

plant_height: A numeric vector indicating the height of plants corresponding to each amount of fertilizer used. The values are measured in centimeters, and they increase as more fertilizer is applied, suggesting that plant height may be positively associated with fertilizer use.

Show summary of the model

summary(model)
## 
## Call:
## lm(formula = plant_height ~ fertilizer_used)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.4849 -0.2500  0.0000  0.2500  0.4849 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      8.73333    0.22874   38.18 2.43e-10 ***
## fertilizer_used  1.59394    0.03687   43.24 9.03e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3348 on 8 degrees of freedom
## Multiple R-squared:  0.9957, Adjusted R-squared:  0.9952 
## F-statistic:  1869 on 1 and 8 DF,  p-value: 9.03e-11

Create a plot

library(ggplot2)
plot <- ggplot(data = data.frame(fertilizer_used, plant_height), aes(x = fertilizer_used, y = plant_height)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "blue") +
  theme_minimal() +
  labs(title = "Plant Height vs. Fertilizer Used",
       x = "Fertilizer Used (units)",
       y = "Plant Height (cm)")

plot
## `geom_smooth()` using formula = 'y ~ x'

3D Plotly Visualization

Now, let’s create a 3D scatter plot to visualize the relationship between fertilizer used, plant height, and soil moisture in our dataset.

library(plotly)

# Assuming 'soil_moisture' is another variable you're interested in
soil_moisture <- runif(10, min = 0, max = 100)

# 3D scatter plot with plotly
plot3D <- plot_ly(data = data.frame(fertilizer_used, plant_height, soil_moisture), x = ~fertilizer_used, y = ~plant_height, z = ~soil_moisture, type = 'scatter3d', mode = 'markers') %>%
  layout(title = '3D Scatter Plot',
         scene = list(xaxis = list(title = 'Fertilizer Used (units)',
                                   backgroundcolor = "white",
                                   gridcolor = "lightgrey",
                                   zerolinecolor = "grey"),
                      yaxis = list(title = 'Plant Height (cm)',
                                   backgroundcolor = "white",
                                   gridcolor = "lightgrey",
                                   zerolinecolor = "grey"),
                      zaxis = list(title = 'Soil Moisture (%)',
                                   backgroundcolor = "white",
                                   gridcolor = "lightgrey",
                                   zerolinecolor = "grey")),
         margin = list(l = 0, r = 0, b = 0, t = 30))

3D plot

plot3D

Bar Plot of Average Plant Height by Fertilizer Used

# Calculate the average plant height for each amount of fertilizer used
average_height <- tapply(plant_height, fertilizer_used, mean)

# Convert the result to a data frame for plotting
average_data <- data.frame(fertilizer_used = as.numeric(names(average_height)), average_height = average_height)

# Sort the data frame by the fertilizer_used variable
average_data <- average_data[order(average_data$fertilizer_used), ]

# Create the bar plot
bar_plot <- ggplot(data = average_data, aes(x = factor(fertilizer_used), y = average_height)) +
  geom_bar(stat = "identity", fill = "skyblue", color = "black") +  # Create the bar plot
  theme_minimal() +  # Apply minimal theme
  labs(title = "Average Plant Height by Fertilizer Used",
       x = "Fertilizer Used (units)",
       y = "Average Plant Height (cm)")

bar_plot

***

Conclusion

Throughout this presentation, we have explored various statistical methods to understand the relationship between fertilizer use and plant growth. Our analysis started with simple linear regression to model the direct correlation between the amount of fertilizer used and the plant height. We then expanded our model to multiple linear regression, allowing us to consider multiple factors that could influence plant growth.