-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_build.R
More file actions
140 lines (101 loc) · 5.92 KB
/
Copy pathmodel_build.R
File metadata and controls
140 lines (101 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
library(shiny)
## load in libraries ###########################################################
library(tidyverse)
library(VGAM)
## make functions ##############################################################
abr_columns <- function(data, valid, column) {
# Tidy eval capture of column name
column_quo <- ensym(column)
# Regex pattern with word boundaries
pattern <- str_c("\\b(", str_c(valid, collapse = "|"), ")\\b")
# Perform transformations
data %>%
mutate(
# Convert to lowercase for matching
temp_col = str_to_lower(!!column_quo),
# Step 1: If more than one valid value appears, set to "other"
temp_col = if_else(str_count(temp_col, pattern) > 1, "other", temp_col),
# Step 2: If there's at least one valid value, extract and keep it
temp_col = if_else(str_detect(temp_col, pattern), str_extract(temp_col, pattern), temp_col),
# Step 3: If no valid value was found at all, set to "other"
temp_col = if_else(str_detect(temp_col, pattern), temp_col, "other"),
# Final result replaces original column
!!column_quo := temp_col
) %>%
select(-temp_col) # clean up temp column
}
get_prediction <- function(model, data) {
probs <- predict(model, data, type = "response")
predicts <- apply(probs, 1, which.max)
accuracy <- mean(reduced_data$refined_class[-index] == predicts)
return(round(accuracy, 4)*100)
}
mse <- function(model, data) {
probs <- predict(model, data, type = "response")
predicts <- apply(probs, 1, which.max)
mse <- sqrt(mean((reduced_data$refined_class[-index] - predicts)^2))
return(round(mse, 2))
}
race_transform <- function(race) {
new_race <- ifelse(race == "human", "Human",
ifelse(race == "elf", "Elf",
ifelse(race %in% c("tiefling", "dragonborn"), "Monster",
ifelse(race %in% c("gnome", "dwarf", "halfling"), "Short",
ifelse(race %in% c("half-orc", "half-elf"), "Hybrid",
ifelse(race == "other", "Other", NA))))))
return(new_race)
}
background_transform <- function(background) {
new_background <- ifelse(background %in% c("charlatan", "criminal"), "Shady",
ifelse(background %in% c("sailor", "guild artisan", "entertainer", "soldier"), "Industrial",
ifelse(background %in% c("hermit", "urchin", "outlander"), "Loner",
ifelse(background %in% c("noble", "folk hero"), "Famous",
ifelse(background %in% c("sage", "acolyte"), "Magical",
ifelse(background == "other", "Other", NA))))))
return(new_background)
}
class_transform <- function(class) {
new_class <- ifelse(class %in% c("rogue", "barbarian", "fighter"), 1,
ifelse(class %in% c("monk", "ranger", "paladin"), 2,
ifelse(class %in% c("bard", "cleric", "druid", "warlock"), 3,
ifelse(class %in% c("sorcerer", "wizard"), 4, NA))))
return(new_class)
}
## start some sweet analysis ###################################################
## read in polished data
data <- read.csv("final_data.csv")
data <- data[data$level > 0, ]
data <- data[data$hp >= 0, ]
## due to computation difficulties, lets summarize this data
data$refined_class <- ifelse(data$class %in% c("rogue", "barbarian", "fighter"), 1,
ifelse(data$class %in% c("monk", "ranger", "paladin"), 2,
ifelse(data$class %in% c("bard", "cleric", "druid", "warlock"), 3,
ifelse(data$class %in% c("sorcerer", "wizard"), 4, NA))))
data$refined_race <- ifelse(data$race == "human", "Human",
ifelse(data$race == "elf", "Elf",
ifelse(data$race %in% c("tiefling", "dragonborn"), "Monster",
ifelse(data$race %in% c("gnome", "dwarf", "halfling"), "Short",
ifelse(data$race %in% c("half-orc", "half-elf"), "Hybrid",
ifelse(data$race == "other", "Other", NA))))))
data$refined_background <- ifelse(data$background %in% c("charlatan", "criminal"), "Shady",
ifelse(data$background %in% c("sailor", "guild artisan", "entertainer", "soldier"), "Industrial",
ifelse(data$background %in% c("hermit", "urchin", "outlander"), "Loner",
ifelse(data$background %in% c("noble", "folk hero"), "Famous",
ifelse(data$background %in% c("sage", "acolyte"), "Magical",
ifelse(data$background == "other", "Other", NA))))))
## now drop anything in an other category to have less data
reduced_data <- data[data$race != "other" & data$background != "other", ]
set.seed(2007)
index <- sample(1:nrow(reduced_data), size = 20000)
new_data <- reduced_data[index, ]
new_data$refined_background <- as.factor(new_data$refined_background)
new_data$refined_race <- as.factor(new_data$refined_race)
test_data <- reduced_data[-index, c(1:7,11,13:14)]
## continuation categories
contratio_fit <- vglm(refined_class ~ hp + strength + dex +
const + intelligence + wisdom +
charisma + level +
refined_race + refined_background,
sratio,
data = new_data)
saveRDS(contratio_fit, "contratio_model.RDS")