-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.r
133 lines (119 loc) · 3.28 KB
/
global.r
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
library(readr)
library(data.table)
library(psych)
library(qgraph)
library(httr)
library(jsonlite)
library(ade4)
openAIkey <- readLines("openai.key")
#### Define functions ####
requestChatCompletion <- function(messages, model) {
body <- list(
model = model,
messages = messages,
temperature = 1,
max_tokens = 6,
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0
)
r <- POST('https://api.openai.com/v1/chat/completions',
body = toJSON(body, auto_unbox = TRUE),
httr::add_headers(Authorization = paste("Bearer", openAIkey)),
httr::content_type('application/json'))
response <- content(r)
return(response)
}
getAnswers <- function(preprompt, questions, model = "gpt-4") {
completions <- c()
messages <- list(
list(
role = "system",
content = preprompt
)
)
for (i in seq_along(questions)) {
messages <- append(messages, list(
list(
role = "user",
content = questions[i]
)
))
completions <- append(completions, requestChatCompletion(messages = messages, model)$choices[[1]]$
message$
content)
messages <- append(messages, list(
list(
role = "assistant",
content = completions[i]
)
))
}
return(completions)
}
plot_mds <- function(fit, brands, file, title, colours) {
x <- fit$points[, 1]
y <- fit$points[, 2]
plot_map(brands, x, y, file, title, colours)
}
plot_map <- function(brands, x, y, file, title, colours) {
brands <- unlist(brands) |> unname()
x <- unlist(x) |> unname()
y <- unlist(y) |> unname()
png(file, width = 600, height = 600)
x <- x - mean(x)
y <- y - mean(y)
# Rotate the plot so that the first point is at North
rotation_angle = -1 * atan2(y, x)[1]
x_rotated = x * cos(rotation_angle) - y * sin(rotation_angle)
y_rotated = x * sin(rotation_angle) + y * cos(rotation_angle)
y <- x_rotated
x <- y_rotated
# Flop the plot so that the second point is to the right
if (x[2] < x[1]) {
x <- -x
}
plot(x, y,
xlab = "Dimension 1", ylab = "Dimension 2", main = title,
pch = 19, col = colours,
xlim = c(min(x) - 0.1, max(x) + 0.1),
ylim = c(min(y) - 0.1, max(y) + 0.1))
text(x, y, brands, cex = 01, pos = 3, col = colours)
dev.off()
}
compare_similarity_matrices <- function(...) {
inputs <- list(...)
comparisons <- matrix(NA, nrow = length(inputs), ncol = length(inputs))
for (i in seq_along(inputs)) {
for (j in seq_along(inputs)) {
if (i > j) {
Di <- as.dist(1 - inputs[[i]])
Dj <- as.dist(1 - inputs[[j]])
comparisons[i, j] <- mantel.randtest(Di, Dj)$obs
}
}
}
comparisons
}
compare_similarity_matrices_by_most_similar_brand <- function(...) {
inputs <- list(...)
for (i in seq_along(inputs)) {
diag(inputs[[i]]) <- -Inf
}
comparisons <- matrix(NA, nrow = length(inputs), ncol = length(inputs))
for (i in seq_along(inputs)) {
for (j in seq_along(inputs)) {
if (i > j) {
Bi <- colnames(inputs[[i]])[apply(inputs[[i]], 1, which.max)]
Bj <- colnames(inputs[[j]])[apply(inputs[[j]], 1, which.max)]
comparisons[i, j] <- mean(Bi == Bj)
}
}
}
comparisons
}
nameDims <- function(M, names) {
colnames(M) <- names
rownames(M) <- names
M[-1, -ncol(M)]
}