set.seed(1111)
source('load_perspective_data.R')
## how accurate are the classifiers?

## the API claims that these scores are "probabilities"
## say we care about the model of the classification, not the probability

## toxicity error is weakly correlated pearson's R = 0.1 with both "white" and "black".

## compare regressions with "white" or "black" as the outcome and "toxicity_coded" or "toxicity_pred" as a predictor.
## here's a great example with presumambly non-differential error: about what identities is toxicity found humorous?
## a bunch of stars reappear when you used the ground-truth data instead of the predictions.
## pro/con of this example: will have to implement family='poisson'.
## shouldn't be that bad though haha.
cortab['toxicity_error',]
cortab['toxicity_error','funny']
cortab['toxicity_coded',]
cortab['identity_error',]
cortab['white',]

## here's a simple example, is P(white | toxic and mentally ill) > P(white | toxic or mentally ill). Are people who discuss their mental illness in a toxic way more likely to be white compared to those who just talk about their mental illness or are toxic? 
summary(glm(white ~ toxicity_coded*psychiatric_or_mental_illness, data = df, family=binomial(link='logit')))

summary(glm(white ~ toxicity_pred*psychiatric_or_mental_illness, data = df, family=binomial(link='logit')))

summary(glm(white ~ toxicity_coded*male, data = df, family=binomial(link='logit')))

summary(glm(white ~ toxicity_pred*male, data = df, family=binomial(link='logit')))

summary(glm(toxicity_coded ~ white*psychiatric_or_mental_illness, data = df, family=binomial(link='logit')))

summary(glm(toxicity_pred ~ white*psychiatric_or_mental_illness, data = df, family=binomial(link='logit')))


## another simple enough example: is P(toxic | funny and white) > P(toxic | funny nand white)? Or, are funny comments more toxic when people disclose that they are white?

summary(glm(toxicity_pred ~ funny*white, data=df, family=binomial(link='logit')))
summary(glm(toxicity_coded ~ funny*white, data=df, family=binomial(link='logit')))

source("../simulations/measerr_methods.R")
                                                                                       
saved_model_file <- "measerr_model_tox.eq.funny.cross.white.RDS"
overwrite_model <- TRUE 

# it works so far with a 20% and 15% sample. Smaller is better. let's try a 10% sample again. It didn't work out. We'll go forward with a 15% sample.
df_measerr_method <- copy(df)[sample(1:.N, 0.05 * .N), toxicity_coded_1 := toxicity_coded]
df_measerr_method <- df_measerr_method[,toxicity_coded := toxicity_coded_1]
summary(glm(toxicity_coded ~ funny*white, data=df_measerr_method[!is.na(toxicity_coded)], family=binomial(link='logit')))

if(!file.exists(saved_model_file) || (overwrite_model == TRUE)){
    measerr_model <- measerr_mle_dv(df_measerr_method,toxicity_coded ~ funny*white,outcome_family=binomial(link='logit'), proxy_formula=toxicity_pred ~ toxicity_coded*funny*white)
saveRDS(measerr_model, saved_model_file)
} else {
    measerr_model <- readRDS(saved_model_file)
}

inv_hessian <- solve(measerr_model$hessian)
se <- diag(inv_hessian)

lm2 <- glm.nb(funny ~ (male + female + transgender + other_gender + heterosexual + bisexual + other_sexual_orientation + christian + jewish + hindu + buddhist + atheist + other_religion + asian + latino + other_race_or_ethnicity + physical_disability + intellectual_or_learning_disability + white + black + psychiatric_or_mental_illness)*toxicity_pred, data = df)
m3 <- glm.nb(funny ~ (male + female + transgender + other_gender + heterosexual + bisexual + other_sexual_orientation + christian + jewish + hindu + buddhist + atheist + other_religion + asian + latino + other_race_or_ethnicity + physical_disability + intellectual_or_learning_disability + white + black + psychiatric_or_mental_illness)*toxicity, data = df)


glm(white ~ disagree, data = df, family=binomial(link='logit'))

## example with differential error

glm(white ~ toxicity_coded + toxicity_error, data=df,family=binomial(link='logit'))

glm(toxicity_coded ~ white, data = df, family=binomial(link='logit'))

glm(toxicity_pred ~ white, data = df, family=binomial(link='logit'))
