4545#
4646# install.packages(tidyverse)
4747# install.packages(class)
48+ # install.packages(gmodels)
4849
4950library(tidyverse )
5051
@@ -58,7 +59,7 @@ library(tidyverse)
5859# Note: this is set to import the file from GitHub. If you have downloaded the
5960# file already, you can change the path to where the file is on your computer.
6061
61- Characters <- read_csv(" https://raw.githubusercontent.com/jilltxt/algorithmic_failure /main/data/characters.csv" ) %> %
62+ Characters <- read_csv(" https://raw.githubusercontent.com/jilltxt/algorithmicfailure /main/data/characters.csv" ) %> %
6263 select(Character , Species , Gender , Sexuality ,
6364 RaceOrEthnicity , Age ) %> %
6465 na_if(" Unknown" ) %> %
@@ -109,7 +110,7 @@ traits <- c("Child", "Young Adult", "Adult", "Elderly",
109110#
110111# If you haven't installed the
111112
112- Situations <- read_csv(" https://raw.githubusercontent.com/jilltxt/algorithmic_failure /main/data/situations.csv" ,
113+ Situations <- read_csv(" https://raw.githubusercontent.com/jilltxt/algorithmicfailure /main/data/situations.csv" ,
113114 col_types = cols(
114115 SituationID = col_integer(),
115116 Situation = col_skip(),
@@ -277,7 +278,7 @@ library(class)
277278
278279normalise <- function (x ) {
279280 return ((x - min(x )) / (max(x ) - min(x )))
280- }
281+ }
281282
282283# convert the Verb column to rownames, since the normalising needs all numeric data
283284# variables - but rownames are fine and used as labels in the plot.
@@ -322,7 +323,13 @@ test <- as.data.frame(lapply(test, normalise))
322323# the verb is active or passive). So we remove first two columns (the verb and
323324# whether or not it is active) from both the
324325# training and test subsets.
325-
326+ #
327+ # Even though it doesn't SEEM like this should be necessary, we have to set.seed() again.
328+ # The knn algorithm is mostly deterministic, but uses randomness when there is a
329+ # tie - that is, when two neighbours are equally close, it choses one at random to
330+ # make its prediction. If the set.seed(2022) isn't repeated, the results will change
331+ # slightly from time to time.
332+ set.seed(2022 )
326333prediction <- knn(train = train [- c(1 : 2 )], test = test [- c(1 : 2 )], cl = train $ target , k = 1 )
327334
328335
@@ -345,12 +352,17 @@ library(gmodels)
345352
346353CrossTable(x = test $ target , y = prediction , prop.chisq = FALSE )
347354
348- # Top left cell is true passive results. The algorithm correctly predicted 28
349- # passive verbs. The bottom number in this cell is the proportion of all results
350- # that were true passive: 12.4%. Above that is the accuracy for all passive
351- # predictions: 38.8
352- # The (middle) bottom right cell is true actives: it correctly
353- # predicted that 96 active verbs were active.
355+ # The output first states what the Cell Contents mean, then shows a cross table
356+ # where the ACTUAL target is shown horizontally, and the predictions vertically.
357+
358+
359+ # AFTER ADDING SET.SEED:
360+ # It predicts that 34.7% of verbs are passive, while actually, 36% are passive.
361+ # It predicts that 65.3% of verbs are active, while actually, 64% are active
362+ # Overall accuracy rate is 56%
363+ # 35.8% accurate for passive verbs
364+ # 64.6% accurate for active verbs.
365+
354366
355367
356368# Identify false predictions ----------------------------------------------
@@ -471,6 +483,7 @@ Character_verb_predictions %>%
471483 arrange(desc(Count ))
472484
473485
486+
474487# LIST FALSE ACTIVES ------------------------------------------------------
475488
476489Character_verb_predictions %> %
@@ -487,23 +500,34 @@ Character_verb_predictions %>%
487500 add_count(Verb ) %> % # adds a column n with count of how many times Verb occurs
488501 distinct() %> % # remove duplicates
489502 arrange(desc(n )) %> %
490- top_n(10 ) %> % # only show the top 10 in n (i.e. 10 most frequently used verbs)
491- group_by(Prediction_type ) %> %
492- summarise(proportion = n() / nrow(. ) )
503+ top_n(10 ) # only show the top 10 in n (i.e. 10 most frequently used verbs)
504+
505+ # All the top 10 are accurate predictions.
506+ # If wantingn to look at more than the top 10, could summarise the proportions of
507+ # each type Prediction_type by adding the following two lines to the code above:
508+ # group_by(Prediction_type) %>%
509+ # summarise(proportion = n() / nrow(.) )
493510# Results are given as decimals adding up to 1
494511
495512# Calculate accuracy of all except top ten -------------------------------
496513
497514Character_verb_predictions %> %
498- select(Verb , Prediction_type ) %> %
515+ select(Verb , prediction , Prediction_type , target ) %> %
499516 add_count(Verb ) %> % # adds a column n with count of how many times Verb occurs
500517 distinct() %> % # remove duplicates
501518 arrange(desc(n )) %> %
502519 slice_tail(n = - 10 ) %> % # remove the top ten rows
503520 group_by(Prediction_type ) %> %
504- summarise(proportion = n() / nrow(. ) )
505-
521+ summarise(proportion = n() / nrow(. ))
506522
523+ # Calculate accuracy of all -------------------------------
507524
525+ Character_verb_predictions %> %
526+ select(Verb , prediction , Prediction_type , target ) %> %
527+ add_count(Verb ) %> % # adds a column n with count of how many times Verb occurs
528+ distinct() %> % # remove duplicates
529+ arrange(desc(n )) %> %
530+ group_by(Prediction_type ) %> %
531+ summarise(proportion = n() / nrow(. ))
508532
509533
0 commit comments