# This example modified from routine SANtest()
n<-100 # number of observations to simulate
x <- runif(4 * n, 0, 1) # simulate covariates
x <- array(x, dim = c(4, n)) # put into array for passing to GAMsetup
pi <- asin(1) * 2 # begin simulating some data
y <- 2 * sin(pi * x[1, ])
y <- y + exp(2 * x[2, ]) - 3.75887
y <- y + 0.2 * x[3, ]^11 * (10 * (1 - x[3, ]))^6 + 10 * (10 *
x[3, ])^3 * (1 - x[3, ])^10 - 1.396
sig2<- -1 # set magnitude of variance
e <- rnorm(n, 0, sqrt(abs(sig2)))
y <- y + e # simulated data
w <- matrix(1, n, 1) # weight matrix
par(mfrow = c(2, 2)) # scatter plots of simulated data
plot(x[1, ], y)
plot(x[2, ], y)
plot(x[3, ], y)
plot(x[4, ], y)
G <- list(m = 4, n = n, nsdf = 0, df = c(15, 15, 15, 15),dim=c(1,1,1,1),s.type=c(0,0,0,0),
p.order=c(0,0,0,0), x = x) # creat list for passing to GAMsetup
H <- GAMsetup(G)
H$y <- y # add data to H
H$sig2 <- sig2 # add variance (signalling GCV use in this case) to H
H$w <- w # add weights to H
H$sp<-array(-1,H$m)
H$fix<-array(FALSE,H$m)
H$conv.tol<-1e-6;H$max.half<-15
H <- mgcv(H) # select smoothing parameters and fit model
Run the code above in your browser using DataLab