install.packages("pacman")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
library("pacman")
pacman::p_load(
tidyverse,
readr,
magrittr,
dplyr,
psych,
ggplot2,
zoo,
tidytext,
textdata,
wordcloud2,
SnowballC,
topicmodels,
stm,
ldatuning,
knitr,
LDAvis
)
BeAMAllData <- read_csv("Data/AllData01182024.csv")
## Rows: 708 Columns: 131
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): VisitsLevel, TimespanLevel, PRE_OtherCourses, PRE_Experience_2_10...
## dbl (121): PID, TrainingsCount, VisitsCount, CoursesTaken, Timespan, T_BeAM1...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Question for Bryant: how do you want to treat NA values in dataset? (i.e. replace with 0, remove records with NA, etc.)
Question for Bryant: has change been calculated for Self-Efficacy, Comms_Value, and/or Maker Excitement? Is that what the ‘gains’ columns are? If not, happy to do that in the block below.
For now, I’ll play around with descriptive statistics for the ‘Gains’ columns and get more information from you during our meeting.
summary(BeAMAllData$POST_Gains_1)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 2.0 4.0 4.0 4.2 5.0 5.0 678
Looks like a lot of missing values from POST_Gains_1 - is this true for all the Gains columns?
Creating a dataframe for tokenization of text and preparing for text mining. Using a single column as a test (PRE_Expectations).
stemmed_Expectations <- BeAMAllData %>%
unnest_tokens(output = word, input = PRE_Expectations) %>%
anti_join(stop_words, by = "word")
stemmed_Expectations <-
select(stemmed_Expectations, PID, VisitsLevel, word)
#remove entries that didn't respond to the PRE_Expectations question
MISSING <- is.na(stemmed_Expectations$word)
stemmed_Expectations <- subset(stemmed_Expectations,
subset = !MISSING)
temp <- textProcessor(stemmed_Expectations,
metadata = stemmed_Expectations,
lowercase=TRUE,
removestopwords=TRUE,
removenumbers=TRUE,
removepunctuation=TRUE,
wordLengths=c(3,Inf),
stem=FALSE,
onlycharacter= FALSE,
striphtml=TRUE,
customstopwords=NULL)
## Building corpus...
## Converting to Lower Case...
## Removing punctuation...
## Removing stopwords...
## Removing numbers...
## Warning in `[<-.data.frame`(`*tmp*`, , tag, value = structure(list(PID =
## c(730434826, : replacement element 1 has 476 rows to replace 3 rows
## Warning in `[<-.data.frame`(`*tmp*`, , tag, value = structure(list(PID =
## c(730434826, : replacement element 1 has 476 rows to replace 3 rows
## Warning in `[<-.data.frame`(`*tmp*`, , tag, value = structure(list(PID =
## c(730434826, : replacement element 1 has 476 rows to replace 3 rows
## Creating Output...
meta <- temp$meta
vocab <- temp$vocab
docs <- temp$documents
tokens_Expectations <- stemmed_Expectations |>
count(word, sort = TRUE)
wordcloud2(tokens_Expectations)
Doing some basic sentiment along Timespan and Visits Levels.
nrc <- get_sentiments("nrc")
token_sentiment_nrc <- inner_join(stemmed_Expectations, nrc, by = "word")
token_sentiment_nrc
## # A tibble: 393 × 4
## PID VisitsLevel word sentiment
## <dbl> <chr> <chr> <chr>
## 1 730434826 Many learn positive
## 2 730621371 <NA> learn positive
## 3 730621371 <NA> hobby joy
## 4 730621371 <NA> hobby positive
## 5 730325320 Many cool positive
## 6 730325320 Many lacking negative
## 7 730325320 Many learn positive
## 8 730387741 Many expect anticipation
## 9 730387741 Many expect positive
## 10 730387741 Many expect surprise
## # … with 383 more rows
summary_nrc_tokens <- token_sentiment_nrc %>%
filter(sentiment %in% c("positive", "negative")) %>%
group_by(VisitsLevel) %>%
count(sentiment, sort = TRUE) %>%
spread(sentiment, n) %>%
mutate(percent_pos = ((positive)/(positive + negative))*100) %>%
mutate(percent_neg = ((negative)/(positive + negative))*100)
summary_nrc_tokens
## # A tibble: 3 × 5
## # Groups: VisitsLevel [3]
## VisitsLevel negative positive percent_pos percent_neg
## <chr> <int> <int> <dbl> <dbl>
## 1 Few 1 14 93.3 6.67
## 2 Many 4 126 96.9 3.08
## 3 <NA> 1 30 96.8 3.23
Also wanted to try out some basic topic modeling.
#cast a document term matrix
Expectations_dtm <- stemmed_Expectations %>%
count(PID, word) %>%
cast_dtm(PID, word, n)
Expectations_dtm
## <<DocumentTermMatrix (documents: 79, terms: 207)>>
## Non-/sparse entries: 468/15885
## Sparsity : 97%
## Maximal term length: 16
## Weighting : term frequency (tf)
#fit it to an LDA model
Expectations_lda <- LDA(Expectations_dtm,
k = 15,
control = list(seed = 200)
)
Expectations_lda
## A LDA_VEM topic model with 15 topics.
#fit it to an STM model
Expectations_stm <- stm(documents=docs,
data=meta,
vocab=vocab,
K=15,
max.em.its=25,
verbose = FALSE)
Expectations_stm
## A topic model with 15 topics, 2 documents and a 209 word dictionary.
plot.STM(Expectations_stm, n = 5)