Linear Plateau in R
A linear plateau model is a statistical model used to describe a situation where the response variable increases linearly with the independent variable up to a certain threshold, after which it remains constant. This model is useful in various fields such as agriculture, biology, and economics, where a response (like crop yield, growth rate, or production) initially increases with an input (like fertilizer, dosage, or investment) but plateaus after a certain point.
# Load necessary libraries
library(ggplot2)
library(nls2)
# Simulated data for Linear Plateau Model (not necessary, as you will have your own data)
set.seed(123)
x <- seq(1, 20, by = 0.5)
y_linear <- ifelse(x <= 10, 3 + 2 * x + rnorm(length(x), sd = 2), 23 + rnorm(length(x), sd = 2))
data_linear <- data.frame(x = x, y = y_linear)
# Fit linear plateau model
linear_plateau_model <- nls(y ~ ifelse(x <= c, a + b * x, a + b * c), data = data_linear, start = list(a = 3, b = 2, c = 10))
# Extract coefficients
linear_coef <- coef(linear_plateau_model)
linear_eqn <- paste0("y = ", round(linear_coef['a'], 2), " + ", round(linear_coef['b'], 2), " * x\nfor x <= ", round(linear_coef['c'], 2), "\n",
"y = ", round(linear_coef['a'] + linear_coef['b'] * linear_coef['c'], 2), "\nfor x > ", round(linear_coef['c'], 2))
# Plot the data and the fitted linear plateau model
ggplot(data_linear, aes(x = x, y = y)) +
geom_point() + # Scatter plot
geom_line(aes(y = predict(linear_plateau_model)), color = "blue", size = 1) + # Linear plateau model
geom_vline(xintercept = linear_coef['c'], linetype = "dashed", color = "blue") + # Add vertical line for breakpoint
annotate("text", x = linear_coef['c'] + 0.5, y = max(data_linear$y), label = paste("Breakpoint =", round(linear_coef['c'], 2)), color = "blue", angle = 90, vjust = -0.5) +
labs(title = "Linear Plateau Model",
subtitle = "Response variable vs. Independent variable",
x = "Independent Variable (x)",
y = "Response Variable (y)") +
annotate("text", x = 15, y = 35, label = linear_eqn, hjust = 0, color = "blue") +
theme_minimal()
Code explanation
linear_plateau_model <- nls(y ~ ifelse(x <= c, a + b * x, a + b * c), data = data_linear, start = list(a = 3, b = 2, c = 10))
nls Function: nls stands for Nonlinear Least Squares, a function used to fit nonlinear models.
y ~ ifelse(x <= c, a + b x, a + b c): This is the model formula.
ifelse(x <= c, a + b x, a + b c) specifies a piecewise linear model where:
Recommended by LinkedIn
For values of x less than or equal to c, the model is y = a + b * x.
For values of x greater than c, the model is y = a + b * c, representing a plateau (constant value).
data = data_linear: Specifies that the data used for fitting the model is stored in the data_linear data frame.
start = list(a = 3, b = 2, c = 10): Provides initial estimates for the parameters a, b, and c to start the nonlinear optimization.
Parameters in the Linear Plateau Model
Disaster Resilience Researcher | Project Manager | Civil Engineer | Soil and Water Conservation Professional |
7moWhat is the inverse of the linear plateau?