Docsity

SAS Two-Way ANOVA: Sales Data, Factor A (Height), Factor B (Width), Slides of Designs and Groups

Toggle sidebar
bg1
Statistics 512: Applied Linear Models
Topic 7
Topic Overview
This topic will cover
•Two-way Analysis of Variance (ANOVA)
•Interactions
Chapter 19: Two-way ANOVA
The response variable Yis continuous.
There are now two categorical explanatory variables (factors). Call them factor Aand
factor Binstead of X1and X2. (We will have enough subscripts as it is!)
Data for Two-way ANOVA
•Y, the response variable
•Fac tor Awith levels i=1toa
•Fac tor Bwith levels j=1tob
•A particular combination of levels is called a treatment or a cell. There are ab treat-
ments.
•Yi,j,k is the kth observation for treatment (i, j), k=1ton
In Chapter 19, we for now assume equal sample size in each treatment combination (ni,j =
n>1; nT=abn). This is called a balanced design. In later chapters we will deal with
unequal sample sizes, but it is more complicated.
Notation
For Yi,j,k the subscripts are interpreted as follows:
•idenotes the level of the factor A
•jdenotes the level of the factor B
•kdenotes the kth observation in cell or treatment (i, j)
i=1,... ,a levels of factor A
j=1,... ,b levels of factor B
k=1,... ,n observations in cell (i, j)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download SAS Two-Way ANOVA: Sales Data, Factor A (Height), Factor B (Width) and more Slides Designs and Groups in PDF only on Docsity!

Statistics 512: Applied Linear Models

Topic 7

Topic Overview

This topic will cover

  • Two-way Analysis of Variance (ANOVA)
  • Interactions

Chapter 19: Two-way ANOVA

The response variable Y is continuous.

There are now two categorical explanatory variables (factors). Call them factor A and factor B instead of X 1 and X 2. (We will have enough subscripts as it is!)

Data for Two-way ANOVA

  • Y , the response variable
  • Factor A with levels i = 1 to a
  • Factor B with levels j = 1 to b
  • A particular combination of levels is called a treatment or a cell. There are ab treat- ments.
  • Yi,j,k is the kth observation for treatment (i, j), k = 1 to n

In Chapter 19, we for now assume equal sample size in each treatment combination (ni,j = n > 1; nT = abn). This is called a balanced design. In later chapters we will deal with unequal sample sizes, but it is more complicated.

Notation

For Yi,j,k the subscripts are interpreted as follows:

  • i denotes the level of the factor A
  • j denotes the level of the factor B
  • k denotes the kth observation in cell or treatment (i, j)

i = 1,... , a levels of factor A j = 1,... , b levels of factor B k = 1,... , n observations in cell (i, j)

KNNL Example

  • KNNL page 832 (nknw817.sas)
  • response Y is the number of cases of bread sold.
  • factor A is the height of the shelf display; a = 3 levels: bottom, middle, top.
  • factor B is the width of the shelf display; b = 2 levels: regular, wide.
  • n = 2 stores for each of the 3 × 2 = 6 treatment combinations (nT = 12)

Read the data

data bread; infile ’h:\System\Desktop\CH19TA07.DAT’; input sales height width; proc print data=bread;

Obs sales height width 1 47 1 1 2 43 1 1 3 46 1 2 4 40 1 2 5 62 2 1 6 68 2 1 7 67 2 2 8 71 2 2 9 41 3 1 10 39 3 1 11 42 3 2 12 46 3 2

Model Assumptions

We assume that the response variable observations are independent, and normally distributed with a mean that may depend on the levels of the factors A and B, and a variance that does not (is constant).

Cell Means Model

Yi,j,k = μi,j + i,j,k where

  • μi,j is the theoretical mean or expected value of all observations in cell (i, j).
  • the i,j,k are iid N (0, σ^2 )
  • Yi,j,k ∼ N (μi,j , σ^2 ), independent

There are ab + 1 parameters of the model: μi,j , for i = 1 to a and j = 1 to b; and σ^2.

means statement height × width

Level of Level of ------------sales------------ height width N Mean Std Dev 1 1 2 45.0000000 2. 1 2 2 43.0000000 4. 2 1 2 65.0000000 4. 2 2 2 69.0000000 2. 3 1 2 40.0000000 1. 3 2 2 44.0000000 2.

Code the factor levels and plot

(We’re just doing this for a nice plot; it is not necessary for the analysis.)

data bread; set bread; if height eq 1 and width eq 1 then hw=’1_BR’; if height eq 1 and width eq 2 then hw=’2_BW’; if height eq 2 and width eq 1 then hw=’3_MR’; if height eq 2 and width eq 2 then hw=’4_MW’; if height eq 3 and width eq 1 then hw=’5_TR’; if height eq 3 and width eq 2 then hw=’6_TW’; title2 ’Sales vs. treatment’; symbol1 v=circle i=none; proc gplot data=bread; plot sales*hw;

Put the means in a new dataset

proc means data=bread; var sales; by height width;

output out=avbread mean=avsales; proc print data=avbread;

Obs height width TYPE FREQ avsales 1 1 1 0 2 45 2 1 2 0 2 43 3 2 1 0 2 65 4 2 2 0 2 69 5 3 1 0 2 40 6 3 2 0 2 44

Plot the means

Recall the plotting syntax to get two separate lines for the two width levels. We can also do a plot of sales vs width with three lines for the three heights.

This type of plot is called an “interaction plot” for reasons that we will see later.

symbol1 v=square i=join c=black; symbol2 v=diamond i=join c=black; symbol3 v=circle i=join c=black; proc gplot data=avbread; plot avsalesheight=width; plot avsaleswidth=height;

The Interaction plots

Questions

Does the height of the display affect sales? If yes, compare top with middle, top with bottom, and middle with bottom. Does the width of the display affect sales? Does the effect of height on sales depend on the width? Does the effect of width on sales depend on the height? If yes to the last two, that is an interaction. Notice that these questions are not straightforward to answer using the cell means model.

Zero-sum Constraints

As in the one-way model, we now have too many parameters and need now several con- straints:

α. =

i

αi = 0

β. =

j

βj = 0

(αβ).j =

i

(αβ)i,j = 0 ∀j (for all j)

(αβ)i. =

j

(αβ)i,j = 0 ∀i (for all i)

Estimates for Factor-effects model

ˆμ = Y¯... =

i,j,k Yi,j,k abn μ ˆi. = Y¯i.. and μˆ.j = Y¯.j. α ˆi = Y¯i.. − Y¯... and βˆj = Y¯.j. − Y¯... (αβ^ ˆ) i,j =^ Y¯i,j.^ −^ Y¯i..^ −^ Y¯.j.^ + Y¯...

SS for ANOVA Table

SSA =

i,j,k αˆ

2 i =^

i,j,k( Y¯i.. − Y¯...)^2 = nb ∑ i( Y¯i.. − Y¯ )^2 factor A sum of squares SSB =

i,j,k βˆ^2 j =^

i,j,k( Y¯.j.^ −^ Y¯...)

(^2) = na ∑ j ( Y¯.j.^ −^ Y¯^ )

(^2) factor B sum of squares

SSAB =

i,j,k (αβˆ)^2 i,j =^ n^

i,j (αβˆ)^2 i,j AB^ interaction sum of squares SSE =

i,j,k(Yi,j,k^ −^ Y¯i,j.)

i,j,k e

2 i,j,k error sum of squares SST =

i,j,k(Yi,j,k^ −^ Y¯...)^2 total sum of squares SSM = SSA + SSB + SSAB = SST − SSE model sum of squares SST = SSA + SSB + SSAB + SSE = SSM + SSE

df for ANOVA Table

dfA = a − 1 dfB = b − 1 dfAB = (a − 1)(b − 1) dfE = ab(n − 1) dfT = abn − 1 = nT − 1 dfM = a − 1 + b − 1 + (a − 1)(b − 1) = ab − 1

MS for ANOVA Table

(no surprises)

M SA = SSA/dfA M SB = SSB/dfB M SAB = SSAB/dfAB M SE = SSE/dfE M ST = SST /dfT M SM = SSM/dfM

Hypotheses for two-way ANOVA

Test for Factor A Effect

H 0 : αi = 0 for all i Ha : αi 6 = 0 for at least one i

The F statistic for this test is FA = M SA/M SE and under the null hypothesis this follows an F distribution with dfA, dfE.

Test for Factor B Effect

H 0 : βj = 0 for all j Ha : βj 6 = 0 for at least one j

The F statistic for this test is FB = M SB/M SE and under the null hypothesis this follows an F distribution with dfB , dfE.

Test for Interaction Effect

H 0 : (αβ)i,j = 0 for all (i, j) Ha : (αβ)i,j 6 = 0 for at least one (i, j)

The F statistic for this test is FAB = M SAB/M SE and under the null hypothesis this follows an F distribution with dfAB , dfE.

F -statistics for the tests

Notice that the denominator is always M SE and the denominator df is always dfE ; the numerators change depending on the test. This is true as long as the effects are fixed. That is to say that the levels of our variables are of intrinsic interest in themselves - they are fixed by the experimenter and not considered to be a sample from a larger population of factor levels. For random effects we would need to do something different (more later).

  • Type III SS is like Type II SS (variable added last) but it also adjusts for differing ni,j. So if all cells have the same number of observations (balanced designs are nice - the variables height and width in our example are independent - no multicollinearity!) SS1, SS2, and SS3 will all be the same.
  • More details on SS later.

Other output

R-Square Coeff Var Root MSE sales Mean 0.962241 6.303040 3.214550 51.

Results

  • The interaction between height and width is not statistically significant (F = 1.16; df = (2, 6); p = 0.37). NOTE: Check Interaction FIRST! If it is significant then main effects are left in the model, even if not significant themselves! We may now go on to examine main effects since our interaction is not significant.
  • The main effect of height is statistically significant (F = 74.71; df = (2, 6); p =
    1. 75 × 10 −^5 ).
  • The main effect of width is not statistically significant (F = 1.16; df = (1, 6); p = 0.32)

Interpretation

  • The height of the display affects sales of bread.
  • The width of the display has no apparent effect.
  • The effect of the height of the display is similar for both the regular and the wide widths.

Additional Analyses

  • We will need to do additional analyses to understand the height effect (factor A).
  • There were three levels: bottom, middle and top. Based on the interaction picture, it appears the middle shelf increases sales.
  • We could rerun the data with a one-way anova and use the methods we learned in the previous chapters to show this (e.g. tukey)..

Parameter Estimation

Cell Means Model

Yi,j,k = μi,j + i,j,k, where

  • μi,j is the theoretical mean or expected value of all observations in cell (i, j).
  • i,j,k ∼iid^ N (0, σ^2 )
  • Yi,j,k ∼ N (μi,j,k, σ^2 ) are independent
  • There are ab + 1 parameters of the model: μi,j ; i = 1,... , a, j = 1,... , b and σ^2.

For the bread example, estimate the μi,j with Y¯i,j. which we can get from the means height*width statement:

μˆ 1 , 1 = Y¯ 1 , 1. = 45 μˆ 1 , 2 = Y¯ 1 , 2. = 43 μˆ 2 , 1 = Y¯ 2 , 1. = 65 μˆ 2 , 2 = Y¯ 2 , 2. = 69 μˆ 3 , 1 = Y¯ 3 , 1. = 40 μˆ 3 , 2 = Y¯ 3 , 2. = 44

As usual, σ^2 is estimated by M SE.

Factor Effects Model

μi,j = μ + αi + βj + (αβ)i,j , where

  • μ is the overall (grand) mean - it is μ.. in KNNL
  • αi is the main effect of Factor A
  • βj is the main effect of Factor B
  • (αβ)i,j is the interaction effect between A and B. Note that (αβ)i,j is the name of a parameter all on its own and does not refer to the product of α and β.

Overall Mean

The overall mean is estimated as ˆμ = Y¯... = 51 under the zero-sum constraining. (This is sales mean in the glm output). You can get a whole dataset of this value by using a model statement with no right hand side, e.g. model sales=; and storing the predicted values.

Doing this in SAS

Unlike proc reg, you are only allowed one model statement per call to glm. But this at least saves you doing the arithmetic by hand.

proc glm data=bread; class height width; model sales=; output out=pmu p=muhat; proc glm data=bread; class height width; model sales=height; output out=pA p=Amean; proc glm data=bread; class height width; model sales=width; output out=pB p=Bmean; proc glm data=bread; class height width; model sales=height*width; output out=pAB p=ABmean; data parmest; merge bread pmu pA pB pAB; alpha = Amean - muhat; beta = Bmean - muhat; alphabeta = ABmean - (muhat+alpha+beta); proc print data=parmest;

Obs sales height width muhat Amean Bmean ABmean alpha beta alphabeta 1 47 1 1 51 44 50 45 -7 -1 2 2 43 1 1 51 44 50 45 -7 -1 2 3 46 1 2 51 44 52 43 -7 1 - 4 40 1 2 51 44 52 43 -7 1 - 5 62 2 1 51 67 50 65 16 -1 - 6 68 2 1 51 67 50 65 16 -1 - 7 67 2 2 51 67 52 69 16 1 1 8 71 2 2 51 67 52 69 16 1 1 9 41 3 1 51 42 50 40 -9 -1 - 10 39 3 1 51 42 50 40 -9 -1 - 11 42 3 2 51 42 52 44 -9 1 1 11 46 3 2 51 42 52 44 -9 1 1

Zero-sum Constraints

α. =

i

αi = 0

β. =

j

βj = 0

(αβ).j =

i

(αβ)i,j = 0 ∀j (for all j)

(αβ).i =

i

(αβ)i,j = 0 ∀i (for all i)

All of these constraints are satisfied by the above estimates. Notice how these main and interaction effects fit together to give back the treatment means:

45 = 51 − 7 − 1 + 2 43 = 51 − 7 + 1 − 2 65 = 51 + 16 − 1 − 1 69 = 51 + 16 + 1 + 1 40 = 51 − 9 − 1 − 1 44 = 51 − 9 + 1 + 1

SAS GLM Constraints

As usual, SAS has to do its constraints differently. As in one-way ANOVA, it sets the parameter for the last category equal to zero.

αa = 0 (1 constraint) βb = 0 (1 constraint) (αβ)a,j = 0 for all j (b constraints) (αβ)i,b = 0 for all i (a constraints)

The total is 1 + 1 + a + b − 1 = a + b + 1 constraints (the constraint (αβ)a,b is counted twice above).

Parameters and constraints

The cell means model has ab parameters for the means. The factor effects model has (1 + a + b + ab) parameters.

  • An intercept (1)
  • Main effect of A (a)

Means

The estimated treatment means are ˆμi,j = ˆμ + ˆαi + βˆj + (αβˆ)i,j.

height width N Mean 1 1 2 45.0000000 = 44 - 1 - 4 + 6 1 2 2 43.0000000 = 44 - 1 + 0 + 0 2 1 2 65.0000000 = 44 + 25 - 4 + 0 2 2 2 69.0000000 = 44 + 25 + 0 + 0 3 1 2 40.0000000 = 44 + 0 - 4 + 0 3 2 2 44.0000000 = 44 + 0 + 0 + 0

ANOVA Table

Source df SS MS F A a − 1 SSA M SA M SA/M SE B b − 1 SSB M SB M SB/M SE AB (a − 1)(b − 1) SSAB M SAB M SAB/M SE Error ab(n − 1) SSE M SE Total abn − 1 SST O M ST

Expected Mean Squares

E(M SE) = σ^2

E(M SA) = σ^2 +

nb a − 1

i

α^2 i

E(M SB) = σ^2 +

na b − 1

j

β^2 j

E(M SAB) = σ^2 +

n (a − 1)(b − 1)

i,j

(αβ)^2 i,j

Here, αi, βj , and (αβ)i,j are defined with the usual zero-sum constraints.

Analytical strategies

  • Run the model with main effects and the two-way interaction.
  • Plot the data, the means and look at the residuals.
  • Check the significance test for the interaction.

What if AB interaction is not significant?

If the AB interaction is not statistically significant, you could rerun the analysis without the interaction (see discussion of pooling KNNL Section 19.10). This will put the SS and df for AB into Error. Results of main effect hypothesis tests could change because M SE

and denominator df have changed (more impact with small sample size). If one main effect is not significant...

  • There is no evidence to conclude that the levels of this explanatory variable are asso- ciated with different means of the response variable.
  • Model could be rerun without this factor giving a one-way ANOVA.

If neither main effect is significant...

  • Model could be run as Y=; (i.e. no factors at all)
  • A one population model
  • This seems silly, but this syntax can be useful for getting parameter estimates in the preferred constraint system (see below).

For a main effect with more than two levels that is significant, use the means statement with the Tukey multiple comparison procedure. Contrasts and linear combinations can also be examined using the contrast and estimate statements.

If AB interaction is significant but not important

  • Plots and a careful examination of the cell means may indicate that the interaction is not very important even though it is statistically significant.
  • For example, the interaction effect may be much smaller in magnitude than the main effects; or may only be apparent in a small number of treatments.
  • Use the marginal means for each significant main effect to describe the important results for the main effects.
  • You may need to qualify these results using the interaction.
  • Keep the interaction in the model.
  • Carefully interpret the marginal means as averages over the levels of the other factor.
  • KNNL also discuss ways that transformations can sometimes eliminate interactions.

If AB interaction is significant and important

The interaction effect is so large and/or pervasive that main effects cannot be interpreted on their own. Options include the following:

  • Treat as a one-way ANOVA with ab levels; use Tukey to compare means; contrasts and estimate can also be useful.
  • Report that the interaction is significant; plot the means and describe the pattern.
  • Analyze the levels of A for each level of B (use a by statement) or vice versa

Tukey Output

Mean N height

A 67.000 4 2

B 44.000 4 1 B B 42.000 4 3

As we noticed from the plot, the middle shelf is significantly different (better in terms of sales if we look at the plot) from the other two.

Specification of contrast and estimate statements

  • When using the contrast statement, you can double check your results with the estimate statement.
  • The order of factors is determined by the order in the class statement, not the order in the model statement.
  • Contrasts to be examined should come a priori from research questions, not from questions that arise after looking at the plots and means.

Contrast/Estimate Example

For the bread example, suppose we want to compare the average of the two height = middle cells with the average of the other four cells; i.e. look at “eye-level” vs. “not eye-level” (for the average person). With this approach, the contrast should correspond to a research question formulated before examining the data. First, formulate the question as a contrast in terms of the cell means model:

H 0 :

(μ 2 , 1 + μ 2 , 2 ) 2

(μ 1 , 1 + μ 1 , 2 + μ 3 , 1 + μ 3 , 2 ) 4

or L = 0, where

L = − 0. 25 μ 1 , 1 − 0. 25 μ 1 , 2 + 0. 5 μ 2 , 1 + 0. 5 μ 2 , 2 − 0. 25 μ 3 , 1 − 0. 25 μ 3 , 2

Then translate the contrast into the factor effects model using μi,j = μ + αi + βj + (αβ)i,j

− 0. 25 μ 1 , 1 = − 0 .25(μ + α 1 + β 1 + αβ 1 , 1 ) − 0. 25 μ 1 , 2 = − 0 .25(μ + α 1 + β 2 + αβ 1 , 2 )

  1. 50 μ 2 , 1 = +0.50(μ + α 2 + β 1 + αβ 2 , 1 )
  2. 50 μ 2 , 2 = +0.50(μ + α 2 + β 2 + αβ 2 , 2 ) − 0. 25 μ 3 , 1 = − 0 .25(μ + α 3 + β 1 + αβ 3 , 1 ) − 0. 25 μ 3 , 2 = − 0 .25(μ + α 3 + β 2 + αβ 3 , 2 )

L = (− 0. 5 α 1 + α 2 − 0. 5 α 3 ) +(− 0. 25 αβ 1 , 1 − 0. 25 αβ 1 , 2 + 0. 5 αβ 2 , 1 + 0. 5 αβ 2 , 2 − 0. 25 αβ 3 , 1 − 0. 25 αβ 3 , 2 )

Note the β’s do not appear in this contrast because we are looking at height only and averaging over width (this would not necessarily be true in an unbalanced design).

proc glm with contrast and estimate

(nknw864.sas)

proc glm data=bread; class height width; model sales=height width heightwidth; contrast ’middle vs others’ height -.5 1 -. heightwidth -.25 -.25 .5 .5 -.25 -.25; estimate ’middle vs others’ height -.5 1 -. heightwidth -.25 -.25 .5 .5 -.25 -.25; means heightwidth;

Output

Contrast DF Contrast SS Mean Square F Value Pr > F middle vs others 1 1536.000000 1536.000000 148.65 <.

Standard Parameter Estimate Error t Value Pr > |t| middle vs others 24.0000000 1.96850197 12.19 <.

Check with means

1 1 45 1 2 43 2 1 65 2 2 69 3 1 40 3 2 44

L^ ˆ = (65 + 69)

Combining with Quantitative Factors

Sometimes a factor can be interpreted as either categorical or quantitative. For example, “low, medium, high” or actual height above floor. If there are replicates for a quantitative factor we could use either regression or ANOVA. Recall that GLM will treat a factor as