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:

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

  1. 𝑎 (Intercept): This parameter represents the intercept of the linear portion of the model. It is the value of the response variable 𝑦 when the independent variable 𝑥 is zero, assuming 𝑥 is within the linear region (i.e., 𝑥≤𝑐).
  2. 𝑏 (Slope): This parameter represents the slope of the linear portion of the model. It indicates the rate at which the response variable 𝑦 changes with respect to the independent variable 𝑥 within the linear region (i.e., 𝑥≤𝑐).
  3. 𝑐 (Breakpoint or Knot): This parameter represents the breakpoint or threshold value of the independent variable 𝑥. It defines the point at which the response variable 𝑦 transitions from the linear region to the plateau region. For 𝑥>𝑐, the value of 𝑦 remains constant.



Kent T.

Disaster Resilience Researcher | Project Manager | Civil Engineer | Soil and Water Conservation Professional |

7mo

What is the inverse of the linear plateau?

Like
Reply

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics