# http://cran.r-project.org/ AND DOWNLOAD THE R LATEST RELEASE: R-2.x.y.
# http://www.staff.ul.ie/mackenzieg/Assess/R_Integration_Tools/r_integration_tools.html (Crash Course in R)
#
# STEPWISE REGRESSIONS USING AIC = -2*log-likelihood + k*npar
#
library(foreign)
x <- read.spss("suicides.sav")
x1 <- data.frame(x)
x1
attach(x1,2)
m1 <- lm(suicide ~ ., data=x1)
m2 <- step(m1)
#
subdat <- subset(x1,select=c(suicide,prestige,income))
m1 <- lm(suicide ~ ., data=subdat)
m2 <- step(m1)
#
# FOR THE MODEL: SUICIDE = A * PRESTIGE + B * INCOME + ERROR;
#
#
# Log likelihood = -n/2 ln(2 Pi s-squared) - n/2
#                = -n/2 ln(2 Pi RSS/n)     - n/2
#                = -36/2 ln(2 6.28 4445.4/36) - 36/2 = -137.77
#
#            AIC = -2 x -137.77 + 2 x 4 = 283.54
#
library(stats)
logLik(m1)
#
# -2log L is computed from the deviance and uses a different additive constant to logLik and hence AIC.
#
#In Step:     AIC = n ln(RSS/n) + 2 df(model)
#                 = 36 ln(4445.4/36) + 2 x 3
#                 = 179.38
#
# Mallows Cp (SPSS syntax)
#
Cp = [RSS / MSE(full model)] + 2 df(model) - n  (income only predicting suicides)
   = [4572.87 / 134.71] + 2 x 2 - 36
   = 1.946

Cp = [4914.39 / 134.71] + (2 x 1) - 36 for null model with just constant term
   = 2.48 > 1.946 (= Cp using in come only as predictor)

AIC = 36 ln(4914.39/36) + 2 =  178.99 > 178.4 (= AIC using income only as a predictor)