FUNDAMENTALS OF R

 

# This file is intended to get you started with R.

# First read in the data and examine it.

 

Example <- read.table("C:/users/wood/Documents/My Teaching/Maximum Likelihood/Data/EXAMPLE.txt", header=TRUE)

attach(Example)

Example

 

# Now summarize the data for all variables.

 

summary(Example)

 

# Now compute the mean, variance, and standard deviation for a single variable.

 

mean(X1)

var(X1)

sd(X1)

median(X1)

max(X1)

min(X1)

 

# Now compute the skewness and kurtosis using the e1071 library

 

library(e1071)

skewness(X1)

kurtosis(X1)

 

# Now create a new variable and add it to the active data file.

 

Example$NEWVAR <- Y+X1+X2+X3

detach(Example)

attach(Example)

 

# Now do a correlation matrix among the original variables.

 

cor(Example[,c("X1","X2","X3","Y")], use="complete.obs")

 

# Now do a scatterplot between two variables with a superimposed regression line,

# a parametric regression line, and box plots for each variable.

 

library(car)

scatterplot(Y~X1, reg.line=lm, boxplots='xy', smooth=TRUE, span =0.5, data=Example)

 

# Now create two variables X1*X2 and X3 squared and add to the active data.

 

Example$X1X2 <- X1 * X2

Example$X3sqr <- X3^2

detach(Example)

attach(Example)

 

# Get descriptive statistics on the new variables

 

summary(X1X2)

var(X1X2)

sd(X1X2)

summary(X3sqr)

var(X3sqr)

sd(X3sqr)

 

# Look at the entire dataset.

 

Example

 

# Now regress Y on X1, X2, X1X2, and X3sqr and look at the output object.

 

regress.model <- lm(Y ~ X1 + X2 + X1X2 + X3sqr)

summary(regress.model)

 

# Now add the residuals and predicted values to the dataset.

 

Example$residuals <- residuals(regress.model)

Example$fitted <- fitted(regress.model)

detach(Example)

attach(Example)

 

# Now list and plot the residuals and predicted values

 

residuals

plot(residuals)

fitted

plot(fitted)

 

# Now get the analysis of variance table for the regression.

 

anova(regress.model)

 

# Now construct a CUSUM plot for model stability.

 

library(strucchange)

plot(efp(Y ~ X1 + X2 + X1X2 + X3sqr, type = "Rec-CUSUM"))