## Nonlinear function with R (splines)

1) cubic splines – use bs() for bases
2) smoothing splines –  use LOO to select smoothing splines
4) GAM for multiple covariates – use s() for smoothing splines, plot.gam(), compare models using anova()

Note; ns=natural splines

[youtube https://www.youtube.com/watch?v=DCn83aXXuHc]

## Fitting nonlinear function in R

1) polynomials or polynomial regressions
2) interested in the fitted function predict(), plot(), fitted()
*use anova() to compare models
3) other than polynomials are splines (not discussed herel

[youtube https://www.youtube.com/watch?v=uQBnDGu6TYU]

## Generalized additive models

1) What is local regression for nonlinear function
2) loess() function
3) generalized additive model for nonlinear function. Retain the additive model
4) fit then plot
5) gam through natural splines or others like smooth splines, local regression,
6) coefficients not interesting. But fitted function yes. Use plot.gam()
7) can include interaction as covariate

[youtube https://www.youtube.com/watch?v=N2hBXqPiegQ]

## A nice note on Generalized additive model

In this page,the author has made clear the steps of using GAM in applied data analysis. The introduction is short but adequate. The code is simple and uses the default arguments. The comparison model is shown using anova function. The interpretation using plot is wonderful

http://ecology.msu.montana.edu/labdsv/R/labs/lab5/lab5.html

## Using GAM (generalized additive model) to fit non-linear data

This is very introductory. The main aim is to to show the capability of GAM (package mgcv) in R to fit non-linear data. Briefly, GAM will identify whether a predictor or many predictor in a univariable or multivariable model needs/need to be in its linear form or need not. Of course GAM in mgcv can do much more including fit the model to the test data but it is beyond this very short note. CHECK THE PDF file here for much better presentation (Generalized Additive Model)

#gen predictor x
data.x<-rnorm(n=50,mean=25,sd=15)
#gen square predictor x
data.x2<-data.x^2
#gen outcome y1
data.y1<-1*data.x
#plot
qplot(x=data.x,y=data.y1)
#gen outcome y2
data.y2<-1*data.x2
#plot
qplot(x=data.x,y=data.y2)
#describe x
summary(data.x)
#gen e (error) – mean=0
data.e<-rnorm(n=50,mean=0,sd=150)
#gen data y2 with random error
data.y2<-1*data.x2+data.e
#plot –> curvilinear
qplot(x=data.x,y=data.y2)
#use gam, predictor x fixed as linear (untransformed)
mod.lin<-gam(data.y2~data.x)
summary(mod.lin)
#use gam, with smoothing applied to predictor x
mod.gam<-gam(data.y2~s(data.x))
#check GCV, check R-squared – smoothed model is better
summary(mod.gam)
#assess model with x fixed as linear – assumption of normality not met
gam.check(mod.lin)
#assess smoothed model – residuals are ok
gam.check(mod.gam)
# compare model
anova( mod.gam, mod.lin,test=’Chisq’)
#model with smoothed x has significantly better fit, p<0.05