The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Package {BayesRE}


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 predict_bayesre.

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|}}, where e_i is the prediction error and y_i is the observed yield for observation i.

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 stanfit object returned by fit_bayesre.

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 predict_bayesre, containing columns Yield_obs, Yhat_mean, Yhat_lo, and Yhat_hi.

save

Logical; if TRUE the plot is saved to filename as a PNG file. Default is FALSE.

filename

Character string giving the file name (including extension) for the saved plot. If NULL, a temporary file is used.

width

Plot width in inches. Default is 8.

height

Plot height in inches. Default is 6.

dpi

Resolution in dots per inch. Default is 300.

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 stanfit object returned by fit_bayesre.

temp_range

A numeric vector of length 2 giving the minimum and maximum temperature (degrees Celsius) for the prediction grid. Default is c(20, 35).

precip_range

A numeric vector of length 2 giving the minimum and maximum precipitation (mm) for the prediction grid. Default is c(50, 200).

grid_size

A positive integer controlling the resolution of the prediction grid. A value of n produces an n x n grid of temperature-precipitation combinations. Default is 50.

Value

A ggplot object displaying a tile heatmap with:

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 stanfit object returned by fit_bayesre.

data

A data frame containing at least two columns: Temp (temperature in degrees Celsius) and Precip (precipitation in mm). These values are plotted as points on top of the heatmap.

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 stanfit object returned by fit_bayesre.

data

The data frame used to fit the model, containing a Yield column.

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).

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.