[英]How do you make a cumulative index based on 3 factor levels
正如標題已經暗示的那樣,我想根據以下 3 個級別計算(負)累積指數:
head(data$sentiment)
Levels: negative neutral positive
sentiment : Factor w/ 4 levels "","negative",..: 3 3 3 3 3
說 Negative 相當於 3,中性 2 和 1 是正面的。 分數越高,負數越多。 我打算制作一個從 0 到 100 的索引 - 100 是最負面的。 這些水平具有相同的權重,並且是特定日期的幾種情緒的累積。 最好的方法是什么?
一種選擇可能是:
(mean(data$sentiment, na.rm = TRUE) - 1) * 50
或者,如果您不想匯總所有值而只取當前擁有的值:
(data$sentiment - 1) * 50
這可確保您的新分數范圍為 0 到 100。
通常,您可能會尋找最小/最大標准化:
https://en.m.wikipedia.org/wiki/Feature_scaling#Rescaling_(min-max_normalization)
因此,在您的情況下,您可以從任何聚合開始,就像我建議的平均值或再次獲取原始值一樣。
x <- data$sentiment
new_x <- 0 + (x - min(x)) * (100 - 0) / (max(x) - min(x))
例子:
set.seed(1)
x <- sample(1:3, 20, replace = TRUE)
new_x <- 0 + (x - min(x)) * (100 - 0) / (max(x) - min(x))
x
[1] 1 3 1 2 1 3 3 2 2 3 3 1 1 1 2 2 2 2 3 1
new_x
[1] 0 100 0 50 0 100 100 50 50 100 100 0 0 0 50 50 50 50 100
[20] 0
為此,基礎 R 具有 function scale
。 如果x
是一個數值向量,列出從a
到b
的分數,並且你想要從 0 到M
的分數,那么你會這樣做:
scale(x, center = a, scale = (b - a) / M)
在您可以使用scale
之前,您需要將您的因素sentiment
強制轉換為列出等效分數的數字向量,如下所示:
set.seed(1L)
sentiment <- gl(4L, 1L, labels = c("", "negative", "neutral", "positive"))[sample(4L, size = 12L, replace = TRUE)]
sentiment
## [1] positive neutral negative
## [7] neutral neutral negative negative neutral neutral
## Levels: negative neutral positive
str(sentiment)
## Factor w/ 4 levels "","negative",..: 1 4 3 1 2 1 3 3 2 2 ...
scores <- c(NA, 3, 2, 1)[as.integer(sentiment)]
scores
## [1] NA 1 2 NA 3 NA 2 2 3 3 2 2
請注意,我們已為您的因素中出現的情緒""
分配了一個缺失值NA
。 現在你可以這樣做:
as.double(scale(scores, center = 1, scale = (3 - 1) / 100))
## [1] NA 0 50 NA 100 NA 50 50 100 100 50 50
在這里, as.double
僅用於將scale
的結果(1 列矩陣)強制轉換為向量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.