#POLYNOMIAL REGRESSION # READ STRUCTURED DATA TABLE K=read.table("c:/2008LinearModelsData/PowerCellsR.txt") K attach(K) options(digits=10) #SCALE AND SQUARE/MULTIPLY X DATA ONLY x1=scale(X1,center=T,scale=0.4) x2=scale(X2,center=T,scale=10) x1s=x1^2 x2s=x2^2 x1x2=x1*x2 #REFORMAT AS DATAFRAME Kscaled=data.frame(Y,x1,x2,x1s,x2s,x1x2) Kscaled detach(K) attach(Kscaled) #FITTING FULL LINEAR MODEL FM=lm(Y~x1+x2+x1s+x2s+x1x2) summary(FM) #PARTIAL F TEST FOR HIGHER ORDER VARIABLES anova(FM) 529+284.928+1645.967 F=((529+284.928+1645.967)/3)/1048.088 F qf(0.95,3,5) #GLM TEST FOR HIGHER ORDER VARIABLES FM=lm(Y~x1+x2+x1s+x2s+x1x2) RM=lm(Y~x1+x2) anova(RM,FM) #++++++++++++++++++++++++++++++++++++++++++++++ #INTERACTIONS REGRESSION # READ A STRUCTURED DATA TABLE K=read.table("c:/2008LinearModelsData/BodyFatR.txt") K attach(K) #CENTERING X VALUES X=cbind(X1,X2,X3) Xc=data.frame(scale(X,center=T,scale=FALSE)) Xc attach(Xc) #MAKING INTERACTION VARIABLES X1X2=X1*X2 X1X3=X1*X3 X2X3=X2*X3 #FIT OF THE FULL LINEAR MODEL AND REPORT FM=lm(Y~X1+X2+X3+X1X2+X1X3+X2X3) summary(FM) #PARTIAL F TEST FOR HIGHER ORDER VARIABLES anova(FM) F=((6.51484+2.70433+1.49572)/3)/6.74538 F qf(0.95,3,13) #GLM TEST FOR HIGHER ORDER VARIABLES FM=lm(Y~X1+X2+X3+X1X2+X1X3+X2X3) RM=lm(Y~X1+X2+X3) anova(RM,FM) #+++++++++++++++++++++++++++++++++++++++ #USING FORMULA OPTIONS IN R: options(digits=7) #FULLY CROSSED MODEL WITH ALL HIGHER-ORDER VARIABLES FM=lm(Y~X1*X2*X3) summary(FM) anova(FM) #SPECIFYING HIGHER-ORDER VARIABLES FM=lm(Y~X1+X2+X3+X1:X2+X1:X3+X2:X3) summary(FM) anova(FM)