4 scores <- fread("perspective_scores.csv")
5 scores <- scores[,id:=as.character(id)]
7 df <- fread("all_data.csv")
9 # only use the data that has identity annotations
10 df <- df[identity_annotator_count > 0]
12 (df[!(df$id %in% scores$id)])
14 df <- df[scores,on='id',nomatch=NULL]
16 ## how accurate are the classifiers?
18 ## the API claims that these scores are "probabilities"
19 ## say we care about the model of the classification, not the probability
21 F1 <- function(y, predictions){
22 tp <- sum( (predictions == y) & (predictions==1))
23 fn <- sum( (predictions != y) & (predictions!=1))
24 fp <- sum( (predictions != y) & (predictions==1))
25 precision <- tp / (tp + fp)
26 recall <- tp / (tp + fn)
27 return (2 * precision * recall ) / (precision + recall)
30 df[, ":="(identity_attack_pred = identity_attack_prob >=0.5,
31 insult_pred = insult_prob >= 0.5,
32 profanity_pred = profanity_prob >= 0.5,
33 severe_toxicity_pred = severe_toxicity_prob >= 0.5,
34 threat_pred = threat_prob >= 0.5,
35 toxicity_pred = toxicity_prob >= 0.5,
36 identity_attack_coded = identity_attack >= 0.5,
37 insult_coded = insult >= 0.5,
38 profanity_coded = obscene >= 0.5,
39 severe_toxicity_coded = severe_toxicity >= 0.5,
40 threat_coded = threat >= 0.5,
41 toxicity_coded = toxicity >= 0.5
46 ## toxicity is about 93% accurate, with an f1 of 0.8
47 ## identity_attack has high accuracy 97%, but an unfortunant f1 of 0.5.
48 ## threat has high accuracy 99%, but a really bad looking f1 of 0.48.
49 accuracies <- df[,.(identity_attack_acc = mean(identity_attack_pred == identity_attack_coded),
50 insult_pred_acc = mean(insult_pred == insult_coded),
51 profanity_acc = mean(profanity_pred == profanity_coded),
52 severe_toxicity_acc = mean(severe_toxicity_pred == severe_toxicity_coded),
53 theat_acc = mean(threat_pred == threat_coded),
54 toxicity_acc = mean(toxicity_pred == toxicity_coded))]
56 f1s <- df[,.(identity_attack_f1 = F1(identity_attack_coded,identity_attack_pred),
57 insult_f1 = F1(insult_coded,insult_pred),
58 profanity_f1 = F1(profanity_coded,profanity_pred),
59 severe_toxicity_f1 = F1(severe_toxicity_coded,severe_toxicity_pred),
60 theat_f1 = F1(threat_coded,threat_pred),
61 toxicity_f1 = F1(toxicity_coded,toxicity_pred))]
63 positive_cases <- df[,.(identity_attacks = sum(identity_attack_coded),
64 insults = sum(insult_coded),
65 profanities = sum(profanity_coded),
66 severe_toxic_comments = sum(severe_toxicity_coded),
67 threats = sum(threat_coded),
68 toxic_comments = sum(toxicity_coded))]
70 ## there are 50,000 toxic comments, 13000 identity attacks, 30000 insults, 3000 profanities, 8 severe toxic, and 1000 threats.
72 proportions_cases <- df[,.(prop_identity = mean(identity_attack_coded),
73 prop_insults = mean(insult_coded),
74 prop_profanity = mean(profanity_coded),
75 prop_severe = mean(severe_toxicity_coded),
76 prop_threats = mean(threat_coded),
77 prop_toxic = mean(toxicity_coded))]
79 ## at 11% of comments, "toxicity" seems not so badly skewed. Try toxicity first, and if it doesn't work out try insults.
81 ## now look for an example where differential error affects an identity, or a reaction.
82 df <- df[,":="(identity_error = identity_attack_coded - identity_attack_pred,
83 insult_error = insult_coded - insult_pred,
84 profanity_error = profanity_coded - profanity_pred,
85 severe_toxic_error = severe_toxicity_coded - severe_toxicity_pred,
86 threat_error = threat_coded - threat_pred,
87 toxicity_error = toxicity_coded - toxicity_pred)]
89 ## what's correlated with toxicity_error ?
90 df <- df[,approved := rating == "approved"]
92 cortab <- cor(df[,.(toxicity_error,
107 other_sexual_orientation,
118 other_race_or_ethnicity,
120 intellectual_or_learning_disability,
121 psychiatric_or_mental_illness,
124 ## toxicity error is weakly correlated pearson's R = 0.1 with both "white" and "black".
126 ## compare regressions with "white" or "black" as the outcome and "toxicity_coded" or "toxicity_pred" as a predictor.
127 ## here's a great example with presumambly non-differential error: about what identities is toxicity found humorous?
128 ## a bunch of stars reappear when you used the ground-truth data instead of the predictions.
129 ## pro/con of this example: will have to implement family='poisson'.
130 ## shouldn't be that bad though haha.
131 cortab['toxicity_error',]
132 cortab['toxicity_error','funny']
133 cortab['toxicity_coded',]
134 cortab['identity_error',]
137 glm(white ~ toxicity_coded + psychiatric_or_mental_illness, data = df, family=binomial(link='logit'))
139 glm(white ~ toxicity_pred + psychiatric_or_mental_illness, data = df, family=binomial(link='logit'))
141 m1 <- 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_coded, data = df)
143 m2 <- 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)
145 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)
148 glm(white ~ disagree, data = df, family=binomial(link='logit'))
150 ## example with differential error
152 glm(white ~ toxicity_coded + toxicity_error, data=df,family=binomial(link='logit'))
154 glm(toxicity_coded ~ white, data = df, family=binomial(link='logit'))
156 glm(toxicity_pred ~ white, data = df, family=binomial(link='logit'))