class: center, middle, inverse, title-slide # PRIMJENJENA STATISTIKA ## Predavanje 8: Kategorijalna analiza ### Luka Sikic, PhD ### Fakultet hrvatskih studija |
Github PS
--- --- class: inverse, middle # PREGLED PREDAVANJA --- layout: true # PREGLED PREDAVANJA --- <br> ## CILJEVI - Goodness-of-fit test - Test nezavisnosti - Korekcija kontinuiranosti - Efekt veličine - Pretpostavke testa - Izvođenje chi-sq testova u R - Fisherov test za male uzorke - McNemarov test zavisnih uzoraka --- layout:false class: middle, inverse # GOF TEST <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Jedan od najstarijih statističkih testova!) --- layout:true # GOF TEST --- <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; } .tiny .remark-code { /*Change made here*/ font-size: 50% !important; } </style> .hi[**Pregled podataka za analizu**] ``` #> id choice_1 choice_2 #> 1 subj1 spades clubs #> 2 subj2 diamonds clubs #> 3 subj3 hearts clubs #> 4 subj4 spades clubs ``` ```r str(cards) # Pregledaj podatke ``` ``` #> 'data.frame': 200 obs. of 3 variables: #> $ id : Factor w/ 200 levels "subj1","subj10",..: 1 112 124 135 146 157 168 179 190 2 ... #> $ choice_1: Factor w/ 4 levels "clubs","diamonds",..: 4 2 3 4 3 1 3 2 4 2 ... #> $ choice_2: Factor w/ 4 levels "clubs","diamonds",..: 1 1 1 1 4 3 2 1 1 4 ... ``` .footnote[[*] Test je osmislio Person (1900), a doradio Fisher (1922).] --- .hi[**Tabulirani pregled podataka**] ```r podatci <- table(cards$choice_1) # Tabuliraj prvu varijablu podatci # Pregledaj tabuliranu varijablu ``` ``` #> #> clubs diamonds hearts spades #> 35 51 64 50 ``` .hi[Matematički zapis] $$ O = (O_1, O_2, O_3, O_4) $$ .hi[Matematički zapis rezultata] $$ O = (35, 51, 64, 50) $$ --- .hi[**Intuitivniji prikaz podataka**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> naziv </th> <th style="text-align:center;"> indeks i </th> <th style="text-align:center;"> simbol </th> <th style="text-align:center;"> R naredba </th> <th style="text-align:center;"> vrijednost </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> clubs `\(\clubsuit\)` </td> <td style="text-align:center;"> 1 </td> <td style="text-align:center;"> O_1 </td> <td style="text-align:center;"> observed[1] </td> <td style="text-align:center;"> 35 </td> </tr> <tr> <td style="text-align:left;"> diamonds `\(\diamondsuit\)` </td> <td style="text-align:center;"> 2 </td> <td style="text-align:center;"> O_2 </td> <td style="text-align:center;"> observed[2] </td> <td style="text-align:center;"> 51 </td> </tr> <tr> <td style="text-align:left;"> hearts `\(\heartsuit\)` </td> <td style="text-align:center;"> 3 </td> <td style="text-align:center;"> O_3 </td> <td style="text-align:center;"> observed[3] </td> <td style="text-align:center;"> 64 </td> </tr> <tr> <td style="text-align:left;"> spades `\(\spadesuit\)` </td> <td style="text-align:center;"> 4 </td> <td style="text-align:center;"> O_4 </td> <td style="text-align:center;"> observed[4] </td> <td style="text-align:center;"> 50 </td> </tr> </tbody> </table> --- .hi[Formuliranje nulte hipoteze] <table> <thead> <tr> <th style="text-align:left;"> Hipoteza </th> <th style="text-align:left;"> Formalni.Zapis </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> `\(H_0\)` : Sve četiri karte su izabrane sa jednakom vjerojatnošću </td> <td style="text-align:left;"> H_0: P = (.25, .25, .25, .25) </td> </tr> </tbody> </table> .hi[Formuliranje alternativne hipoteze] <table> <thead> <tr> <th style="text-align:left;"> Nulta.Hipoteza </th> <th style="text-align:left;"> Alternativna.Hipoteza </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> H_0 : Sve četiri karte su izabrane sa jednakom vjerojatnošću </td> <td style="text-align:left;"> H_1 : Barem jedan izbor nije sa vjerojatnošću 0.25 </td> </tr> </tbody> </table> .hi[Formalni zapis] <table> <thead> <tr> <th style="text-align:left;"> H_0 </th> <th style="text-align:left;"> H_1 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> P = (.25, .25, .25, .25) </td> <td style="text-align:left;"> P(.25,.25,.25,.25) </td> </tr> </tbody> </table> --- .hi[**Formiraj vjerojatnosti u R**] ```r # Pripiši vjerojatnosti varijabli vjerojatnosti <- c(clubs = .25, diamonds = .25, hearts = .25, spades = .25) vjerojatnosti # Pogledaj varijablu ``` ``` #> clubs diamonds hearts spades #> 0.25 0.25 0.25 0.25 ``` .hi[**Testna statistika**] `$$E_i = N \times P_i$$` .hi[**Formiraj očekivane vjerojatnosti u R**] ```r N <- 200 # Veličina uzorka ocekivano <- N * vjerojatnosti # Očekivane frekvencije ocekivano# Pogledaj podatke ``` ``` #> clubs diamonds hearts spades #> 50 50 50 50 ``` --- .hi[**Prikaži sve podatke**] <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> </th> <th style="text-align:right;"> club </th> <th style="text-align:right;"> dijamant </th> <th style="text-align:right;"> hertz </th> <th style="text-align:right;"> spade </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Očekivana frekvencija </td> <td style="text-align:left;"> `\(E_i\)` </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 50 </td> </tr> <tr> <td style="text-align:left;"> Podatci </td> <td style="text-align:left;"> `\(O_i\)` </td> <td style="text-align:right;"> 35 </td> <td style="text-align:right;"> 51 </td> <td style="text-align:right;"> 64 </td> <td style="text-align:right;"> 50 </td> </tr> <tr> <td style="text-align:left;"> Razlika </td> <td style="text-align:left;"> `\(O_i - E_i\)` </td> <td style="text-align:right;"> -15 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 14 </td> <td style="text-align:right;"> 0 </td> </tr> </tbody> </table> --- .hi[**Postupak izračuna u R (korak po korak)**] ```r # Izračunaj razliku između podataka i očekivanih frekvencija podatci - ocekivano ``` ``` #> #> clubs diamonds hearts spades #> -15 1 14 0 ``` ```r # Kvadriraj razlike kako bi se dobili veći brojevi (podatci - ocekivano)^2 ``` ``` #> #> clubs diamonds hearts spades #> 225 1 196 0 ``` ```r # Podijeli kvadrirane razlike (podatci - ocekivano)^2 / ocekivano ``` ``` #> #> clubs diamonds hearts spades #> 4.50 0.02 3.92 0.00 ``` ```r # Zbroji podatke za sve karte sum( (podatci - ocekivano)^2 / ocekivano ) ``` ``` #> [1] 8.44 ``` --- .hi[Matematički zapis] <br> <br> <br> `$$X^2 = \sum_{i=1}^k \frac{(O_i - E_i)^2}{E_i}$$` .hi[Sampling distribucija GOF statistike] <br> <br> <br> `$$O_i \sim \mbox{Binomial}(P_i, N)$$` --- .hi[Stupnjevi slobode] <img src="08_KATEGORIJALNA_ANALIZA_files/figure-html/manychi-1.svg" style="display: block; margin: auto;" /> .footnote[[*]Chi-square distribucija za različite stupnjeve slobode.] --- .hi[Grafički prikaz] <img src="08_KATEGORIJALNA_ANALIZA_files/figure-html/goftest-1.svg" style="display: block; margin: auto;" /> .footnote[[*]Prikaz kritičke regije za odbacivanje nulte hipoteze.] --- .hi[**Testiranje u R**] ```r # Izračunaj 95i centil chi-sq distribucije qchisq(p = .95, df = 3) ``` ``` #> [1] 7.814728 ``` ```r # Izračunaj p-vrijednost za podatke iz radnog primjera pchisq(q = 8.44, df = 3, lower.tail = FALSE) ``` ``` #> [1] 0.03774185 ``` ```r # Alternativna varijanta izračuna 1-pchisq(q = 8.44, df = 3) ``` ``` #> [1] 0.03774185 ``` --- .tiny[ ```r # Provedi test u R pomoću funkcije lsr::goodnessOfFitTest( cards$choice_1 ) ``` ``` #> #> Chi-square test against specified probabilities #> #> Data variable: cards$choice_1 #> #> Hypotheses: #> null: true probabilities are as specified #> alternative: true probabilities differ from those specified #> #> Descriptives: #> observed freq. expected freq. specified prob. #> clubs 35 50 0.25 #> diamonds 51 50 0.25 #> hearts 64 50 0.25 #> spades 50 50 0.25 #> #> Test results: #> X-squared statistic: 8.44 #> degrees of freedom: 3 #> p-value: 0.038 ``` ] --- .hi[**Test sa drugim vjerojatnostima**] <br> <br> <br> ```r # Formuliraj druge vjerojatnosti nullProbs <- c(clubs = .2, diamonds = .3, hearts = .3, spades = .2) nullProbs # Pregledaj varijablu ``` ``` #> clubs diamonds hearts spades #> 0.2 0.3 0.3 0.2 ``` --- .tiny[ ```r # Provedi test sa drugačije specificiranim vjerojatnostima goodnessOfFitTest( x = cards$choice_1, p = nullProbs ) ``` ``` #> #> Chi-square test against specified probabilities #> #> Data variable: cards$choice_1 #> #> Hypotheses: #> null: true probabilities are as specified #> alternative: true probabilities differ from those specified #> #> Descriptives: #> observed freq. expected freq. specified prob. #> clubs 35 40 0.2 #> diamonds 51 60 0.3 #> hearts 64 60 0.3 #> spades 50 40 0.2 #> #> Test results: #> X-squared statistic: 4.742 #> degrees of freedom: 3 #> p-value: 0.192 ``` ] --- .hi[**Interpretacija rezultata testa**] <br> <br> <br> <br> 1. Opis podataka i nulte hipoteze. <br> <br> 2. Statistički blok. <br> <br> 3. Interpretacija rezultata. --- layout:false class: middle, inverse # TEST NEZAVISNOSTI KATEGORIJA <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Povezanost kategoričkih varijabli.) --- layout:true # TEST NEZAVISNOSTI KATEGORIJA --- .hi[**Pregled podataka**] ```r str(chapek9) # Pregledaj strukturu podataka ``` ``` #> 'data.frame': 180 obs. of 2 variables: #> $ species: Factor w/ 2 levels "robot","human": 1 2 2 2 1 2 2 1 2 1 ... #> $ choice : Factor w/ 3 levels "puppy","flower",..: 2 3 3 3 3 2 3 3 1 2 ... ``` ```r # Pregledaj podatke head(chapek9) ``` ``` #> species choice #> 1 robot flower #> 2 human data #> 3 human data #> 4 human data #> 5 robot data #> 6 human flower ``` --- .hi[**Pregled podataka**] ```r summary(chapek9) ``` ``` #> species choice #> robot:87 puppy : 28 #> human:93 flower: 43 #> data :109 ``` .hi[**Tabuliraj podatke**] ```r chapekFrequencies <- xtabs( ~ choice + species, data = chapek9) chapekFrequencies # Pregledaj tabulirane podatke ``` ``` #> species #> choice robot human #> puppy 13 15 #> flower 30 13 #> data 44 65 ``` --- .hi[**Tablični prikaz podataka**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:center;"> Robot </th> <th style="text-align:center;"> Human </th> <th style="text-align:center;"> Ukupno </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Puppy </td> <td style="text-align:center;"> 13 </td> <td style="text-align:center;"> 15 </td> <td style="text-align:center;"> 28 </td> </tr> <tr> <td style="text-align:left;"> Flower </td> <td style="text-align:center;"> 30 </td> <td style="text-align:center;"> 13 </td> <td style="text-align:center;"> 43 </td> </tr> <tr> <td style="text-align:left;"> Data file </td> <td style="text-align:center;"> 44 </td> <td style="text-align:center;"> 65 </td> <td style="text-align:center;"> 109 </td> </tr> <tr> <td style="text-align:left;"> Ukupno </td> <td style="text-align:center;"> 87 </td> <td style="text-align:center;"> 93 </td> <td style="text-align:center;"> 180 </td> </tr> </tbody> </table> --- .hi[**Formalni zapis podataka**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> Robot </th> <th style="text-align:left;"> Human </th> <th style="text-align:left;"> Ukupno </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Puppy </td> <td style="text-align:left;"> O_11 </td> <td style="text-align:left;"> O_12 </td> <td style="text-align:left;"> R_1 </td> </tr> <tr> <td style="text-align:left;"> Flower </td> <td style="text-align:left;"> O_21 </td> <td style="text-align:left;"> O_22 </td> <td style="text-align:left;"> R_2 </td> </tr> <tr> <td style="text-align:left;"> Data file </td> <td style="text-align:left;"> O_31 </td> <td style="text-align:left;"> O_32 </td> <td style="text-align:left;"> R_3 </td> </tr> <tr> <td style="text-align:left;"> Ukupno </td> <td style="text-align:left;"> C_1 </td> <td style="text-align:left;"> C_2 </td> <td style="text-align:left;"> N </td> </tr> </tbody> </table> --- .hi[**Formuliraj nultu hipotezu**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> H_0: </td> <td style="text-align:left;"> Sve od sljedećeg je točno: </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> P_11 = P_12 (jednaka vjerojatnost izbora puppy) </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> P_21 = P_22 (jednaka vjerojatnost izbora flower) i </td> </tr> <tr> <td style="text-align:left;"> </td> <td style="text-align:left;"> P_31 = P_32 (jednaka vjerojatnost izbora data). </td> </tr> </tbody> </table> --- .hi[**Formuliraj testnu statistiku**] <br> `$$\hat{P}_i = \frac{R_i}{N}$$` <br> <br> `$$X^2 = \sum_{i=1}^r\sum_{j=1}^c\frac{({E}_{ij} - O_{ij})^2}{{E}}_{ij}}$$` --- .hi[**Stupnjevi slobode kod testa nezavisnosti**] <br> `$$df = (r-1)(c-1)$$` <br> `$$\begin{array}{rcl} df &=& \mbox{(broj opservacija)} - \mbox{(broj ograničenja)} \\ &=& (rc) - (c + (r-1)) \\ &=& rc - c - r + 1 \\ &=& (r - 1)(c - 1) \end{array}$$` <br> `$$\begin{array}{rcl} df &=& \mbox{(broj opservacija)} - \mbox{(broj ograničenja)} \\ &=& rc - ( (c-1) + (r-1) + 1) \\ &=& rc - c - r + 1 \\ &=& (r - 1)(c - 1) \end{array}$$` --- .hi[**Izvođenje testa u R**] <br> <br> ```r # Tabuliraj podatke xtabs( formula = ~ choice + species, data = chapek9 ) ``` ``` #> species #> choice robot human #> puppy 13 15 #> flower 30 13 #> data 44 65 ``` --- .tiny[ ```r # Izvedi test u R associationTest( formula = ~ choice + species, data = chapek9 ) ``` ``` #> #> Chi-square test of categorical association #> #> Variables: choice, species #> #> Hypotheses: #> null: variables are independent of one another #> alternative: some contingency exists between variables #> #> Observed contingency table: #> species #> choice robot human #> puppy 13 15 #> flower 30 13 #> data 44 65 #> #> Expected contingency table under the null hypothesis: #> species #> choice robot human #> puppy 13.5 14.5 #> flower 20.8 22.2 #> data 52.7 56.3 #> #> Test results: #> X-squared statistic: 10.722 #> degrees of freedom: 2 #> p-value: 0.005 #> #> Other information: #> estimated effect size (Cramer's v): 0.244 ``` ] --- .hi[**Korekcija kontinuiranosti**] <br> <br> `$$X^2 = \sum_{i} \frac{(|E_i - O_i| - 0.5)^2}{E_i}$$` .hi[**Efekt veličine**] <br> <br> `$$\phi = \sqrt{\frac{X^2}{N}}$$` --- .hi[**Cramer V**] <br> <br> `$$V = \sqrt{\frac{X^2}{N(k-1)}}$$` .hi[**Izvedi test u R**] <br> <br> ```r cramersV(chapekFrequencies) ``` ``` #> [1] 0.244058 ``` --- layout:false class: middle, inverse # PRETPOSTAVKE TESTA <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Uvjerljivost testa!) --- layout:true # PRETPOSTAVKE TESTA --- .hi[**Radne pretpostavke GOF i testa nezavisnosti**] <br> <br> <br> <br> <br> 1. Očekivane frekvencije su dovoljno velike <br> <br> <br> <br> 2. Podatci su međusobno neovisni <br> <br> --- layout:false class: middle, inverse # IZVOĐENJE CHI-SQ TESTOVA U R <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Uvjerljivost testa!) --- layout:true # IZVOĐENJE CHI-SQ TESTOVA U R --- ```r # Pregledaj podatke podatci ``` ``` #> #> clubs diamonds hearts spades #> 35 51 64 50 ``` ```r # Izvedi chi-sq test na standardni način chisq.test(x = podatci) ``` ``` #> #> Chi-squared test for given probabilities #> #> data: podatci #> X-squared = 8.44, df = 3, p-value = 0.03774 ``` --- ```r # Izvedi chi-sq test na standardni način; drugačije definirane vjerojatnosti chisq.test(x = podatci, p = c(.2,.3,.3,.2)) ``` ``` #> #> Chi-squared test for given probabilities #> #> data: podatci #> X-squared = 4.7417, df = 3, p-value = 0.1917 ``` --- ```r # Test sa drugim podatcima # Pregledaj podatke chapekFrequencies ``` ``` #> species #> choice robot human #> puppy 13 15 #> flower 30 13 #> data 44 65 ``` ```r # Izvedi chi-sq test chisq.test(chapekFrequencies) ``` ``` #> #> Pearson's Chi-squared test #> #> data: chapekFrequencies #> X-squared = 10.722, df = 2, p-value = 0.004697 ``` --- layout:false class: middle, inverse # FISHEROV TEST <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Premalo podataka...) --- layout:true # FISHEROV TEST --- .hi[**Pregledaj podatke**] ```r head(trial,5) # prvih pet redova ``` ``` #> happy on.fire #> 1 TRUE FALSE #> 2 TRUE FALSE #> 3 FALSE FALSE #> 4 FALSE TRUE #> 5 TRUE FALSE ``` ```r str(trial) # Struktura podataka ``` ``` #> 'data.frame': 16 obs. of 2 variables: #> $ happy : logi TRUE TRUE FALSE FALSE TRUE TRUE ... #> $ on.fire: logi FALSE FALSE FALSE TRUE FALSE FALSE ... ``` --- .hi[**Tabuliraj podatke**] ```r salem.tabs <- table( trial ) # Prikaži podatke print( salem.tabs ) ``` ``` #> on.fire #> happy FALSE TRUE #> FALSE 3 3 #> TRUE 10 0 ``` --- .hi[**Provedi test**] ```r # Izvedi chi-sq test na tabuliranim podatcima chisq.test( salem.tabs ) ``` ``` #> #> Pearson's Chi-squared test with Yates' continuity correction #> #> data: salem.tabs #> X-squared = 3.3094, df = 1, p-value = 0.06888 ``` --- .hi[**Pregled varijabli u formalnom zapisu**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> Sretan </th> <th style="text-align:left;"> Tužan </th> <th style="text-align:left;"> Ukupno </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Zapaljena </td> <td style="text-align:left;"> `\(O_{11}\)` </td> <td style="text-align:left;"> `\(O_{12}\)` </td> <td style="text-align:left;"> `\(R_{1}\)` </td> </tr> <tr> <td style="text-align:left;"> Nije zapaljena </td> <td style="text-align:left;"> `\(O_{21}\)` </td> <td style="text-align:left;"> `\(O_{22}\)` </td> <td style="text-align:left;"> `\(R_{2}\)` </td> </tr> <tr> <td style="text-align:left;"> Ukupno </td> <td style="text-align:left;"> `\(C_{1}\)` </td> <td style="text-align:left;"> `\(C_{2}\)` </td> <td style="text-align:left;"> `\(N\)` </td> </tr> </tbody> </table> <br> <br> <br> .hi[**Formalni zapis vjerojatnosti za test**] `$$P(O_{11}, O_{12}, O_{21}, O_{22} \ | \ R_1, R_2, C_1, C_2)$$` --- .hi[**Provedi Fisherov test**] ```r # Izvedi Fisherov test u R fisher.test(salem.tabs) ``` ``` #> #> Fisher's Exact Test for Count Data #> #> data: salem.tabs #> p-value = 0.03571 #> alternative hypothesis: true odds ratio is not equal to 1 #> 95 percent confidence interval: #> 0.000000 1.202913 #> sample estimates: #> odds ratio #> 0 ``` --- layout:false class: middle, inverse # McNEMAROV TEST <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> (Međusobno zavisni podatci...) --- layout:true # McNEMAROV TEST --- .hi[**Pregledaj podatke**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:center;"> Prije </th> <th style="text-align:center;"> Poslije </th> <th style="text-align:center;"> Ukupno </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Da </td> <td style="text-align:center;"> 30 </td> <td style="text-align:center;"> 10 </td> <td style="text-align:center;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Ne </td> <td style="text-align:center;"> 70 </td> <td style="text-align:center;"> 90 </td> <td style="text-align:center;"> 160 </td> </tr> <tr> <td style="text-align:left;"> Ukupno </td> <td style="text-align:center;"> 100 </td> <td style="text-align:center;"> 100 </td> <td style="text-align:center;"> 200 </td> </tr> </tbody> </table> .footnote[[*]Podatci su međusobno zavisni je ima 200 opservacija, a 100 ljudi] --- .hi[**Drugačiji prikaz podataka**] <br> <br> <br> <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:center;"> Prije: Da </th> <th style="text-align:center;"> Prije: Ne </th> <th style="text-align:center;"> Ukupno </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Poslije: Da </td> <td style="text-align:center;"> 5 </td> <td style="text-align:center;"> 5 </td> <td style="text-align:center;"> 10 </td> </tr> <tr> <td style="text-align:left;"> Poslije: Ne </td> <td style="text-align:center;"> 25 </td> <td style="text-align:center;"> 65 </td> <td style="text-align:center;"> 90 </td> </tr> <tr> <td style="text-align:left;"> Ukupno </td> <td style="text-align:center;"> 30 </td> <td style="text-align:center;"> 70 </td> <td style="text-align:center;"> 100 </td> </tr> </tbody> </table> --- .hi[**Formalni zapis testa**] <br> `$$X^2 = \frac{(|b-c| - 0.5)^2}{b+c}$$` <br> <br> `$$X^2 = \frac{(|O_{12}-O_{21}| - 0.5)^2}{O_{12} + O_{21}}$$` --- .hi[**Pregled podataka**] ```r str(agpp) # Pregled podataka ``` ``` #> 'data.frame': 100 obs. of 3 variables: #> $ id : Factor w/ 100 levels "subj.1","subj.10",..: 1 13 24 35 46 57 68 79 90 2 ... #> $ response_before: Factor w/ 2 levels "no","yes": 1 2 2 2 1 1 1 1 1 1 ... #> $ response_after : Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 2 1 1 ... ``` ```r head(agpp,5) # Pregled podataka ``` ``` #> id response_before response_after #> 1 subj.1 no yes #> 2 subj.2 yes no #> 3 subj.3 yes no #> 4 subj.4 yes no #> 5 subj.5 no no ``` --- .hi[**Tabuliraj podatke**] ```r summary(agpp ) # Pregled podataka ``` ``` #> id response_before response_after #> subj.1 : 1 no :70 no :90 #> subj.10 : 1 yes:30 yes:10 #> subj.100: 1 #> subj.11 : 1 #> subj.12 : 1 #> subj.13 : 1 #> (Other) :94 ``` ```r pravaTablica <- xtabs( ~ response_before + response_after, data = agpp) print(pravaTablica) # Pregled podataka ``` ``` #> response_after #> response_before no yes #> no 65 5 #> yes 25 5 ``` --- .hi[**Provedi test u R**] <br> <br> <br> ```r mcnemar.test(pravaTablica) ``` ``` #> #> McNemar's Chi-squared test with continuity correction #> #> data: pravaTablica #> McNemar's chi-squared = 12.033, df = 1, p-value = 0.0005226 ``` --- .hi[**Razlika među testovima**] ```r # Tabuliraj podatke izborKarata <- xtabs( ~ choice_1 + choice_2, data = cards ) izborKarata # Pregled podataka ``` ``` #> choice_2 #> choice_1 clubs diamonds hearts spades #> clubs 10 9 10 6 #> diamonds 20 4 13 14 #> hearts 20 18 3 23 #> spades 18 13 15 4 ``` --- .hi[**Provedi testove**] ```r # Provedi standardni chi-sq test chisq.test(izborKarata) ``` ``` #> #> Pearson's Chi-squared test #> #> data: izborKarata #> X-squared = 29.237, df = 9, p-value = 0.0005909 ``` ```r # Provedi McNemarov tets mcnemar.test(izborKarata) ``` ``` #> #> McNemar's Chi-squared test #> #> data: izborKarata #> McNemar's chi-squared = 16.033, df = 6, p-value = 0.01358 ``` --- 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: Usporedba prosjeka.)