G-test

att = read.csv("attitudes.csv", header = T)

distance v attitude for SEOT

From qualtrics: There is a statistically significant relationship between Q8_1: When you encounter sea otters and harbor…lose do you generally get to them? | sea otters and Q9_4: What is your general attitude towards sea otters and harbor seals? | sea otters

Q8v9 = att %>% select ("Q8_1", "Q9_1", "ResponseId")
dim(Q8v9)
## [1] 331   3

turn Q8_1 into an integer

Q8v9$Q8_1 = as.integer(Q8v9$Q8_1) 

bin distances

Q8v9$binned.Q8 <- as.factor(ifelse(Q8v9$Q8 <=10, 'B1 <=10',      
                     ifelse(Q8v9$Q8<52, 'B2 11-51',
                     ifelse(Q8v9$Q8<103, 'B3 52-102', 
                     ifelse(Q8v9$Q8<154, 'B4 103-153',
                     ifelse(Q8v9$Q8<205, 'B5 154-204',
                     ifelse(Q8v9$Q8<256, 'B6 206-255', 
                     ifelse(Q8v9$Q8 <300, 'B7 256-299',
                     ifelse(Q8v9$Q8 >=300, 'B8 >= 300', 'Transfer')))))))))

remove any rows with NAs

Q8v9 <- na.omit(Q8v9)
dim(Q8v9)
## [1] 289   4

Response ID R_12KtQUffRaYUb7b still had no value for q9 so I omitted that.

Q8v9 = Q8v9[!grepl("R_12KtQUffRaYUb7b", Q8v9$ResponseId),]
dim(Q8v9)
## [1] 288   4

Create a contingency table (I made sure it matches what was in Qualtrics)

Q8v9_table = table(Q8v9$binned.Q8, Q8v9$Q9) 
#Q8v9_table

Run a Chi-square test for independence; these results are the same as what came from Qualtrics

chisq_Q8v9 = chisq.test(Q8v9_table) 
chisq_Q8v9
## 
##  Pearson's Chi-squared test
## 
## data:  Q8v9_table
## X-squared = 38.543, df = 14, p-value = 0.0004289

Look at which pairs contribute most to the significance. First look at the Pearson’s residuals

round(chisq_Q8v9$residuals, 2)
##             
##              I don't like seeing them I love seeing them
##   B1 <=10                       -0.50              -0.69
##   B2 11-51                      -0.49              -0.31
##   B3 52-102                      0.31               0.27
##   B4 103-153                    -1.05               0.42
##   B5 154-204                    -0.58               0.23
##   B6 206-255                     1.40              -0.08
##   B7 256-299                     2.78              -0.33
##   B8 >= 300                      0.35              -0.02
##             
##              Neither like nor dislike
##   B1 <=10                        4.00
##   B2 11-51                       2.00
##   B3 52-102                     -1.64
##   B4 103-153                    -1.36
##   B5 154-204                    -0.75
##   B6 206-255                    -0.67
##   B7 256-299                    -0.42
##   B8 >= 300                     -0.17

Now see what percentage each pair contributes to the overall effect.

contrib_Q8v9 <- 100*(chisq_Q8v9$residuals^2/chisq_Q8v9$statistic)
round(contrib_Q8v9, 3)
##             
##              I don't like seeing them I love seeing them
##   B1 <=10                       0.649              1.246
##   B2 11-51                      0.624              0.250
##   B3 52-102                     0.253              0.185
##   B4 103-153                    2.865              0.449
##   B5 154-204                    0.865              0.136
##   B6 206-255                    5.093              0.016
##   B7 256-299                   19.988              0.287
##   B8 >= 300                     0.312              0.001
##             
##              Neither like nor dislike
##   B1 <=10                      41.555
##   B2 11-51                     10.380
##   B3 52-102                     6.937
##   B4 103-153                    4.775
##   B5 154-204                    1.441
##   B6 206-255                    1.171
##   B7 256-299                    0.450
##   B8 >= 300                     0.072

Plot the relationships between pairs.

corrplot(chisq_Q8v9$residuals, is.cor = FALSE)

Run a G-test

GTest(x = Q8v9_table, correct = "none")
## 
##  Log likelihood ratio (G-test) test of independence without correction
## 
## data:  Q8v9_table
## G = 29.825, X-squared df = 14, p-value = 0.008068

Pairwise G-test

pairwise_Q8vQ9 <- pairwise.G.test(Q8v9_table)
pairwise_Q8vQ9
## 
##  Pairwise comparisons using G-tests 
## 
## data:  Q8v9_table 
## 
##            B1 <=10 B2 11-51 B3 52-102 B4 103-153 B5 154-204 B6 206-255
## B2 11-51   0.427   -        -         -          -          -         
## B3 52-102  0.038   0.117    -         -          -          -         
## B4 103-153 0.063   0.163    0.511     -          -          -         
## B5 154-204 0.273   0.448    0.757     1.000      -          -         
## B6 206-255 0.273   0.418    0.757     0.418      0.577      -         
## B7 256-299 0.320   0.384    0.511     0.273      0.427      0.806     
## B8 >= 300  0.273   0.622    0.495     0.384      0.577      0.674     
##            B7 256-299
## B2 11-51   -         
## B3 52-102  -         
## B4 103-153 -         
## B5 154-204 -         
## B6 206-255 -         
## B7 256-299 -         
## B8 >= 300  0.520     
## 
## P value adjustment method: fdr

relationship v attitude for SEOT

From qualtrics: There is a statistically significant relationship between Q3: What is your relationship with Homer? and Q9_4: What is your general attitude towards sea otters and harbor seals? | sea otters

Q3v9 = att %>% select ("Q3", "Q9_1", "ResponseId")
dim(Q3v9)
## [1] 331   3

remove any rows with NAs

Q3v9 <- na.omit(Q3v9)
dim(Q3v9)
## [1] 331   3

Response IDs R_1rue2tn8fn8a5Ps R_3XE5rI11Shr6nYZ R_3SH2f5YqJ1todmk R_7LdtSOSwT5bECw1 R_12KtQUffRaYUb7b

still had no value for q9 so I omitted them.

Q3v9 = Q3v9[!grepl("R_1rue2tn8fn8a5Ps", Q3v9$ResponseId),]
Q3v9 = Q3v9[!grepl("R_3XE5rI11Shr6nYZ", Q3v9$ResponseId),]
Q3v9 = Q3v9[!grepl("R_3SH2f5YqJ1todmk", Q3v9$ResponseId),]
Q3v9 = Q3v9[!grepl("R_7LdtSOSwT5bECw1", Q3v9$ResponseId),]
Q3v9 = Q3v9[!grepl("R_12KtQUffRaYUb7b", Q3v9$ResponseId),]
dim(Q3v9)
## [1] 326   3

Response IDs R_1ZZ5435aHC9X0Ax R_3w0Qkvfaatk3Lyr

still had no value for Q3 so I omitted them.

Q3v9 = Q3v9[!grepl("R_1ZZ5435aHC9X0Ax", Q3v9$ResponseId),]
Q3v9 = Q3v9[!grepl("R_3w0Qkvfaatk3Lyr", Q3v9$ResponseId),]
dim(Q3v9)
## [1] 324   3

Create a contingency table (I made sure it matches what was in Qualtrics)

Q3v9_table = table(Q3v9$Q3, Q3v9$Q9_1) 
#Q3v9_table

Run a Chi-square test for independence; these results are the same as what came from Qualtrics

chisq_Q3v9 = chisq.test(Q3v9_table) 
chisq_Q3v9
## 
##  Pearson's Chi-squared test
## 
## data:  Q3v9_table
## X-squared = 36.846, df = 6, p-value = 1.887e-06

Look at which pairs contribute most to the significance. First look at the Pearson’s residuals

round(chisq_Q3v9$residuals, 2)
##                                       
##                                        I don't like seeing them
##   I am not an Alaska resident                             -0.62
##   I am visiting from my home in Alaska                     0.81
##   I live here                                              0.18
##   I work here                                             -0.43
##                                       
##                                        I love seeing them
##   I am not an Alaska resident                        0.46
##   I am visiting from my home in Alaska               0.14
##   I live here                                       -1.02
##   I work here                                       -0.15
##                                       
##                                        Neither like nor dislike
##   I am not an Alaska resident                             -1.96
##   I am visiting from my home in Alaska                    -1.32
##   I live here                                              5.24
##   I work here                                              1.13

Now see what percentage each pair contributes to the overall effect.

contrib_Q3v9 <- 100*(chisq_Q3v9$residuals^2/chisq_Q3v9$statistic)
round(contrib_Q3v9, 3)
##                                       
##                                        I don't like seeing them
##   I am not an Alaska resident                             1.048
##   I am visiting from my home in Alaska                    1.774
##   I live here                                             0.090
##   I work here                                             0.503
##                                       
##                                        I love seeing them
##   I am not an Alaska resident                       0.570
##   I am visiting from my home in Alaska              0.051
##   I live here                                       2.805
##   I work here                                       0.065
##                                       
##                                        Neither like nor dislike
##   I am not an Alaska resident                            10.438
##   I am visiting from my home in Alaska                    4.754
##   I live here                                            74.414
##   I work here                                             3.487

Plot the relationships between pairs.

corrplot(chisq_Q3v9$residuals, is.cor = FALSE)

Run a G-test

GTest(x = Q3v9_table, correct = "none")
## 
##  Log likelihood ratio (G-test) test of independence without correction
## 
## data:  Q3v9_table
## G = 25.435, X-squared df = 6, p-value = 0.0002835

Pairwise G-test

pairwise_Q3vQ9 <- pairwise.G.test(Q3v9_table)
pairwise_Q3vQ9
## 
##  Pairwise comparisons using G-tests 
## 
## data:  Q3v9_table 
## 
##                                      I am not an Alaska resident
## I am visiting from my home in Alaska 0.65700                    
## I live here                          0.00021                    
## I work here                          0.36197                    
##                                      I am visiting from my home in Alaska
## I am visiting from my home in Alaska -                                   
## I live here                          0.00228                             
## I work here                          0.36197                             
##                                      I live here
## I am visiting from my home in Alaska -          
## I live here                          -          
## I work here                          0.65700    
## 
## P value adjustment method: fdr