#Generalized additive models are a conceptually straightforward tool that allows one to incorporate 
#nonlinear predictor effects into their otherwise linear models. In addition, they allow one to keep 
#within the linear and generalized linear frameworks with which one is already fa- miliar, while 
#providing new avenues of model exploration and possibly improved results. 
d = read.csv("http://www.nd.edu/~mclark19/learn/data/pisasci2006.csv")
library(psych)
## Warning: package 'psych' was built under R version 3.1.2
describe(d)[-1, 1:9] #univariate
## Warning in FUN(newX[, i], ...): no non-missing arguments to min; returning
## Inf
## Warning in FUN(newX[, i], ...): no non-missing arguments to max; returning
## -Inf
##          vars  n   mean    sd median trimmed   mad    min    max
## Overall     2 57 473.14 54.58 489.00  477.45 48.93 322.00 563.00
## Issues      3 57 469.91 53.93 489.00  474.28 41.51 321.00 555.00
## Explain     4 57 475.02 54.02 490.00  478.85 47.44 334.00 566.00
## Evidence    5 57 469.81 61.74 489.00  475.11 54.86 288.00 567.00
## Interest    6 57 528.16 49.84 522.00  525.72 53.37 448.00 644.00
## Support     7 57 512.18 26.08 512.00  512.15 25.20 447.00 569.00
## Income      8 61   0.74  0.11   0.76    0.75  0.11   0.41   0.94
## Health      9 61   0.89  0.07   0.89    0.89  0.08   0.72   0.99
## Edu        10 59   0.80  0.11   0.81    0.80  0.12   0.54   0.99
## HDI        11 59   0.81  0.09   0.82    0.81  0.09   0.58   0.94
library(car)
## Warning: package 'car' was built under R version 3.1.2
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:psych':
## 
##     logit
scatterplotMatrix(d[,-c(1,3:5)],pch=19,cex=.5,reg.line=F, lwd.smooth=1.25,
                  spread=F,ellipse=T, col=c('gray60','#2957FF','#FF8000'), col.axis='gray50')
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.window(...): "lwd.smooth" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "lwd.smooth" is not a graphical
## parameter
## Warning in title(...): "lwd.smooth" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "lwd.smooth"
## is not a graphical parameter

library(ggplot2); library(reshape2)
## 
## Attaching package: 'ggplot2'
## 
## The following object is masked from 'package:psych':
## 
##     %+%
## Warning: package 'reshape2' was built under R version 3.1.2
dmelt = melt(d, id=c('Country','Overall'),
             measure=c('Interest','Support','Income','Health','Edu','HDI'))
head(d)
##      Country Overall Issues Explain Evidence Interest Support Income
## 1    Albania      NA     NA      NA       NA       NA      NA  0.599
## 2  Argentina     391    395     386      385      567     506  0.678
## 3  Australia     527    535     520      531      465     487  0.826
## 4    Austria     511    505     516      505      507     515  0.835
## 5 Azerbaijan     382    353     412      344      612     542  0.566
## 6    Belgium     510    515     503      516      503     492  0.831
##   Health   Edu   HDI
## 1  0.886 0.716 0.724
## 2  0.868 0.786 0.773
## 3  0.965 0.978 0.920
## 4  0.944 0.824 0.866
## 5  0.780    NA    NA
## 6  0.935 0.868 0.877
#leave the smooth off for now
ggplot(aes(x=value,y=Overall), data=dmelt) + geom_point(color='#FF8000',alpha=.75) +
    #geom_smooth(se=F) +
    geom_text(aes(label=Country), alpha=.25, size=1,angle=30, hjust=-.2, vjust=-.2) +
    facet_wrap(~variable, scales='free_x') 
## Warning: Removed 8 rows containing missing values (geom_point).
## Warning: Removed 8 rows containing missing values (geom_point).
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning: Removed 13 rows containing missing values (geom_point).
## Warning: Removed 13 rows containing missing values (geom_point).
## Warning: Removed 8 rows containing missing values (geom_text).
## Warning: Removed 8 rows containing missing values (geom_text).
## Warning: Removed 11 rows containing missing values (geom_text).
## Warning: Removed 11 rows containing missing values (geom_text).
## Warning: Removed 13 rows containing missing values (geom_text).
## Warning: Removed 13 rows containing missing values (geom_text).

library(mgcv)
## Warning: package 'mgcv' was built under R version 3.1.2
## Loading required package: nlme
## Warning: package 'nlme' was built under R version 3.1.2
## This is mgcv 1.8-4. For overview type 'help("mgcv-package")'.
ggplot(aes(x=value,y=Overall), data=dmelt) +
    geom_point(color='#FF8000',alpha=.75) +
    geom_smooth(se=F, method='gam', formula=y~s(x), color='#2957FF') + facet_wrap(~variable, scales='free_x')
## Warning: Removed 8 rows containing missing values (stat_smooth).
## Warning: Removed 8 rows containing missing values (stat_smooth).
## Warning: Removed 11 rows containing missing values (stat_smooth).
## Warning: Removed 11 rows containing missing values (stat_smooth).
## Warning: Removed 13 rows containing missing values (stat_smooth).
## Warning: Removed 13 rows containing missing values (stat_smooth).
## Warning: Removed 8 rows containing missing values (geom_point).
## Warning: Removed 8 rows containing missing values (geom_point).
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning: Removed 11 rows containing missing values (geom_point).
## Warning: Removed 13 rows containing missing values (geom_point).
## Warning: Removed 13 rows containing missing values (geom_point).

#    ggtheme

mod_gam1 <- gam(Overall ~ s(Income, bs = "cr"), data = d) 
summary(mod_gam1)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## Overall ~ s(Income, bs = "cr")
## 
## Parametric coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  470.444      4.082   115.3   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##             edf Ref.df     F  p-value    
## s(Income) 6.895  7.741 16.43 1.97e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =    0.7   Deviance explained = 73.9%
## GCV = 1053.7  Scale est. = 899.67    n = 54
plot(mod_gam1)

#AIC(mod_lm)
## [1] 550.2
#summary(mod_lm)$sp.criterion
## [1] 1504
#summary(mod_lm)$r.sq #adjusted R squared

#aic_lm aic_gam1 gcv_lm gcv_gam1 rsq_lm rsq_gam1 
#550.24 529.81 1504.50 1053.73 0.52 0.70

#anova(mod_lm, mod_gam1, test = "Chisq")


#polynomial regression and GAMs
#ffects are nonlinear in the param- eters

#Response Transformation