library(tidyr)
library(dplyr)
library(ggplot2)
library(vcd)
library(reshape2)
library(stats)
library(png)
library(gridExtra)
library(kableExtra)
library(VennDiagram)
library(cowplot)
library(stringr)

Part 3

THIS IS THE NOT-NORMALISED VERSION; JS THE COUNTS THEMSELVES SO WE CAN USE FISHER’S TEST

Part 3.1: Association of various factors with RV tags

data.format <- function (df,tag.var){

  unique.tag <- unique(df$tag[grep(paste(tag.var,collapse='|'), df$tag)])
  unique.tag <- unique.tag[order(unique.tag)]

  v.r.<- c('value.relational.social','value.relational.care',
           'value.relational.spiritual','value.relational.eudaimonia')
  v.r.label <- c('Social','Care','Spiritual','Eudaimonia')
  if (length(unique.tag)==1){

    df.out <- data.frame(matrix(0, ncol = length(v.r.), nrow = (length(unique.tag) + 1)))
    rownames(df.out)<-c(unique.tag,'opp')
    colnames(df.out)<-v.r.label
  }
  else{
    df.out <- data.frame(matrix(0, ncol = length(v.r.), nrow = length(unique.tag)))
    rownames(df.out)<-unique.tag
    colnames(df.out)<-v.r.label
  }

  int.num <- 1


  while (int.num <=30) {

      temp <- df[df$document==(paste0('Interview ',int.num)),]

      temp <- temp %>% distinct(tag, .keep_all = TRUE)

      if (length(unique.tag)<1){

        tag.full <- c(temp$tag[grep(paste(tag.var,collapse='|'),temp$tag)],'opp')
      }else{

        tag.full <- temp$tag[grep(paste(tag.var,collapse='|'),temp$tag)]
      }

      if (length(tag.full)==0){
        tag.full = 0
      }
      # tag.full <- ifelse(length(unique.tag)==1,
      #                    c(temp$tag[grep(tag.var,temp$tag)],'opp'),
      #                    temp$tag[grep(tag.var,temp$tag)])

      for (x in 1:length(tag.full)){

        if ((length(tag.full)!=0) && (!is.na(tag.full[x])) && sum(grepl(tag.full[x],temp$tag)) >=1){

        for (i in 1:nrow(temp)){

          for (j in 1:length(v.r.)){

            if (v.r.[j]==temp$tag[i]){

              df.out[tag.full[x],v.r.label[j]] = df.out[tag.full[x],v.r.label[j]]+1

              }
            }
          }
        }
        else{

          if (!'opp' %in% rownames(df.out)) {
            new.row <- data.frame(matrix(0, ncol = length(v.r.), nrow = 1))
            colnames(new.row) <- v.r.label
            rownames(new.row) <- 'opp'
            df.out <- rbind(df.out, new.row)
            }

          for (i in 1:nrow(temp)){

          for (j in 1:length(v.r.)){

            if (v.r.[j]==temp$tag[i]){

              df.out['opp',v.r.label[j]] = df.out['opp',v.r.label[j]]+1

              }
          }
          }
        }

      }
  int.num = int.num+1
  }



  return (df.out)
  }
  
##
# https://www-jstor-org.remotexs.ntu.edu.sg/stable/2288652?seq=7
# proof that fisher test can be used outside of 2x2 --> limited only by computational limits but can with R (mostly)

data.age <- data.format(data4,'age\\.')
data.age.plot <- data.age;data.age.plot$Age.Group<-rownames(data.age)
data.age.all <- data.age; data.age.all$RVs <- rowSums(data.age.all)
data.age.all <- data.age.all[,'RVs',drop=F]
data.age.all.plot <- data.age.all;data.age.all.plot$Age.Group <- rownames(data.age.all)

data.freq <- data.format(data4,'frequency\\.')
data.freq <- data.freq[c('frequency.rare',
                         'frequency.occasionally',
                         'frequency.regular',
                         'frequency.frequent'),]
data.freq.plot <- data.freq;data.freq.plot$freq<-rownames(data.freq)
data.freq.all <- data.freq; data.freq.all$RVs <- rowSums(data.freq.all)
data.freq.all <- data.freq.all[,'RVs',drop=F]
data.freq.all.plot <- data.freq.all;data.freq.all.plot$freq <- rownames(data.freq.all)

data.usual <- data.format(data4,'usual\\.')
data.usual <- data.usual[c('usual.alone','usual.sameforboth','usual.wothers'),]
data.usual.plot <- data.usual;data.usual.plot$usual<-rownames(data.usual)
data.usual.all <- data.usual; data.usual.all$RVs <- rowSums(data.usual.all)
data.usual.all <- data.usual.all[,'RVs',drop=F] 
data.usual.all.plot <- data.usual.all;data.usual.all.plot$usual <- rownames(data.usual.all)

data.household <- data.format(data4,'household\\.')
data.household <- data.household[c('household.alone','household.wothers'),]
data.household.plot <- data.household;data.household.plot$household<-rownames(data.household)
data.household.all <- data.household; data.household.all$RVs <- rowSums(data.household.all)
data.household.all <- data.household.all[,'RVs',drop=F] 
data.household.all.plot <- data.household.all
data.household.all.plot$household <- rownames(data.household.all)

data.nationality <- data.format(data4,'nationality\\.')
data.nationality <- data.nationality[c('nationality.nonsg','nationality.sg'),]
data.nationality.plot <- data.nationality;data.nationality.plot$nationality<-rownames(data.nationality)
data.nationality.all <- data.nationality; data.nationality.all$RVs <- rowSums(data.nationality.all)
data.nationality.all <- data.nationality.all[,'RVs',drop=F] 
data.nationality.all.plot <- data.nationality.all
data.nationality.all.plot$nationality <- rownames(data.nationality.all)

# for the activities; changed the categories to work (work+study), 
# exercise (walk+other exercise), hangout with friends and family, 
# relax and others (birdwatching+naturephotog + picnic)
# this is because there are too many categories which cannot be analysed
# this is the only one that will be analysed through chisq 
# rather than fisher's test for this same reason

data.act <- data.format(data4,'activities\\.')
data.act['activities.work',] <- data.act['activities.work',]+data.act['activities.study',]
data.act <- data.act[!rownames(data.act)=='activities.study',,drop=F]
data.act['activities.exercise',] <- data.act['activities.exercise',]+data.act['activities.walk',]
data.act <- data.act[!rownames(data.act)=='activities.walk',,drop=F]
data.act['activities.others',] <- data.act['activities.birdwatching',]+
  data.act['activities.naturephotog',]+data.act['activities.picnic',]
data.act <- data.act[!rownames(data.act)=='activities.birdwatching',,drop=F]
data.act <- data.act[!rownames(data.act)=='activities.naturephotog',,drop=F]
data.act <- data.act[!rownames(data.act)=='activities.picnic',,drop=F]
data.act <- data.act[c('activities.work',
                     'activities.exercise',
                     'activities.hangout',
                     'activities.relax',
                     'activities.others'),]

data.act.plot <- data.act;data.act.plot$act<-c('activities.1work',
                     'activities.2exercise',
                     'activities.3hangout',
                     'activities.4relax',
                     'activities.5others')
data.act.all <- data.act; data.act.all$RVs <- rowSums(data.act.all)
data.act.all <- data.act.all[,'RVs',drop=F]
data.act.all.plot <- data.act.all
data.act.all.plot$act <- c('activities.1work',
                     'activities.2exercise',
                     'activities.3hangout',
                     'activities.4relax',
                     'activities.5others')

# rejected using chisq test in the end; bc data points are too small:
# chisq.test(data.age)
# chisq.test(data.freq)
# chisq.test(data.usual)
# chisq.test(data.household)
# chisq.test(data.nationality)
# chisq.test(data.act)
# rejected using a poisson model as well because it overfits the data

# NOTE: stats test here may not be very useful due to sample size limitations
# but plotted raw data may already show some inkling of a story

Part 3.1.1: Age (Fisher’s Test)

fisher.test(data.age)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.age
## p-value = 0.9982
## alternative hypothesis: two.sided

Part 3.1.2: Frequency (Fisher’s Test)

fisher.test(data.freq)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.freq
## p-value = 0.4142
## alternative hypothesis: two.sided

Part 3.1.3: Usually with Others, Alone, or Both (Fisher’s Test)

fisher.test(data.usual)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.usual
## p-value = 0.577
## alternative hypothesis: two.sided

Part 3.1.4: Household (Fisher’s Test)

fisher.test(data.household)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.household
## p-value = 1
## alternative hypothesis: two.sided

Part 3.1.5: Nationality (Fisher’s Test)

fisher.test(data.nationality)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.nationality
## p-value = 0.9214
## alternative hypothesis: two.sided

Part 3.1.5: Activities (Chisq Test)

data.act.plot$act <- factor(data.act.plot$act)

#fisher.test(data.act) # too big to do fishers test
chisq.test(data.act)
## Warning in chisq.test(data.act): Chi-squared approximation may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  data.act
## X-squared = 10.849, df = 12, p-value = 0.5419

Part 3.2: Association of mentionsothercountry with RVs (Fisher’s Test)

data.mot <- data.format(data4,'mentionsothercountry')
rownames(data.mot)[rownames(data.mot)=='opp']<- 'doesnotmentionsothercountry'

fisher.test(data.mot)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.mot
## p-value = 1
## alternative hypothesis: two.sided

Part 3.3: Association of young factors with RVs (Fisher’s Test)

data.young <- data.format(data4,'young')
rownames(data.young)[rownames(data.young)=='opp']<- 'Uncertain'

data.young <- data.young[c('Uncertain','young.not',
                                   'young.not.butwanted','young.butdidntaffect',
                                   'young.young'),]
fisher.test(data.young)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  data.young
## p-value = 0.8815
## alternative hypothesis: two.sided