Th ree-way ANODEV (Contingency table)

Một phần của tài liệu Bookflare net modern analysis of biological data generalized linear models in r (Trang 201 - 210)

We will demonstrate here how the power of the GLM approach can be useful for the analysis of more complex Poisson data than that we have seen so far. We might, for instance, be inter- ested in a detailed analysis of how three factors simultaneously infl uence observed counts. Th is is a case of a multinomial model.

EXAMPLE

Some predators use conditional strategies to catch prey. Th e use of a strategy oft en de- pends on prey characteristics. Woodlice-eating spiders can use three capture strategies (STRATEGY: stratA, stratB, stratC) to catch woodlice. In the fi eld, it was observed which of three strategies spiders used to capture woodlice. Captured prey was then classifi ed ac-

cording to its size (SIZE: large, small) and speed of movement (PREY: fast, slow). Altogether there were 88 observations. Th e observed frequencies (freq) are shown in Table 10-1. We are interested in the following questions: (1) Is the use of strategy infl uenced by prey size and its movement? (2) If it is infl uenced, which prey type tend to be captured by each particular strategy?

EDA

Although there are three categorical variables in the data frame, we are interested only in one specifi c question: how the capture strategy (STRATEGY) was aff ected by traits of prey (PREY and SIZE). freq is the response variable (i.e. frequency of observations for particular combi- nations of levels of all three explanatory variables). Of all the interactions, we are interested only in the interaction among PREY, SIZE, and STRATEGY. Th us, for a quick orientation to the data, we will use interaction plots, where the fi rst factor is going to be STRATEGY and the second one is going to be one of the two other explanatory variables.

> dat<-read.delim(" predator.txt"); attach(dat); dat prey size strategy freq

1 slow small stratA 19 2 slow small stratB 4 3 slow small stratC 0 ...

12 fast large stratC 2

> interaction.plot(strategy,prey,freq)

> interaction.plot(strategy,size,freq)

From the fi rst plot (Fig. 10-10A), it is apparent that to capture both “slow” and “fast” prey, the spiders most frequently used “stratA”, less frequently “stratB” and they and almost never used “stratC”. As the lines cross only very slightly, interaction might not be signifi cant. Th e second plot (Fig. 10-10B) shows that when capturing “small” prey, “stratA” was used almost exclusively. Th is does not apply to “large” prey, which were captured most frequently with both “stratA” and “stratB”. Interaction STRATEGY:SIZE will thus be most likely signifi cant.

MODEL

We will test the null hypothesis, i.e. that the choice of strategy has not been infl uenced by the two study factors (or their interactions). Th at is why we will be only interested in the interac- tions between the STRATEGY and the explanatory variables, PREY and SIZE (i.e. two-way interactions). We are not interested in the main eff ects of PREY, SIZE and STRATEGY (for example, in the case of PREY, we are not interested to know if there was more slow or fast 10.6 THREE-WAY ANODEV (CONTINGENCY TABLE)

Table 10-1 Frequencies of three capture strategies used when hunting four types of prey.

PREY: slow fast

SIZE: small large small large stratA 19 10 21 12 stratB 4 10 0 8

stratC 0 1 1 2

prey). Similarly, we are not interested in testing interaction PREY:SIZE, i.e. in determining which combinations of the prey were more frequent. Even though we are not interested in the main eff ects or in the two-way interaction between PREY and SIZE, we have to include them in the model as without them, the estimate and tests of the eff ects would not be correct.

Th e following is the expected model for the ith strategy, jth size and kth prey type:

ij ik

k j

i ijk

SIZE STRATEGY PREY

STRATEGY

PREY SIZE

STRATEGY

: :

) log(

+ +

+ +

+

= α μ

, where freqijk~ Poi(μijk), independent among measurements.

ANALYSIS

You have likely realised that this is an example of a log-linear model (this method was de- veloped, and frequently and long used, before the GLM). To analyse the data we will not use the function loglin, but glm with a Poisson error structure. Both factors and the stimulus variable STRATEGY will appear in the linear predictor (i.e. on the right side of the model formula). Let’s see what happens if we fi t a model, which is much larger than the one we expected. It will include all main eff ects, three two-way and one three-way interactions:

ijk jk

ij

ik k

j i ijk

PREY SIZE STRATEGY PREY

SIZE SIZE

STRATEGY

PREY STRATEGY PREY

SIZE STRATEGY

: : :

: ) :

log(

+ +

+

+ +

+ +

= α μ

, where freqijk~ Poi(μijk), independent among measurements.

> m1<-glm(freq~strategy*size*prey,family=poisson)

> summary(m1)

(10-7) (10-6) Fig. 10-10 A. Comparison of frequencies (freq) of capture strategies (STRATEGY) when hunting “fast”

and “slow” prey. B. Comparison of frequencies (freq) of capture strategies (STRATEGY) when hunting

“large” and “small” prey.

051015

A

strategy

mean of freq

stratA stratB stratC

prey fastslow

05101520

B

strategy

mean of freq

stratA stratB stratC

size large small

Call:

glm(formula = freq ~ strategy * size * prey, family = poisson) Deviance Residuals:

[1] 0 0 0 0 0 0 0 0 0 0 0 0 Coeffi cients:

Estimate Std. Error z value Pr(>|z|) (Intercept) 2.485e+00 2.887e-01 8.608 <2e-16 strategystratB -4.055e-01 4.564e-01 -0.888 0.3744 strategystratC -1.792e+00 7.638e-01 -2.346 0.0190 sizesmall 5.596e-01 3.619e-01 1.546 0.1220 preyslow -1.823e-01 4.282e-01 -0.426 0.6702 strategystratB:sizesmall -2.594e+01 6.965e+04 -0.000372 0.9997 strategystratC:sizesmall -1.253e+00 1.277e+00 -0.981 0.3266 strategystratB:preyslow 4.055e-01 6.390e-01 0.635 0.5257 strategystratC:preyslow -5.108e-01 1.297e+00 -0.394 0.6938 sizesmall:preyslow 8.224e-02 5.325e-01 0.154 0.8773 strategystratB:sizesmall:preyslow 2.438e+01 6.965e+04 0.000350 0.9997 strategystratC:sizesmall:preyslow -2.269e+01 6.965e+04 -0.000326 0.9997 ---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 8.7966e+01 on 11 degrees of freedom Residual deviance: 3.0330e-10 on 0 degrees of freedom AIC: 60.15

Number of Fisher Scoring iterations: 21

Th is is a saturated model. It has no residual degrees of freedom. Th is is because the model estimates as many parameters (12) as there are degrees of freedom available from the data (as many as there are cells in the contingency table). Residual deviance is therefore null (though, due to small numerical errors, in the summary output we can read a very small number, 3.033×10-10 instead of zero). Consequently, the deviance residuals are uniformly equal to zero. Th e saturated model produced exact estimates of observed frequencies, but we cannot evaluate the quality of the model (at least not by the standard graphical diagnostic tools). Yet, such model might be useful as a starting point for fi tting smal- ler and simpler models (given these data it is impossible to fi t a diff erent non- saturated Poisson model).

Let’s examine the eff ect of the individual model terms using ANODEV table.

> anova(m1,test="Chi") ...

Df Deviance Resid. Df Resid. Dev P(>|Chi|) NULL 11 87.966 strategy 2 64.205 9 23.761 1.143e-14 size 1 0.045 8 23.715 0.831 prey 1 0.000 7 23.715 1.000 strategy:size 2 15.939 5 7.776 3.458e-04 strategy:prey 2 2.962 3 4.814 0.227

10.6 THREE-WAY ANODEV (CONTINGENCY TABLE)

size:prey 1 0.507 2 4.307 0.476 strategy:size:prey 2 4.307 0 3.033e-10 0.116

Of all the terms, only two are signifi cant, the main eff ect of the STRATEGY and interaction STRATEGY:SIZE. As we have already stated before, we are interested only in the three- and two-way interactions, i.e. those that include STRATEGY. Th e other terms, i.e. all three main eff ects and the PREY:SIZE interactions, have to be kept in the model even though we will not attempt to interpret them, because they are not in the direction of our interest. Firstly, we will remove the non-signifi cant three-way interaction.

> m2<-update(m1,~.-strategy:size:prey)

> anova(m2,test="Chi") ...

Df Deviance Resid. Df Resid. Dev P(>|Chi|) NULL 11 87.966 strategy 2 64.205 9 23.761 1.143e-14 size 1 0.045 8 23.715 0.831 prey 1 0.000 7 23.715 1.000 strategy:size 2 15.939 5 7.776 3.458e-04 strategy:prey 2 2.962 3 4.814 0.227 size:prey 1 0.507 2 4.307 0.476

In the adjusted model, the STRATEGY:PREY interaction is not signifi cant. Th is says that move- ment of the prey had no eff ect on the use of the strategy. Th at is actually the last component that we can remove from the model since the STRATEGY:SIZE interaction is signifi cant.

> m3<-update(m2,~.-strategy:prey)

> anova(m3,test="Chi") ...

Df Deviance Resid. Df Resid. Dev P(>|Chi|) NULL 11 87.966 strategy 2 64.205 9 23.761 1.143e-14 size 1 0.045 8 23.715 0.831 prey 1 0.000 7 23.715 1.000 strategy:size 2 15.939 5 7.776 3.458e-04 size:prey 1 0.045 4 7.731 0.831

Eff ect of the STRATEGY:SIZE interaction is still signifi cant – the use of strategies diff ered based on the size of prey. Th ere is nothing more in model m3 that we could further simplify.

We have thus arrived at a model that we will consider to be fi nal and that can be formally written as follows:

ij jk

k j

i ijk

SIZE STRATEGY PREY

SIZE

PREY SIZE

STRATEGY

: :

) log(

+ +

+ +

+

= α μ

, where freqijk~ Poi(μijk), independent among measurements.

Let’s have a closer look at the table of coeffi cients of this model.

> summary(m3)

(10-8)

Call:

glm(formula = freq ~ strategy + size + prey + strategy:size + size:prey, family = poisson)

Deviance Residuals:

1 2 3 4 5 6 7 -0.3233 1.2076 -1.0111 -0.2297 0.3990 -0.4079 0.3227 8 9 10 11 12

-1.9777 0.6395 0.2194 -0.4077 0.3585 Coeffi cients:

Estimate Std. Error z value Pr(>|z|) (Intercept) 2.42088 0.26010 9.307 < 2e-16 ***

strategystratB -0.20067 0.31782 -0.631 0.527782 strategystratC -1.99243 0.61546 -3.237 0.001207 **

sizesmall 0.55237 0.34042 1.623 0.104669 preyslow -0.04652 0.30508 -0.152 0.878805 strategystratB:sizesmall -2.10191 0.61318 -3.428 0.000608 ***

strategystratC:sizesmall -1.69645 1.18481 -1.432 0.152193 sizesmall:preyslow 0.09097 0.42662 0.213 0.831142 ---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for poisson family taken to be 1) Null deviance: 87.9661 on 11 degrees of freedom Residual deviance: 7.7306 on 4 degrees of freedom AIC: 59.88

Number of Fisher Scoring iterations: 5

Of course, the model is not saturated any more − the table includes only eight coeffi cients.

Th at is also why the residuals are not zero any more. Interpretation of the coeffi cients may appear cumbersome at fi rst, but it is actually not diffi cult. We just need to keep in mind that we have used treatment contrasts and that the coeffi cients are on a logarithmic scale.

Frequencies can be obtained from the table relatively simply. For example, we get the mean value for “stratB-large” by adding the fi rst two coeffi cients and exponentiating the result:

exp(2.421–0.201) = 9.207. Only two partial tests, apart from the test for Intercept, are sig- nifi cant in the table of coeffi cients of model m3. Th e fi rst one shows a signifi cant diff erence between strategy “stratC” and strategy “stratA” for large prey, and the second one shows a signifi cant diff erence between strategy “stratB” and strategy “stratA” for a small prey. For a simple interpretation of the fi nal model, we will enter the predicted values for the combi- nation of the STRATEGY and SIZE variables in the attacks object.

> attacks<-tapply(predict(m3,type="response"),list(size,strategy),mean)

> attacks

stratA stratB stratC large 11 9 1.5 small 20 2 0.5

Now we can nicely see how diff erent strategies are used for diff erent prey sizes. Th is table corresponds to the plot on Fig. 10-10B.

10.6 THREE-WAY ANODEV (CONTINGENCY TABLE)

We expect that the data come from the Poisson distribution and that they are independent.

We could check the fi rst assumption informally using the pre-set diagnostic plots. We will consider the assumption that the various data are independent to be correct based on the way in which the study was conducted (properly randomised).

We will calculate the 95% confi dence intervals for the mean count values. Th at is why we will refi t the model in the textbook parametrization. Prior to that, we combine both factors into a single factor with six levels. Th e new factor levels will correspond to the six combina- tions of levels STRATEGY and SIZE. Next, we modify the model formula by removing the intercept. In this way, we obtain estimates of the mean values together with their standard errors. Both of them are needed for confi dence interval construction. We will calculate the intervals on the logarithmic scale using the confi nt function and transform the result to the original scale of counts through exponentiation.

> both<-paste(strategy,size)

> m4<-glm(freq~factor(both)-1,poisson)

> exp(confi nt(m4))

Waiting for profi ling to be done...

2.5 % 97.5 % factor(both)stratA large 7.01961803 16.257527 factor(both)stratA small 14.42415842 26.853438 factor(both)stratB large 5.45596390 13.820389 factor(both)stratB small 0.62054466 4.642473 factor(both)stratC large 0.37275278 3.885165 factor(both)stratC small 0.02850154 2.201752

Now we can draw the estimated mean values, which we have saved in the attacks object, together with the 95% confi dence intervals in the plot (Fig. 10-11) using a few functions with which you should be quite familiar already:

Fig. 10-11 Comparison of frequencies of three capture strategies (“stratA”, “stratB”, “stratC”) when hunting “large” and “small” prey. Vertical lines are 95% confi dence intervals.

stratA stratB stratC

large small

Strategy No. of attacks 0510152025

> barplot(attacks, beside=T,ylab="No. of attacks",xlab="Strategy", + legend. text=c("large","small"),ylim=c(0,25))

> lines(c(1.5,1.5),c(7,16.3))

> lines(c(2.5,2.5),c(14.4,26.9))

> lines(c(4.5,4.5),c(5.5,13.8))

> lines(c(5.5,5.5),c(0.6,4.6))

> lines(c(7.5,7.5),c(0.4,3.9))

> lines(c(8.5,8.5),c(0.03,2.2))

CONCLUSION

Th e data do not support an eff ect of the prey movement on the type of the hunting strategy used (GLM-p, χ21 < 0.1, P = 0.83). On the other hand, they support a signifi cant eff ect of the size of prey (GLM-p, χ22 = 15.9, P = 0.0004). While the spiders hunted small prey, they used almost exclusively the “stratA” strategy. When hunting large prey, they used both “stratA”

and “stratB” strategies.

10.6 THREE-WAY ANODEV (CONTINGENCY TABLE)

1 1

A negative-binomial model is a parametric alternative to the Poisson model with overdisper- sion. It can be obtained as a compound distribution: by randomly varying the mean para- meter (according to a gamma distribution) in the Poisson model. A negative-binomial model has one extra parameter (related to the description of overdispersion) in comparison with the Poisson model, which increases its fl exibility. Even though we can also use other models for modelling overdispersion, the advantage of a negative-binomial model is its simplicity. It is implemented in R (and in other environments) and it is frequently used in practice.

Một phần của tài liệu Bookflare net modern analysis of biological data generalized linear models in r (Trang 201 - 210)

Tải bản đầy đủ (PDF)

(258 trang)