#QUALITATIVE PREDICTORS # READ STRUCTURED DATA TABLE K=read.table("c:/2008LinearModelsData/InsInnovationR.txt") K attach(K) options(digits=7) #FITTING FULL MODEL WITH FACTOR FM=lm(Y~X+FAC) summary(FM) anova(FM) #LOOKING AT FACTORS #READ STRUCTURED DATA TABLE WITH NUMERIC CODED FACTOR K1=read.table("c:/2008LinearModelsData/SurgicalUnit1R.txt") K1 attach(K1) #SETTING VARIABLE X7 AS A FACTOR **FOR NUMERIC CODES** X7=factor(X7c) #VIEWING CONTRAST & DESIGN MATRIX X FM=lm(Y~X1+X2+X3+X4+X5+X6+X7) contrasts(X7) model.matrix(FM) summary(FM) anova(FM) #READ STRUCTURED DATA TABLE WITH TEXT CODED FACTOR K2=read.table("c:/2008LinearModelsData/SurgicalUnit2R.txt") K2 detach(K1) attach(K2) #VIEWING CONTRAST & DESIGN MATRIX X FM=lm(Y~X1+X2+X3+X4+X5+X6+X7) contrasts(X7) model.matrix(FM) summary(FM) anova(FM) #READ STRUCTURED DATA TABLE WITH #INDICATOR CODEs FOR FACTOR K3=read.table("c:/2008LinearModelsData/SurgicalUnit3R.txt") K3 detach(K2) attach(K3) #DIRECT CALCULATION WITH CONTRAST MATRIX X7c=cbind(X7n,X7m,X7h) X7c Ct=contr.treatment(3) Ct #MAKING CODED FACTOR X7=X7c%*%Ct #MATRIX MULTIPLICATION X7 #VIEWING CONTRAST & DESIGN MATRIX X FM=lm(Y~X1+X2+X3+X4+X5+X6+X7) model.matrix(FM) summary(FM) anova(FM) #DIFFERENT CONTRASTS #TREATMENT CONTRASTS (DEFAULT) X7=factor(K1$X7c) contrasts(X7) FM=lm(Y~X1+X2+X3+X4+X5+X6+X7) model.matrix(FM) summary(FM) anova(FM) #SETTING HELMERT CONTRASTS contrasts(K2$X7)=contr.helmert FM=lm(Y~X1+X2+X3+X4+X5+X6+K2$X7) contrasts(K2$X7) model.matrix(FM) summary(FM) anova(FM) #SETTING SUM CONTRASTS contrasts(K2$X7)=contr.sum FM=lm(Y~X1+X2+X3+X4+X5+X6+K2$X7) contrasts(K2$X7) model.matrix(FM) summary(FM) anova(FM)