# NOT RUN {
# }
# NOT RUN {
## Concave 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.45
dat <- data.frame(x=x,y=y)
b <- scam(y~s(x,k=15,bs="cv",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,15*exp(f))
dat <- data.frame(x=x,y=y)
## fit model ...
b <- scam(y~s(x,k=15,bs="cv",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,15*exp(f)) ## the true function
lines(x,b$fitted,col=2) ## constrained fit
lines(x,b1$fitted,col=3) ## unconstrained fit
## plotting on log scale...
plot(x,log(15*exp(f)),type="l") ## the true function
lines(x,log(b$fitted),col=2) ## constrained fit
lines(x,log(b1$fitted),col=3) ## unconstrained fit
# }
Run the code above in your browser using DataLab