# NOT RUN {
# }
# NOT RUN {
## Convex P-splines example
## simulating data...
set.seed(1)
n <- 100
x <- sort(2*runif(n)-1)
f <- 4*x^2
y <- f + rnorm(n)*0.4
dat <- data.frame(x=x,y=y)
b <- scam(y~s(x,k=15,bs="cx",m=2),family=gaussian,data=dat,not.exp=FALSE)
# UNCONSTRAINED FIT *****************
b1 <- scam(y~s(x,k=15,bs="cr",m=2),family=gaussian, data=dat,not.exp=FALSE)
## plot results ...
plot(x,y,xlab="x",ylab="y")
lines(x,f) ## the true function
lines(x,b$fitted,col=2) ## constrained fit
lines(x,b1$fitted,col=3) ## unconstrained fit
## Poisson version...
y <- rpois(n,exp(f))
dat <- data.frame(x=x,y=y)
## fit model ...
b <- scam(y~s(x,k=15,bs="cx",m=2),family=poisson(link="log"),data=dat,not.exp=FALSE)
# UNCONSTRAINED FIT *****************
b1 <- scam(y~s(x,k=15,bs="cr",m=2),family=poisson(link="log"), data=dat,not.exp=FALSE)
## plot results ...
plot(x,y,xlab="x",ylab="y")
lines(x,exp(f)) ## the true function
lines(x,b$fitted,col=2) ## constrained fit
lines(x,b1$fitted,col=3) ## unconstrained fit
# }
Run the code above in your browser using DataLab