| Type: | Package |
| Title: | Bayesian Multiple Linear Regression Estimation for Crop Yield |
| Version: | 0.1.0 |
| 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). |
| Depends: | R (≥ 4.0) |
| License: | MIT + file LICENSE |
| Imports: | rstan, ggplot2, bayesplot, stats |
| Encoding: | UTF-8 |
| LazyData: | true |
| Suggests: | testthat (≥ 3.0.0), rmarkdown |
| Config/roxygen2/version: | 8.1.0 |
| RoxygenNote: | 7.3.3 |
| NeedsCompilation: | no |
| Packaged: | 2026-09-25 11:21:46 UTC; iasri |
| Author: | 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 <prakash289111@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-10-06 07:50:09 UTC |
Example Rice Yield Dataset
Description
Simulated rice yield dataset with temperature and precipitation.
Usage
bayesre_example
Format
A data frame with 130 rows and 5 variables:
- Location
Location ID
- Year
Year
- Yield
Crop yield (t/ha)
- Temp
Mean seasonal temperature (°C)
- Precip
Seasonal precipitation (mm)
Compute Model Metrics
Description
Computes root mean squared error (RMSE) and root mean absolute percentage error (RMAPE) from posterior mean predictions.
Usage
compute_metrics(predictions)
Arguments
predictions |
A data frame returned by |
Value
A named list with two numeric elements:
- RMSE
Root mean squared error between observed and posterior mean predicted yield. Expressed in the same units as the yield variable.
- RMAPE
Root mean absolute percentage error, expressed as a percentage. Computed as
100 \times \sqrt{\overline{|e_i / y_i|}}, wheree_iis the prediction error andy_iis the observed yield for observationi.
Examples
# 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)
Fit Bayesian Regression Model
Description
Fits Bayesian linear regression using Stan.
Usage
fit_bayesre(data, chains = 1, iter = 500, warmup = 200)
Arguments
data |
Data frame containing Yield, Temp, Precip |
chains |
Number of MCMC chains |
iter |
Total iterations |
warmup |
Warmup iterations |
Value
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.
Plot Diagnostics
Description
Generates MCMC trace plots for the model parameters ...
Usage
plot_diagnostics(fit)
Arguments
fit |
A |
Value
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.
Plot Observed vs Predicted Yield
Description
Generates an observed vs posterior mean predicted yield plot with 95
Usage
plot_observed_vs_predicted(
predictions,
save = FALSE,
filename = NULL,
width = 8,
height = 6,
dpi = 300
)
Arguments
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 |
Value
A ggplot object.
Examples
# 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)
Plot Yield Response Heatmap
Description
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.
Usage
plot_response_surface(
fit,
temp_range = c(20, 35),
precip_range = c(50, 200),
grid_size = 50
)
Arguments
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 |
Value
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
\hat{\alpha} + \hat{\beta}_{\text{temp}} \times T +
\hat{\beta}_{\text{precip}} \times P, where \hat{\alpha},
\hat{\beta}_{\text{temp}}, and \hat{\beta}_{\text{precip}}
are the posterior means of the respective parameters.
Plot Response Surface with Observations
Description
Overlays observed temperature-precipitation data points on the posterior
mean yield response surface produced by plot_response_surface.
Usage
plot_response_surface_data(fit, data)
Arguments
fit |
A |
data |
A data frame containing at least two columns: |
Value
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.
Posterior Predictions
Description
Draws posterior predictions from a fitted stanfit object and
summarises them per observation.
Usage
predict_bayesre(fit, data)
Arguments
fit |
A |
data |
The data frame used to fit the model, containing a |
Value
A data frame with one row per observation and four columns:
- Yield_obs
Observed yield values from
data$Yield.- Yhat_mean
Posterior mean of the predicted yield.
- Yhat_lo
2.5th percentile of the posterior predictive distribution (lower bound of the 95% credible interval).
- Yhat_hi
97.5th percentile of the posterior predictive distribution (upper bound of the 95% credible interval).