Bayz Manual

Quick start and demo of bayz

Bayz in R runs models by calling the bayz() function. The model description is like a regualar R model-description, but bayz has small functions around each model-term to specify how a variable should be fitted, for instance rg() to fit a regression. Here is a small example fitting the standard intercept and slope model:

x = rnorm(500)
y = 2 + x + rnorm(500) # set intercept 2 and slope of y on x as 1
dat1 = data.frame(y,x)
library(BayzR)
fit1 = bayz(y~rg(x),data=dat1, chain=c(2000,100,10)) 
summary(fit1)
The summary fit will report a small table with "Estimates for model coefficients" showing a mean (intercept) and value for "x" (regression on the covariable x) with estimates (PostMean, Posterior Means) around 2 and 1. If you see this you have bayz working!

Fixed class variables (in R called factors) are fitted with the fx() model-term, and they allow interactions using a colon (:), as in this example:

Year = as.factor(rep(c("yr1","yr2"),250))
Locat = as.factor(rep(c("lc1","lc2"),each=250))
y = rnorm(500)  # there is no real Year and Locat effects in the data
dat2 = data.frame(y,Year,Locat)
library(BayzR)
fit2 = bayz(y~fx(Year)+fx(Locat)+fx(Year:Locat),data=dat2, chain=c(2000,100,10)) 
summary(fit2)
Bayz has no feature to expand an interaction specified with * to main effects and interaction, instead as in the example above, one needs to write out the main effects and use the colon for interactions. The estimates table in summary now contains estimates for Year, Locat and the interaction. It is possible to retrieve all, or only one of the model estimates using the fixef() function (if it is fixed effects you want to retrieve) as shown below:
fixef(fit2)$"Year"
##          Year  postMean   postSD
## Year%yr1  yr1  0.000000 0.000000
## Year%yr2  yr2 -4.687776 3.034762

Random effects are fitted with the rn() model-term. They can have interactions as for fixed effects, and it is possible to add similarity / relationship-matrices and products of similarity-matrices (fitted as Kronecker products) in case of interactions. For details on using similarity-matrices see the Model Terms section. Here is a small demo of basic random effects by changing the Year:Locat effects to random:

Year = as.factor(rep(c("yr1","yr2","yr3","yr4","yr5"),200))
Locat = as.factor(rep(c("lc1","lc2","lc3","lc4"),each=250))
y = rnorm(1000)  # there is no real Year and Locat effects in the data
dat3 = data.frame(y,Year,Locat)
library(BayzR)
fit3 = bayz(y~fx(Year)+fx(Locat)+rn(Year:Locat),data=dat3, chain=c(2000,100,10)) 
summary(fit3)
Now summary() will include a table of variance components with a var.Year.Locat term as well as the var.y term for the residual variance. You will also see a note that Year:Locat is not in the table of model coefficients because it has too many levels. You can find these effects either by increasing the maxLevel limit in summary, or by using fixef() and ranef() functions. Here Year:Locat was random, so its effects can be retrieved with ranef():
ranef(fit3)$"Year:Locat"