class_docstr: G^2 (log likelihood ratio)s from (Rayson and Garside 2000)
  
  A direct translation of the R function from (Evert 2023)
  Stephanie Evert. 2023. Measuring Keyness. https://osf.io/x8z9n.
  G2.term <- function (O, E) {
    res <- O * log(O / E)
    res[O == 0] <- 0
    res
  }
  
  G2 <- function (f1, f2, N1, N2, alpha=NULL, correct=TRUE) {
    stopifnot(length(f1) == length(f2))
  
    ## observed and expected contingency tables
    N <- N1 + N2
    R1 <- f1 + f2
    O11 <- f1;      E11 <- R1 * N1 / N
    O12 <- f2;      E12 <- R1 * N2 / N
    O21 <- N1 - f1; E21 <- N1 - E11
    O22 <- N2 - f2; E22 <- N2 - E12
  
    ## log-likelihood statistic (simplest formula)
    G2 <- 2 * (G2.term(O11, E11) + G2.term(O12, E12) + G2.term(O21, E21) + G2.term(O22, E22))
    res <- sign(O11 - E11) * G2 # set sign to distinguish positive vs. negative keywords
  
    ## weed out non-significant items if alpha is specified
    if (!is.null(alpha)) {
      if (correct) alpha <- alpha / length(f1)
      theta <- qchisq(alpha, df=1, lower.tail=FALSE)
      res[G2 < theta] <- 0 # set to 0 if not significant at level alpha
    }
  
    res
  }
class_docstr_tok: ["G^2","(","log","likelihood","ratio",")","s","from","(","Rayson","and","Garside","2000",")","A","direct","translation","of","the","R","function","from","(","Evert","2023",")","Stephanie","Evert",".","2023",".","Measuring","Keyness",".","https",":","\/\/osf.io\/x8z9n",".","G2.term","<","-","function","(","O",",","E",")","{","res","<","-","O","*","log","(","O","\/","E",")","res","[","O","==","0","]","<","-","0","res","}","G2","<","-","function","(","f1",",","f2",",","N1",",","N2",",","alpha=NULL",",","correct=TRUE",")","{","stopifnot","(","length","(","f1",")","==","length","(","f2",")",")","#","#","observed","and","expected","contingency","tables","N","<","-","N1","+","N2","R1","<","-","f1","+","f2","O11","<","-","f1",";","E11","<","-","R1","*","N1","\/","N","O12","<","-","f2",";","E12","<","-","R1","*","N2","\/","N","O21","<","-","N1","-","f1",";","E21","<","-","N1","-","E11","O22","<","-","N2","-","f2",";","E22","<","-","N2","-","E12","#","#","log-likelihood","statistic","(","simplest","formula",")","G2","<","-","2","*","(","G2.term","(","O11",",","E11",")","+","G2.term","(","O12",",","E12",")","+","G2.term","(","O21",",","E21",")","+","G2.term","(","O22",",","E22",")",")","res","<","-","sign","(","O11","-","E11",")","*","G2","#","set","sign","to","distinguish","positive","vs.","negative","keywords","#","#","weed","out","non-significant","items","if","alpha","is","specified","if","(","!","is.null","(","alpha",")",")","{","if","(","correct",")","alpha","<","-","alpha","\/","length","(","f1",")","theta","<","-","qchisq","(","alpha",",","df=1",",","lower.tail=FALSE",")","res","[","G2","<","theta","]","<","-","0","#","set","to","0","if","not","significant","at","level","alpha","}","res","}"]
code: def get_score_df(self, label_append=""):
      N1, N2, f1, f2 = self._get_ns_and_fs(())
      gsquare, res = self._get_g2_and_res(N1, N2, f1, f2)
      df = pd.DataFrame(
          {
              "G2": gsquare,
              "Score": res,
              "P": chi2.sf(gsquare, df=1),
          }
      )
      return df.assign(
          CorrectedP=lambda df: fdrcorrection(
              pvals=df.P.values, alpha=0.05, method="indep"
          )[1]
      )
  
file_path: scattertext/scattertext/termscoring/g2.py
imports: import numpy
  import pandas
  from scipy.stats import chi2
  from statsmodels.stats.multitest import fdrcorrection
  from scattertext.termscoring.CorpusBasedTermScorer import CorpusBasedTermScorer
label_desc: Use image node_id 2 for calling the G2 obj's underlying member method code with example usage: obj.get_score_df(label_append) and returns: df