Logo of the Physikalisch-Technische Bundesanstalt

Download of the Software for Bayesian Normal Linear Regression


R Software



Name:Initiates file downloadBayesianNormalLinearRegressionR (zip)



The R code for Bayesian inference of Normal linear regression problems consists of a main file and four supporting files. The main file PlotGener.r starts with the input specifications which need to be altered for each application, and then calls the functions described below, prints results (estimates of the non-informative Bayesian analyses of all previous measurement series, the prior distribution and posterior estimates), and performs the plotting of figures - all as given in [Opens external link in new windowKlauenberg et al., 2015-2]. The function

  • ReadData.r defines the functions for reading the data from a file containing three columns and functions for plotting these (the data include measurement series of the current as well as of previous experiments),
  • Fit.r defines the functions for calculating the design matrix, calculating the posterior distribution for one or many non-informative Bayesian analyses and least squares fits as well as for plotting the parameters of these results,
  • Prior.r defines the functions for deriving the prior distribution from a series of previous fits as well as for plotting the distribution of parameters and regression curves,
  • Post.r defines the functions for calculating the posterior distribution of the Bayesian analysis while accounting for prior knowledge, for printing the estimates and for performing the prediction for new, inter- or extrapolating x-values.

In order to run the code, place the data file in your directory, create a subfolder called Pictures, save the above R-files in the Picture folder and run the code in PlotGener.r from the parent directory. The above R code depends on the package MCMCPack, and runs under the R version 2015.

The statistical computing software R for computer-supported statistical calculation can be downloaded free of charge at Opens external link in new windowwww.r-project.org.

The preferred reference for citing the software in scientific papers is via the related publication [Opens external link in new windowKlauenberg et al., 2015-2]. In case of questions, remarks or suggestions, please contact Opens window for sending emailKaty Klauenberg.

To top

Matlab Software

Name:Initiates file downloadBayesianNormalLinearRegressionMATLAB (zip)


The MATLAB code is provided for Normal linear regression using Bayesian inference. The inference is carried out by running the main script BayesLR.m. Within the main script, the previous measurement data, the new measurement data as well as the regression model are specified. By editing this part of the script the software can be easily adapted to other Normal linear regression problems. The code utilizes a number of supporting functions for the calculation of the prior and posterior distributions, posterior estimates and posterior predictions. In addition, functions for generating graphic illustrations similar to those presented in this paper are provided. The code makes use of the functions gampdf.m and tinv.m from the MATLAB Statistics toolbox.

The preferred reference for citing the software in scientific papers is via the related publication [Opens external link in new windowKlauenberg et al., 2015-2].

In case of questions, remarks or suggestions, please contact Gerd Wübbeler.

To top


Guidance on Bayesian inference for regression tasks is largely lacking in metrology. For linear regression models with Gaussian measurement errors, a tutorial developed by PTB Working Group 8.42 gives explicit guidance. Divided into three steps, the tutorial first illustrates how a priori knowledge, which is available from previous experiments, can be translated into prior distributions from a specific class. These prior distributions have the advantage of yielding analytical, closed form results, thus avoiding the need to apply numerical methods such as Markov Chain Monte Carlo (MCMC). Secondly, formulas for the posterior results are given, explained and illustrated, and software implementations are provided. In the third step, Bayesian tools are used to assess the assumptions behind the suggested approach.

These three steps (prior elicitation, posterior calculation, and robustness to prior uncertainty and model adequacy) are critical to Bayesian inference. The general guidance given for Normal linear regression tasks is accompanied by a simple, but real-world, metrological example. The calibration of a flow device serves as a running example and illustrates the three steps. It is shown that prior knowledge from previous calibrations of the same sonic nozzle enables robust predictions even for extrapolations.

This tutorial is published as

   K. Klauenberg, G. Wübbeler, B. Mickan, P. M. Harris, and C. Elster. (2015). A Tutorial on Bayesian Normal Linear Regression. Metrologia, 52, 878–892. [DOI: 10.1088/0026-1394/52/6/878].

Along with the tutorial, PTB Working Group 8.42 has implemented software in the free statistical computing software R and in MATLAB in order to encourage metrologists and other scientists to apply Bayesian Normal linear regression. The software derives the prior distribution from previous Normal linear regressions, calculates the posterior distribution of all regression parameters, the regression curve, predictions as well as for estimates, uncertainties and credible intervals, and visualizes these quantities as well.

To top