data(bh1996)
# A simple example with lm
tmod<-lm(WBEING~HRS+LEAD,bh1996)
simple.predict(tmod,~HRS+LEAD,data.frame(HRS=c(5,10),LEAD=c(3.0,4.5)))
predict(tmod,data.frame(HRS=c(5,10),LEAD=c(3.0,4.5))) #Using the predict.lm function
# A simple example with glm
tmod.glm<-glm(ifelse(WBEING>3,1,0)~HRS+LEAD,family="binomial",bh1996)
simple.predict(tmod.glm,~HRS+LEAD,data.frame(HRS=c(5,10),LEAD=c(3.0,4.5)),dichot=TRUE)
predict(tmod.glm,data.frame(HRS=c(5,10),LEAD=c(3.0,4.5)),type="response") #Using the predict.glm function
################################################
# An example including a factor as a predictor #
################################################
#Creating a factor
bh1996$LEAD.F<-ifelse(bh1996$LEAD<2.5,0,ifelse(bh1996$LEAD>3.5,2,1))
bh1996$LEAD.F<-factor(bh1996$LEAD.F,levels=c(0,1,2),labels=c("Lo","Med","Hi"))
#Estimating the original model
tmod<-lm(WBEING~HRS+LEAD.F,bh1996)
#Setting up the predictor dataframe
TDAT<-data.frame(HRS=c(5,10),LEAD.F=c("Lo","Med"))
TDAT$LEAD.F<-factor(TDAT$LEAD.F,levels=c("Lo","Med","Hi")) #This is key in making this work with factors
#Contrasting the predicted values from approaches
simple.predict(tmod,~HRS+LEAD.F,TDAT)
predict(tmod,data.frame(HRS=c(5,10),LEAD.F=c("Lo","Med"))) #Using the predict.lm function
#################################################
# An Example using the lme4 package and lmer #
# Delete the preceding # to execute the example #
#################################################
#library(lme4)
#tmod<-lmer(WBEING~HRS+LEAD+(1|GRP),bh1996)
#simple.predict(tmod,~HRS+LEAD,data.frame(HRS=c(5,10),LEAD=c(3.0,4.5)))
Run the code above in your browser using DataLab