#Program to analyze the data in Table 7.1 for Example 7.1
library(wle)
library(car)
yd=read.table("C:\\Research\\Book\\Data\\table71.txt")
x1 = yd[[2]]
x2 = yd[[3]]
x3 = yd[[4]]
x4 = yd[[5]]
y =  yd[[6]]

#Regression analysis with y as response and x1-x4 as predictors
md0= y ~ 1
lreg0= lm(md0)
md = y ~ x1 + x2 + x3 + x4
lreg = lm(md)
lreg
summary.lm(lreg)
anova.lm(lreg)


# Function to compute the C(p)
mallows=mle.cp(md)
mallows
mallow1 = mallows[[1]]; C.p=mallow1[,6]
mallow2 = mallows[[2]]
cbind(mallow2,C.p)

# Function to run stepwise selection
stepwise=step(lreg, direction="both")
summary(stepwise)
anova(stepwise)

# Function to run backward selection
step(lreg, direction="backward")

# Function to run forward selection
step(lreg0, scope=list(lower=md0, upper=md), direction="forward")

# Function to test the linear hypothesis x = 0
bmo = y ~ x1 + x2 + x4
lreg1 = lm(bmo)
linearHypothesis(lreg1, "x2=0")

linearHypothesis(lreg, "x3=0")
#anova(lreg1,lreg)

#####################################################################

#Program to analyze the gifted children data for Example 7.2
library(wle)
yd=read.table("C:\\Research\\Lawal_Book\\Data\\example72.txt")
y  = yd[[1]]
x1 = yd[[2]]
x2 = yd[[3]]
x3 = yd[[4]]
x4 = yd[[5]]
x5 = yd[[6]]
x6 = yd[[7]]
x7 = yd[[8]]

#Regression analysis with y as response and x1-x7 as predictors
md0= y ~ 1
lreg0= lm(md0)
md = y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7
lreg = lm(md)
lreg
summary.lm(lreg)
anova.lm(lreg)

mallows=mle.cp(md)
mallows

# Function to run stepwise selection
step(lreg, direction="both")

# Function to run backward selection
step(lreg, direction="backward")

# Function to run forward selection
step(lreg0, scope=list(lower=md0, upper=md), direction="forward")







