Monday, September 20, 2010

R: Multiple Regression

# In general, I didn't code this myself.

# lib(s): MASS
# Note to self: Multiple regression is covered in Linear Models I of LSE's Computational Statistics course by Penzer.

# Red indicates stuff you have to edit.

# Before using this script:
library(MASS)

# Data input: generic

# alpha tested ok on 3 Aug 2010
# For data frame 'infile':

MultiReg1 <- lm(dependent.var~independent.var1+independent.var2+independent.var3+...+independent.var,data=infile) # beta tested ok against result from STATA 10.
summary(MultiReg1)
anova(MultiReg1)# (sequential) anova table in the order which the variables are specified. Changing the order in which the independent variables are specified will change the results of the anova.
# par(mfrow = c(2,2))# Uncomment if you want all 4 plots in one window
plot(MultiReg1)
# par(mfrow = c(1,1)) # returns plot window to single plot mode.
boxcox(MultiReg1)
# To add variables into the model:
addterm(MultiReg1, ~.+Var1+Var2+Var3+...+Var, test="F") # adds a single term to those listed and displays the corresponding F-stat. ~. denotes the existing model.
# To remove variables from the model:
MultiReg2 <- update(MultiReg1,~(.-Var1),data=infile)

# Other useful functions # - alpha tested ok on 3 Aug 2010 -
coefficients(MultiReg) # model coefficients
confint(MultiReg, level=0.95) # CIs for model parameters
fitted(MultiReg) # predicted values
residuals(MultiReg) # residuals
vcov(MultiReg) # covariance matrix for model parameters
influence(MultiReg) # regression diagnostics
# Backwards Elimination # alpha tested ok on 2 Aug 2010
MultiReg <- lm(DepVar~indep.var1+indep.var2+indep.var3+...+IndepVar)
dropterm(MultiReg, test="F")
MultiReg2 <- update(MultiReg, ~.-IndepVar)
dropterm(MultiReg2,test="F") # to drop more variables, repeat this dropterm(...) and NewReg <- update(OldReg, ~.-VarToDrp) as necessary.

No comments:

Post a Comment