# Normal simulated data
set.seed(0)
n<-400
sig<-2
x0 <- runif(n, 0, 1)
x1 <- runif(n, 0, 1)
x2 <- runif(n, 0, 1)
x3 <- runif(n, 0, 1)
f0 <- function(x) 2 * sin(pi * x)
f1 <- function(x) exp(2 * x)
f2 <- function(x) 0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10
f3 <- function(x) 0*x
f <- f0(x0) + f1(x1) + f2(x2)
e <- rnorm(n, 0, sig)
y <- f + e
# prior
prior<-list(taub1=0.01,
taub2=0.01,
beta0=rep(0,1),
Sbeta0=diag(100,1),
tau1=0.01,
tau2=0.01)
# Initial state
state <- NULL
# MCMC parameters
nburn<-5000
nsave<-5000
nskip<-0
ndisplay<-10
mcmc <- list(nburn=nburn,
nsave=nsave,
nskip=nskip,
ndisplay=ndisplay)
# fitting the model
fit1<-PSgam(formula=y~ps(x0,x1,x2,x3,k=20,degree=3,pord=1),
family=gaussian(),prior=prior,
mcmc=mcmc,ngrid=30,
state=state,status=TRUE)
# A binary example
g <- (f-5)/3
g <- binomial()$linkinv(g)
y <- rbinom(g,1,g)
# fitting the model
fit2<-PSgam(formula=y~ps(x0,x1,x2,x3,k=20,degree=3,pord=1),
family=binomial(logit),prior=prior,
mcmc=mcmc,ngrid=30,
state=state,status=TRUE)
# Poisson data
g<-exp(f/4)
y<-rpois(rep(1,n),g)
# fitting the model
fit3<-PSgam(formula=y~ps(x0,x1,x2,x3,k=20,degree=3,pord=1),
family=poisson(log),prior=prior,
mcmc=mcmc,ngrid=30,
state=state,status=TRUE)
Run the code above in your browser using DataLab