class: center, middle, inverse, title-slide # PRIMJENJENA STATISTIKA ## Predavanje 10: ANOVA ### Luka Sikic, PhD ### Fakultet hrvatskih studija |
Github PS
--- class: inverse, middle # PREGLED PREDAVANJA --- layout: true # PREGLED PREDAVANJA --- <br> ## CILJEVI - Pregled podataka - ANOVA - ANOVA u R - Efekt veličine - Post hoc testovi - Pretpostavke jednostrane ANOVA-e - Homogenost varijance - Normalnost distribucije --- <style type="text/css"> @media print { .has-continuation { display: block !important; } } remark-slide-content { font-size: 22px; padding: 20px 80px 20px 80px; } .remark-code, .remark-inline-code { background: #f0f0f0; } .remark-code { font-size: 16px; } .huge .remark-code { /*Change made here*/ font-size: 200% !important; } .mid .remark-code { /*Change made here*/ font-size: 70% !important; } .tiny .remark-code { /*Change made here*/ font-size: 50% !important; } </style> .hi[**Pregled podataka**] ```r load(file.path("../Podatci/clinicaltrial.Rdata")) # Učitaj podatke str(clin.trial) # Pregledaj podatke ``` ``` #> 'data.frame': 18 obs. of 3 variables: #> $ drug : Factor w/ 3 levels "placebo","anxifree",..: 1 1 1 2 2 2 3 3 3 1 ... #> $ therapy : Factor w/ 2 levels "no.therapy","CBT": 1 1 1 1 1 1 1 1 1 2 ... #> $ mood.gain: num 0.5 0.3 0.1 0.6 0.4 0.2 1.4 1.7 1.3 0.6 ... ``` ```r head(clin.trial, 8) # Pregledaj podatke ``` ``` #> drug therapy mood.gain #> 1 placebo no.therapy 0.5 #> 2 placebo no.therapy 0.3 #> 3 placebo no.therapy 0.1 #> 4 anxifree no.therapy 0.6 #> 5 anxifree no.therapy 0.4 #> 6 anxifree no.therapy 0.2 #> 7 joyzepam no.therapy 1.4 #> 8 joyzepam no.therapy 1.7 ``` --- .hi[**Deskriptivna statistika**] ```r summary(clin.trial) ``` ``` #> drug therapy mood.gain #> placebo :6 no.therapy:9 Min. :0.1000 #> anxifree:6 CBT :9 1st Qu.:0.4250 #> joyzepam:6 Median :0.8500 #> Mean :0.8833 #> 3rd Qu.:1.3000 #> Max. :1.8000 ``` .pull-left[ ```r # Tabuliraj podatke xtabs( ~ drug, clin.trial ) ``` ``` #> drug #> placebo anxifree joyzepam #> 6 6 6 ``` ] .pull-right[ ```r # Tabuliraj podatke xtabs( ~ therapy, clin.trial ) ``` ``` #> therapy #> no.therapy CBT #> 9 9 ``` ] --- .hi[**Deskriptivna statistika**] ```r # Agregiraj podatke po primjenjenom ljeku aggregate( mood.gain ~ drug, clin.trial, mean ) # Prosjek ``` ``` #> drug mood.gain #> 1 placebo 0.4500000 #> 2 anxifree 0.7166667 #> 3 joyzepam 1.4833333 ``` ```r aggregate( mood.gain ~ drug, clin.trial, sd ) # Standardna devijacija ``` ``` #> drug mood.gain #> 1 placebo 0.2810694 #> 2 anxifree 0.3920034 #> 3 joyzepam 0.2136976 ``` --- .hi[**Vizualizacija**] ```r library(gplots) # Učitaj paket plotmeans( formula = mood.gain ~ drug, # Porast raspoloženja vs. ljek data = clin.trial, # Podatci xlab = "Primjenjeni ljek", ylab = "Rast raspoloženja", n.label = FALSE # Ne prikazuj veličinu uzorka ) ``` <img src="10_ANOVA_files/figure-html/unnamed-chunk-7-1.svg" style="display: block; margin: auto;" /> --- layout:false class: middle, inverse # ANOVA <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Usporedba više prosjeka!) --- layout:true # ANOVA --- .hi[**Hipoteze kod ANOVA-e**] `$$\begin{array}{rcl} H_0 &:& \mbox{točno je } \mu_P = \mu_A = \mu_J \end{array}$$` `$$\begin{array}{rcl} H_1 &:& \mbox{nije točno } \mu_P = \mu_A = \mu_J \end{array}$$` <br> <br> .hi[**Izračun varijance kod ANOVA-e**] `$$\mbox{Var}(Y) = \frac{1}{N} \sum_{k=1}^G \sum_{i=1}^{N_k} \left(Y_{ik} - \bar{Y} \right)^2$$` --- .hi[**Praktični primjer**] <table> <thead> <tr> <th style="text-align:left;"> ime </th> <th style="text-align:left;"> osoba (p) </th> <th style="text-align:left;"> grupa </th> <th style="text-align:left;"> broj grupe (k) </th> <th style="text-align:left;"> index grupe (i) </th> <th style="text-align:left;"> raspolozenje (Y_ik ili Y_p) </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Ann </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> cool </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 20 </td> </tr> <tr> <td style="text-align:left;"> Ben </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> cool </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 55 </td> </tr> <tr> <td style="text-align:left;"> Cat </td> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> cool </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> 21 </td> </tr> <tr> <td style="text-align:left;"> Dan </td> <td style="text-align:left;"> 4 </td> <td style="text-align:left;"> uncool </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 91 </td> </tr> <tr> <td style="text-align:left;"> Egg </td> <td style="text-align:left;"> 5 </td> <td style="text-align:left;"> uncool </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 22 </td> </tr> </tbody> </table> .hi[**Usporedi sa formulom za varijancu**] `$$\mbox{Var}(Y) = \frac{1}{N} \sum_{p=1}^N \left(Y_{p} - \bar{Y} \right)^2$$` --- .hi[**Od varijance do sume kvadrata odstupanja**] `$$\mbox{SS}_{tot} = \sum_{k=1}^G \sum_{i=1}^{N_k} \left(Y_{ik} - \bar{Y} \right)^2$$` .hi[**Suma kvadrata odstupanja unutar grupe**] `$$\mbox{SS}_w = \sum_{k=1}^G \sum_{i=1}^{N_k} \left( Y_{ik} - \bar{Y}_k \right)^2$$` .hi[**Suma kvadrata odstupanja među grupama**] `$$\begin{array}{rcl} \mbox{SS}_{b} &=& \sum_{k=1}^G \sum_{i=1}^{N_k} \left( \bar{Y}_k - \bar{Y} \right)^2 \\ &=& \sum_{k=1}^G N_k \left( \bar{Y}_k - \bar{Y} \right)^2 \end{array}$$` .hi[**Ukupna varijacija**] `$$\mbox{SS}_w + \mbox{SS}_{b} = \mbox{SS}_{tot}$$` --- .hi[**Varijacija među grupama**] <div class="figure" style="text-align: center"> <img src="10_ANOVA_files/figure-html/anovavara-1.svg" alt="Prikaz varijacije između grupa." /> <p class="caption">Prikaz varijacije između grupa.</p> </div> --- .hi[**Varijacija unutar grupa**] <div class="figure" style="text-align: center"> <img src="10_ANOVA_files/figure-html/anovavarb-1.svg" alt="Prikaz varijacije unutar grupa." /> <p class="caption">Prikaz varijacije unutar grupa.</p> </div> --- .hi[**Od sume kvadrata odstupanja do F-testa**] - Stupnjevi slobode `$$\begin{array}{lcl} \mbox{df}_b &=& G-1 \\ \mbox{df}_w &=& N-G \\ \end{array}$$` - Kvadrati odstupanja od prosjeka `$$\begin{array}{lcl} \mbox{MS}_b &=& \displaystyle\frac{\mbox{SS}_b }{ \mbox{df}_b} \\ \mbox{MS}_w &=& \displaystyle\frac{\mbox{SS}_w }{ \mbox{df}_w} \end{array}$$` - F-statistika `$$F = \frac{\mbox{MS}_b }{ \mbox{MS}_w }$$` --- .hi[**Sažeti prikaz u tablici**] <img src="../Foto/anova.png" width="1552" style="display: block; margin: auto;" /> --- .hi[**Izračun ANOVA-e korak po korak**] - Pregled podataka u tablici <table> <thead> <tr> <th style="text-align:left;"> Grupa (k) </th> <th style="text-align:left;"> Ishod (Y_ik) </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.5 </td> </tr> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.3 </td> </tr> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.1 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.6 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.4 </td> </tr> </tbody> </table> --- - Dodaj grupni prosjek <br> <br> <table> <thead> <tr> <th style="text-align:left;"> Grupa (k) </th> <th style="text-align:left;"> Ishod (Y_ik) </th> <th style="text-align:left;"> Grupni prosjek (Y_k) </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.5 </td> <td style="text-align:left;"> 0.45 </td> </tr> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.3 </td> <td style="text-align:left;"> 0.45 </td> </tr> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.1 </td> <td style="text-align:left;"> 0.45 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.6 </td> <td style="text-align:left;"> 0.72 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.4 </td> <td style="text-align:left;"> 0.72 </td> </tr> </tbody> </table> --- - Odstupanja od grupnog prosjeka i kvadrati odstupanja <table> <thead> <tr> <th style="text-align:left;"> Grupa (k) </th> <th style="text-align:left;"> Ishod (Y_ik) </th> <th style="text-align:left;"> Grupni prosjek (Y_k) </th> <th style="text-align:left;"> dodstupanje od gr. prosjeka (Y_ik - Y_k) </th> <th style="text-align:left;"> Kvadrat odstupanja (Y_ik - Y_k)^2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.5 </td> <td style="text-align:left;"> 0.45 </td> <td style="text-align:left;"> 0.05 </td> <td style="text-align:left;"> 0.0025 </td> </tr> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.3 </td> <td style="text-align:left;"> 0.45 </td> <td style="text-align:left;"> -0.15 </td> <td style="text-align:left;"> 0.0225 </td> </tr> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.1 </td> <td style="text-align:left;"> 0.45 </td> <td style="text-align:left;"> -0.35 </td> <td style="text-align:left;"> 0.1225 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.6 </td> <td style="text-align:left;"> 0.72 </td> <td style="text-align:left;"> -0.12 </td> <td style="text-align:left;"> 0.0136 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.4 </td> <td style="text-align:left;"> 0.72 </td> <td style="text-align:left;"> -0.32 </td> <td style="text-align:left;"> 0.1003 </td> </tr> </tbody> </table> - Izračunaj sumu kvadrata odstupanja unutar grupa `$$\begin{array}{rcl} \mbox{SS}_w &=& 0.0025 + 0.0225 + 0.1225 + 0.0136 + 0.1003 \\ &=& 0.2614 \end{array}$$` --- .hi[**Izračun u R-u korak po korak**] <br> <br> ```r # Definiraj varijable outcome <- clin.trial$mood.gain group <- clin.trial$drug gp.means <- tapply(outcome,group,mean) gp.means <- gp.means[group] dev.from.gp.means <- outcome - gp.means squared.devs <- dev.from.gp.means ^2 # Poveži u Data Frame Y <- data.frame( group, outcome, gp.means, dev.from.gp.means, squared.devs ) ``` --- ```r # Pregled podataka print(head(Y,6), digits = 2) ``` ``` #> group outcome gp.means dev.from.gp.means squared.devs #> 1 placebo 0.5 0.45 0.05 0.0025 #> 2 placebo 0.3 0.45 -0.15 0.0225 #> 3 placebo 0.1 0.45 -0.35 0.1225 #> 4 anxifree 0.6 0.72 -0.12 0.0136 #> 5 anxifree 0.4 0.72 -0.32 0.1003 #> 6 anxifree 0.2 0.72 -0.52 0.2669 ``` ```r # Izračunaj sumu kvadrata odstupanja SSw <- sum( squared.devs ) print( SSw ) ``` ``` #> [1] 1.391667 ``` --- <br> .hi[**Izračunaj grupna odstupanja od "master" prosjeka**] <br> <br> <table> <thead> <tr> <th style="text-align:left;"> Grupa (k) </th> <th style="text-align:left;"> Group mean (Y_k) </th> <th style="text-align:left;"> Grand mean (Y) </th> <th style="text-align:left;"> devijacija (Y_k - Y) </th> <th style="text-align:left;"> Kvadrirane devijacije (Y_k - Y)^2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.45 </td> <td style="text-align:left;"> 0.88 </td> <td style="text-align:left;"> -0.43 </td> <td style="text-align:left;"> 0.18 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.72 </td> <td style="text-align:left;"> 0.88 </td> <td style="text-align:left;"> -0.16 </td> <td style="text-align:left;"> 0.03 </td> </tr> <tr> <td style="text-align:left;"> joyzepam </td> <td style="text-align:left;"> 1.48 </td> <td style="text-align:left;"> 0.88 </td> <td style="text-align:left;"> 0.60 </td> <td style="text-align:left;"> 0.36 </td> </tr> </tbody> </table> --- - Pomnoži sa veličinom uzorka <table> <thead> <tr> <th style="text-align:left;"> Grupa (k) </th> <th style="text-align:left;"> Kvadrirane devijacije (Y_k - Y)^2 </th> <th style="text-align:left;"> Veličina uzorka (N_k) </th> <th style="text-align:left;"> Ponderirani kv. odst. N_k*(Y_k - Y)^2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> placebo </td> <td style="text-align:left;"> 0.18 </td> <td style="text-align:left;"> 6 </td> <td style="text-align:left;"> 1.11 </td> </tr> <tr> <td style="text-align:left;"> anxifree </td> <td style="text-align:left;"> 0.03 </td> <td style="text-align:left;"> 6 </td> <td style="text-align:left;"> 0.16 </td> </tr> <tr> <td style="text-align:left;"> joyzepam </td> <td style="text-align:left;"> 0.36 </td> <td style="text-align:left;"> 6 </td> <td style="text-align:left;"> 2.18 </td> </tr> </tbody> </table> - Zbroji kvadrate odstupanja između grupa `$$\begin{array}{rcl} \mbox{SS}_{b} &=& 1.11 + 0.16 + 2.18 \\ &=& 3.45 \end{array}$$` --- <br> .hi[**Provedi proceduru u R**] <br> ```r # Definiraj varijable gp.means <- tapply(outcome,group,mean) grand.mean <- mean(outcome) dev.from.grand.mean <- gp.means - grand.mean squared.devs <- dev.from.grand.mean ^2 gp.sizes <- tapply(outcome,group,length) wt.squared.devs <- gp.sizes * squared.devs # Poveži u DF Y <- data.frame( gp.means, grand.mean, dev.from.grand.mean, squared.devs, gp.sizes, wt.squared.devs ) ``` --- ```r # Prikaži podatke print(head(Y,6), digits = 2) ``` ``` #> gp.means grand.mean dev.from.grand.mean squared.devs gp.sizes #> placebo 0.45 0.88 -0.43 0.188 6 #> anxifree 0.72 0.88 -0.17 0.028 6 #> joyzepam 1.48 0.88 0.60 0.360 6 #> wt.squared.devs #> placebo 1.13 #> anxifree 0.17 #> joyzepam 2.16 ``` ```r # Izračunaj sumu kvadrata odstupanja SSb <- sum( wt.squared.devs ) print( SSb ) ``` ``` #> [1] 3.453333 ``` --- .hi[**Izračunaj stupnjeve slobode**] <br> `$$\begin{array}{lclcl} \mbox{df}_b &=& G - 1 &=& 2 \\ \mbox{df}_w &=& N - G &=& 15 \end{array}$$` .hi[**Izračunaj MS statistike**] <br> <br> `$$\begin{array}{lclclcl} \mbox{MS}_b &=& \displaystyle\frac{\mbox{SS}_b }{ \mbox{df}_b } &=& \displaystyle\frac{3.45}{ 2} &=& 1.73 \end{array}$$` `$$\begin{array}{lclclcl} \mbox{MS}_w &=& \displaystyle\frac{\mbox{SS}_w }{ \mbox{df}_w } &=& \displaystyle\frac{1.39}{15} &=& 0.09 \end{array}$$` --- <br> .hi[**Izračunaj F-statistiku**] <br> `$$F \ = \ \frac{\mbox{MS}_b }{ \mbox{MS}_w } \ = \ \frac{1.73}{0.09} \ = \ 18.6$$` <br> .hi[**Provjeri p-vrijednost**] <br> ```r pf( 18.6, df1 = 2, df2 = 15, lower.tail = FALSE) ``` ``` #> [1] 8.672727e-05 ``` --- <br> .hi[**Prikaži rezultate u tablici**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> df </th> <th style="text-align:left;"> suma kvadrata </th> <th style="text-align:left;"> srednji kvadrati </th> <th style="text-align:left;"> F-statistika </th> <th style="text-align:left;"> p-vrijednost </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> between groups </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 3.45 </td> <td style="text-align:left;"> 1.73 </td> <td style="text-align:left;"> 18.6 </td> <td style="text-align:left;"> 8.67 * 10^-5 </td> </tr> <tr> <td style="text-align:left;"> within groups </td> <td style="text-align:left;"> 15 </td> <td style="text-align:left;"> 1.39 </td> <td style="text-align:left;"> 0.09 </td> <td style="text-align:left;"> - </td> <td style="text-align:left;"> - </td> </tr> </tbody> </table> --- layout:false class: middle, inverse # ANOVA U R <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Četiri načina!) --- layout:true # ANOVA U R --- <br> .hi[**Opcije**] <br> <br> ```r # Različiti načini za provođenje ANOVA postupka aov( formula = mood.gain ~ drug, data = clin.trial ) # Puna specifikacija aov( clin.trial$mood.gain ~ clin.trial$drug ) # Definiranje varijabli aov( mood.gain ~ drug, clin.trial ) # Skraćeno;definirani df ``` ```r # Spremi rezultate testa u objekt my.anova <- aov( mood.gain ~ drug, clin.trial ) ``` --- ```r # Provjeri klasu objekta class( my.anova ) ``` ``` #> [1] "aov" "lm" ``` ```r # Provjeri što sadrži objekt names( my.anova ) ``` ``` #> [1] "coefficients" "residuals" "effects" "rank" #> [5] "fitted.values" "assign" "qr" "df.residual" #> [9] "contrasts" "xlevels" "call" "terms" #> [13] "model" ``` --- ```r # Prikaži procjenjeni objekt print( my.anova ) ``` ``` #> Call: #> aov(formula = mood.gain ~ drug, data = clin.trial) #> #> Terms: #> drug Residuals #> Sum of Squares 3.453333 1.391667 #> Deg. of Freedom 2 15 #> #> Residual standard error: 0.3045944 #> Estimated effects may be unbalanced ``` --- <br> .hi[**Testiranje ANOVA hipoteza**] <br> <br> ```r # Prikaži deskriptivnu statistiku summary( my.anova ) ``` ``` #> Df Sum Sq Mean Sq F value Pr(>F) #> drug 2 3.453 1.7267 18.61 8.65e-05 *** #> Residuals 15 1.392 0.0928 #> --- #> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- layout:false class: middle, inverse # EFEKT VELIČINE <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Snaga odnosa) --- layout:true # EFEKT VELIČINE --- .hi[**Formula**] `$$\eta^2 = \frac{\mbox{SS}_b}{\mbox{SS}_{tot}}$$` .hi[**Izračun**] `$$\eta^2 = \frac{3.45}{4.84} = 0.71$$` .hi[**Makni kvadrat**] `$$\eta= \sqrt{\frac{\mbox{SS}_b}{\mbox{SS}_{tot}}}$$` --- .hi[**Napravi procjenitelj u R**] <br> <br> ```r SStot <- SSb + SSw # Ukupna suma kvadrata eta.squared <- SSb / SStot # Eta kvadrat print( eta.squared ) # Prikaži rezultate ``` ``` #> [1] 0.7127623 ``` ```r library(lsr) # Izračunaj pomoću funkcije lsr::etaSquared( x = my.anova ) ``` ``` #> eta.sq eta.sq.part #> drug 0.7127623 0.7127623 ``` --- layout:false class: middle, inverse # PRETPOSTAVKE JEDNOSTRANE ANOVA-e <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Robusnost...) --- layout:true # PRETPOSTAVKE JEDNOSTRANE ANOVA-e --- <br> <br> .pull-left[ .hi[**Statistički model ANOVA-e**] <br> `$$\begin{array}{lrcl} H_0: & Y_{ik} &=& \mu + \epsilon_{ik} \\ H_1: & Y_{ik} &=& \mu_k + \epsilon_{ik} \end{array}$$` ] .pull-right[ .hi[**Pretpostavka o rezidualnoj strukturi**] <br> `$$\epsilon_{ik} \sim \mbox{Normal}(0, \sigma^2)$$` ] <br> .hi[**Pretpostavke**] <br> - Normalnost distribucije - Homogenost varijance - Nezavisnost --- layout:false class: middle, inverse # HOMOGENOST VARIJANCE <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Kruške ili jabuke!?) --- layout:true # HOMOGENOST VARIJANCE --- <br> <br> .hi[**Provjera homogenosti Leven-ovim testom**] <br> `$$Z_{ik} = \left| Y_{ik} - \bar{Y}_k \right|$$` <br> <br> .hi[**Provjera homogenosti Brown-Forsythe testom**] <br> `$$Z_{ik} = \left| Y_{ik} - \mbox{median}_k(Y) \right|$$` --- .hi[**Provedi test u R**] ```r library( car ) # Učitaj paket car::leveneTest( my.anova ) # Provedi Levene test ``` ``` #> Levene's Test for Homogeneity of Variance (center = median) #> Df F value Pr(>F) #> group 2 1.4672 0.2618 #> 15 ``` ```r car::leveneTest( y = my.anova, center = mean ) # Provedi B-F test ``` ``` #> Levene's Test for Homogeneity of Variance (center = mean) #> Df F value Pr(>F) #> group 2 1.4497 0.2657 #> 15 ``` --- ```r # Provedi (Levene) test korak po korak Y <- clin.trial $ mood.gain # Ishod G <- clin.trial $ drug # Grupa gp.mean <- tapply(Y, G, mean) # Izračunaj grupne prosjeke Ybar <- gp.mean[G] # Izvadi grupne prosjeke vezane uz svaku ops Z <- abs(Y - Ybar) # Transformirana varijabla summary( aov(Z ~ G) ) # Izvrši ANOVA-u ``` ``` #> Df Sum Sq Mean Sq F value Pr(>F) #> G 2 0.0616 0.03080 1.45 0.266 #> Residuals 15 0.3187 0.02125 ``` --- .hi[**Ukoliko su varijance heterogene:**] ```r # Welch-ev test za ANOVA-u oneway.test(mood.gain ~ drug, data = clin.trial) ``` ``` #> #> One-way analysis of means (not assuming equal variances) #> #> data: mood.gain and drug #> F = 26.322, num df = 2.0000, denom df = 9.4932, p-value = 0.000134 ``` ```r # Usporedi sa rezultatima testa za jednake varijance oneway.test(mood.gain ~ drug, data = clin.trial, var.equal = TRUE) ``` ``` #> #> One-way analysis of means #> #> data: mood.gain and drug #> F = 18.611, num df = 2, denom df = 15, p-value = 8.646e-05 ``` --- layout:false class: middle, inverse # NORMALNOST DISTRIBUCIJE <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Vizualizacija i formalni test!) --- layout:true # NORMALNOST DISTRIBUCIJE --- .hi[**Provjera normalnosti distribucije**] ```r my.anova.residuals <- residuals( object = my.anova ) # Izvadi reziduale ``` ```r hist( x = my.anova.residuals ) # Napravi histogram reziduala ``` <img src="10_ANOVA_files/figure-html/unnamed-chunk-36-1.svg" style="display: block; margin: auto;" /> --- ```r qqnorm( y = my.anova.residuals ) # Napravi QQ plot ``` <img src="10_ANOVA_files/figure-html/unnamed-chunk-37-1.svg" style="display: block; margin: auto;" /> ```r shapiro.test( x = my.anova.residuals ) # Provedi Shapiro-Wilk test ``` ``` #> #> Shapiro-Wilk normality test #> #> data: my.anova.residuals #> W = 0.96019, p-value = 0.6053 ``` --- .hi[**Kruskal-Wallis test**] - Izračunaj prosječni rank vezan uz svaku grupu <br> `$$\bar{R}_k = \frac{1}{N_K} \sum_{i} R_{ik}$$` - Izračunaj rank grand prosjeka <br> `$$\bar{R} = \frac{1}{N} \sum_{i} \sum_{k} R_{ik}$$` - Izračunaj sumu kvadrata ukupnih rank-odstupanja <br> `$$\mbox{RSS}_{tot} = \sum_k \sum_i ( R_{ik} - \bar{R} )^2$$` --- .hi[**Kruskal-Wallis test**] - Izračunaj sumu kvadrata rank-odstupanja među grupama `$$\begin{array}{rcl} \mbox{RSS}_{b} &=& \sum_k \sum_i ( \bar{R}_k - \bar{R} )^2 \\ &=& \sum_k N_k ( \bar{R}_k - \bar{R} )^2 \end{array}$$` - Izračunaj Kruskal-Wallis statistiku `$$K = (N - 1) \times \frac{\mbox{RSS}_b}{\mbox{RSS}_{tot}}$$` - Drugi način za izračun K-W statistike `$$K = \frac{12}{N(N-1)} \sum_k N_k {\bar{R}_k}^2 - 3(N+1)$$` --- .hi[**Kruskal-Wallis test**] - Prikaži rangirane podatke ```r f <- table( clin.trial$mood.gain ) # Tabuliraj i spremi u varijablu print(f) # Prikaži ``` ``` #> #> 0.1 0.2 0.3 0.4 0.5 0.6 0.8 0.9 1.1 1.2 1.3 1.4 1.7 1.8 #> 1 1 2 1 1 2 1 1 1 1 2 2 1 1 ``` - Izvrši K-W test u R ```r # Prvi način kruskal.test(mood.gain ~ drug, data = clin.trial) ``` ``` #> #> Kruskal-Wallis rank sum test #> #> data: mood.gain by drug #> Kruskal-Wallis chi-squared = 12.076, df = 2, p-value = 0.002386 ``` --- .hi[**Kruskal-Wallis test**] ```r # Drugi način kruskal.test(x = clin.trial$mood.gain, g = clin.trial$drug) ``` ``` #> #> Kruskal-Wallis rank sum test #> #> data: clin.trial$mood.gain and clin.trial$drug #> Kruskal-Wallis chi-squared = 12.076, df = 2, p-value = 0.002386 ``` <br> ```r # Treći način mood.gain <- list( placebo, joyzepam, anxifree ) # Spremi serije u listu kruskal.test( x = mood.gain ) # Provedi test ``` --- layout:false class: middle, inverse # ANOVA vs STUDENTOV t-test <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Postoji li razlika?!) --- layout:true # ANOVA vs t-test --- <br> ```r # Provedi ANOVA test summary( aov( mood.gain ~ therapy, data = clin.trial )) ``` ``` #> Df Sum Sq Mean Sq F value Pr(>F) #> therapy 1 0.467 0.4672 1.708 0.21 #> Residuals 16 4.378 0.2736 ``` --- ```r # Provedi studentov t-test t.test( mood.gain ~ therapy, data = clin.trial, var.equal = TRUE ) ``` ``` #> #> Two Sample t-test #> #> data: mood.gain by therapy #> t = -1.3068, df = 16, p-value = 0.2098 #> alternative hypothesis: true difference in means is not equal to 0 #> 95 percent confidence interval: #> -0.8449518 0.2005073 #> sample estimates: #> mean in group no.therapy mean in group CBT #> 0.7222222 1.0444444 ``` ```r # Kvadriraj za F-statistiku 1.3068 ^ 2 ``` ``` #> [1] 1.707726 ``` --- layout:false class: middle, inverse # Hvala na pažnji! <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Sljedeće predavanje: Linearna regresija)