Описательная статистика R Петрова Ульяна Павловна
Использование R в описательной статистике и визуализации
Расширяем анализ subjects из предыдущего раздела: считаем лексическое разнообразие (TTR), топ-20 слов и топ-15 биграмм.
Код программы
library(tidyverse)
library(tidytext)
library(ggplot2)
# classics_subj уже загружен из предыдущего шага
tokens <- classics_subj %>%
select(bibliography.subjects) %>%
unnest_tokens(word, bibliography.subjects) %>%
anti_join(stop_words, by = "word") %>%
filter(!str_detect(word, "^[0-9]+$"))
# Type-Token Ratio
total_words <- nrow(tokens)
unique_words <- n_distinct(tokens$word)
tt_ratio <- unique_words / total_words * 100
cat("Всего слов:", total_words, "\n")
cat("Уникальных слов:", unique_words, "\n")
cat("Type-Token Ratio:", round(tt_ratio, 2), "%\n\n")
# Топ-20 слов
word_freq <- tokens %>% count(word, sort = TRUE)
print(word_freq %>% head(20))
p1 <- ggplot(word_freq %>% head(20), aes(x = reorder(word, n), y = n)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(title = "Топ-20 слов в жанровых тегах (subjects)",
x = "Слово", y = "Частота") +
theme_minimal()
print(p1)
# Топ-15 биграмм
bigrams <- classics_subj %>%
unnest_tokens(bigram, bibliography.subjects, token = "ngrams", n = 2) %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(!is.na(word1), !is.na(word2)) %>%
filter(!word1 %in% stop_words$word,
!word2 %in% stop_words$word) %>%
count(word1, word2, sort = TRUE)
print(bigrams %>% head(15))
p2 <- bigrams %>%
head(15) %>%
unite(bigram, word1, word2, sep = " ") %>%
ggplot(aes(x = reorder(bigram, n), y = n)) +
geom_col(fill = "coral") +
coord_flip() +
labs(title = "Топ-15 биграмм в subjects", x = "Биграмма", y = "Частота") +
theme_minimal()
print(p2)

Вывод программы
Всего слов: 8021 Уникальных слов: 1418 Type-Token Ratio: 17.68 % # A tibble: 20 × 2 word n <chr> <int> 1 fiction 1415 2 stories 212 3 drama 206 4 english 178 5 england 155 6 poetry 133 7 history 129 8 life 104 9 character 95 10 century 90 11 translations 87 12 social 84 13 fictitious 77 14 women 73 15 united 68 16 tales 65 17 biography 61 18 science 61 19 customs 53 20 american 48 # A tibble: 15 × 3 word1 word2 n <chr> <chr> <int> 1 england fiction 110 2 fictitious character 75 3 character fiction 67 4 social life 50 5 short stories 47 6 19th century 46 7 science fiction 39 8 women fiction 39 9 fiction england 38 10 juvenile fiction 38 11 psychological fiction 37 12 century fiction 36 13 domestic fiction 36 14 love stories 36 15 fiction love 35
Выводы расширенного анализа
Лексическое разнообразие тегов subjects низкое (TTR = 17.68%) — ожидаемо, так как subjects не свободный текст, а стандартизованные рубрики Library of Congress, и одни и те же слова повторяются из книги в книгу.
В топ-20 после ядра "fiction/stories/drama/english/england" видны вторичные темы: переводная литература (translations), социальный аспект (social, customs), пол персонажей (women), география (united/american vs england/ english) и science (как часть "science fiction").
Биграммы в основном — фрагменты составных рубрик: "fictitious character" и "19th century" — это части стандартных заголовков LCSH, а не свободные словосочетания. Из реальных поджанров видны short stories, love stories, science fiction, domestic fiction, psychological fiction, juvenile fiction.
