| Title: | Bayesian Multiple Linear Regression Estimation for Crop Yield |
|---|---|
| Description: | Implements Bayesian multiple linear regression models for estimating crop yield response to climatic variables. Posterior predictions and regional sensitivity coefficients are returned and can be visualised with built-in plotting utilities. Methods are based on Stan (Carpenter et al. (2017) <doi:10.18637/jss.v076.i01>) and Bayesian workflow described in Gelman et al. (2013, ISBN:9781439840955). |
| Authors: | Prakash Kumar [aut, cre], Himadri Sekhar Roy [aut], Ranjit Kumar Paul [aut], Md. Yeasin [aut], Neeraj Budhlakoti [aut], Sunil Kumar Yadav [aut], Amrit Kumar Paul [aut] |
| Maintainer: | Prakash Kumar <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-07 09:32:35 UTC |
| Source: | https://github.com/cran/BayesRE |
Simulated rice yield dataset with temperature and precipitation.
bayesre_examplebayesre_example
A data frame with 130 rows and 5 variables:
Location ID
Year
Crop yield (t/ha)
Mean seasonal temperature (°C)
Seasonal precipitation (mm)
Computes root mean squared error (RMSE) and root mean absolute percentage error (RMAPE) from posterior mean predictions.
compute_metrics(predictions)compute_metrics(predictions)
predictions |
A data frame returned by |
A named list with two numeric elements:
Root mean squared error between observed and posterior mean predicted yield. Expressed in the same units as the yield variable.
Root mean absolute percentage error, expressed as a
percentage. Computed as ,
where is the prediction error and is the observed
yield for observation .
# Small deterministic toy predictions; runs in well under 5 seconds. toy_predictions <- data.frame( Yield_obs = c(10, 12, 15), Yhat_mean = c(9.5, 12.5, 14), Yhat_lo = c(8, 11, 13), Yhat_hi = c(11, 14, 16) ) metrics <- compute_metrics(toy_predictions) stopifnot(is.list(metrics), all(c("RMSE", "RMAPE") %in% names(metrics))) # A full Stan workflow is intentionally excluded from the automatic # example run because model compilation/sampling can exceed 5 seconds. data(bayesre_example) fit <- fit_bayesre(bayesre_example, chains = 1, iter = 500, warmup = 200) pred <- predict_bayesre(fit, bayesre_example) compute_metrics(pred)# Small deterministic toy predictions; runs in well under 5 seconds. toy_predictions <- data.frame( Yield_obs = c(10, 12, 15), Yhat_mean = c(9.5, 12.5, 14), Yhat_lo = c(8, 11, 13), Yhat_hi = c(11, 14, 16) ) metrics <- compute_metrics(toy_predictions) stopifnot(is.list(metrics), all(c("RMSE", "RMAPE") %in% names(metrics))) # A full Stan workflow is intentionally excluded from the automatic # example run because model compilation/sampling can exceed 5 seconds. data(bayesre_example) fit <- fit_bayesre(bayesre_example, chains = 1, iter = 500, warmup = 200) pred <- predict_bayesre(fit, bayesre_example) compute_metrics(pred)
Fits Bayesian linear regression using Stan.
fit_bayesre(data, chains = 1, iter = 500, warmup = 200)fit_bayesre(data, chains = 1, iter = 500, warmup = 200)
data |
Data frame containing Yield, Temp, Precip |
chains |
Number of MCMC chains |
iter |
Total iterations |
warmup |
Warmup iterations |
A stanfit object containing the posterior samples.
Use predict_bayesre to obtain predictions,
plot_diagnostics for MCMC trace plots, and
plot_response_surface for a yield response heatmap.
Generates MCMC trace plots for the model parameters ...
plot_diagnostics(fit)plot_diagnostics(fit)
fit |
A |
A ggplot object (the trace-plot grid) returned invisibly by
bayesplot::mcmc_trace(). The plot is also printed to the active
graphics device as a side effect.
Generates an observed vs posterior mean predicted yield plot with 95
plot_observed_vs_predicted( predictions, save = FALSE, filename = NULL, width = 8, height = 6, dpi = 300 )plot_observed_vs_predicted( predictions, save = FALSE, filename = NULL, width = 8, height = 6, dpi = 300 )
predictions |
A data frame returned by |
save |
Logical; if |
filename |
Character string giving the file name (including extension)
for the saved plot. If |
width |
Plot width in inches. Default is |
height |
Plot height in inches. Default is |
dpi |
Resolution in dots per inch. Default is |
A ggplot object.
# Small toy predictions; no Stan model is needed. toy_predictions <- data.frame( Yield_obs = c(10, 12, 15, 17), Yhat_mean = c(10.2, 11.7, 14.6, 17.4), Yhat_lo = c(9, 10.5, 13.5, 16), Yhat_hi = c(11.5, 13, 15.8, 18.5) ) p <- plot_observed_vs_predicted(toy_predictions) stopifnot(inherits(p, "ggplot")) plot_observed_vs_predicted( toy_predictions, save = TRUE, filename = tempfile(fileext = ".png") ) # A full Stan workflow is intentionally excluded from the automatic # example run because model compilation/sampling can exceed 5 seconds. data(bayesre_example) fit <- fit_bayesre(bayesre_example, chains = 1, iter = 500, warmup = 200) pred <- predict_bayesre(fit, bayesre_example) plot_observed_vs_predicted(pred)# Small toy predictions; no Stan model is needed. toy_predictions <- data.frame( Yield_obs = c(10, 12, 15, 17), Yhat_mean = c(10.2, 11.7, 14.6, 17.4), Yhat_lo = c(9, 10.5, 13.5, 16), Yhat_hi = c(11.5, 13, 15.8, 18.5) ) p <- plot_observed_vs_predicted(toy_predictions) stopifnot(inherits(p, "ggplot")) plot_observed_vs_predicted( toy_predictions, save = TRUE, filename = tempfile(fileext = ".png") ) # A full Stan workflow is intentionally excluded from the automatic # example run because model compilation/sampling can exceed 5 seconds. data(bayesre_example) fit <- fit_bayesre(bayesre_example, chains = 1, iter = 500, warmup = 200) pred <- predict_bayesre(fit, bayesre_example) plot_observed_vs_predicted(pred)
Generates a posterior mean yield response surface over a grid of temperature and precipitation values, using the posterior means of the intercept and regression coefficients extracted from the fitted model.
plot_response_surface( fit, temp_range = c(20, 35), precip_range = c(50, 200), grid_size = 50 )plot_response_surface( fit, temp_range = c(20, 35), precip_range = c(50, 200), grid_size = 50 )
fit |
A |
temp_range |
A numeric vector of length 2 giving the minimum and maximum
temperature (degrees Celsius) for the prediction grid. Default is
|
precip_range |
A numeric vector of length 2 giving the minimum and
maximum precipitation (mm) for the prediction grid. Default is
|
grid_size |
A positive integer controlling the resolution of the
prediction grid. A value of |
A ggplot object displaying a tile heatmap with:
Temperature (degrees Celsius) on the x-axis.
Precipitation (mm) on the y-axis.
Posterior mean predicted yield mapped to the fill colour using the viridis colour scale.
The predicted yield at each grid point is computed as
, where ,
, and
are the posterior means of the respective parameters.
Overlays observed temperature-precipitation data points on the posterior
mean yield response surface produced by plot_response_surface.
plot_response_surface_data(fit, data)plot_response_surface_data(fit, data)
fit |
A |
data |
A data frame containing at least two columns: |
A ggplot object: the posterior mean yield heatmap from
plot_response_surface with the observed
temperature-precipitation points from data overlaid as filled
black circles (size 1.5, alpha 0.7). The temperature and precipitation
ranges of the heatmap default to c(20, 35) and c(50, 200)
respectively; pass a customised base plot via
plot_response_surface if different ranges are needed.
Draws posterior predictions from a fitted stanfit object and
summarises them per observation.
predict_bayesre(fit, data)predict_bayesre(fit, data)
fit |
A |
data |
The data frame used to fit the model, containing a |
A data frame with one row per observation and four columns:
Observed yield values from data$Yield.
Posterior mean of the predicted yield.
2.5th percentile of the posterior predictive distribution (lower bound of the 95% credible interval).
97.5th percentile of the posterior predictive distribution (upper bound of the 95% credible interval).