diff --git a/R/sentomeasures_methods.R b/R/sentomeasures_methods.R index 8243b99..f2b3c07 100644 --- a/R/sentomeasures_methods.R +++ b/R/sentomeasures_methods.R @@ -235,6 +235,7 @@ summary.sentomeasures <- function(object, ...) { cat("\n") cat("Aggregate statistics:", "\n") print(round(rowMeans(sentomeasures$stats), 5)) + NULL } #' @export diff --git a/README.md b/README.md index bf84276..90947e3 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ## sentometrics: An Integrated Framework for Textual Sentiment Time Series Aggregation and Prediction -[![Build Status](https://travis-ci.org/sborms/sentometrics.svg?branch=master)](https://travis-ci.org/sborms/sentometrics) [![CRAN](http://www.r-pkg.org/badges/version/sentometrics)](https://cran.r-project.org/package=sentometrics) +[![Build Status](https://travis-ci.org/sborms/sentometrics.svg?branch=master)](https://travis-ci.org/sborms/sentometrics) +[![codecov](https://codecov.io/github/sborms/sentometrics/branch/master/graphs/badge.svg)](https://codecov.io/github/sborms/sentometrics) [![Downloads](http://cranlogs.r-pkg.org/badges/sentometrics?color=brightgreen)](http://www.r-pkg.org/pkg/sentometrics) [![Downloads](http://cranlogs.r-pkg.org/badges/grand-total/sentometrics?color=brightgreen)](http://www.r-pkg.org/pkg/sentometrics) -[![codecov](https://codecov.io/github/sborms/sentometrics/branch/master/graphs/badge.svg)](https://codecov.io/github/sborms/sentometrics) diff --git a/tests/testthat/test_aggregation.R b/tests/testthat/test_aggregation.R index c52b11a..4ee656a 100644 --- a/tests/testthat/test_aggregation.R +++ b/tests/testthat/test_aggregation.R @@ -9,14 +9,20 @@ corpus <- quanteda::corpus_sample(sento_corpus(corpusdf = usnews), size = 1000) data("list_lexicons") lex <- list_lexicons[c("GI_en", "LM_en")] -ctr <- ctr_agg(howWithin = "tf-idf", howDocs = "proportional", howTime = "almon", by = "month", - lag = 3, ordersAlm = 1:3, do.inverseAlm = TRUE) - -sentMeas <- sento_measures(corpus, lex, ctr) ### tests from here ### +ctr1 <- ctr_agg(howWithin = "tf-idf", howDocs = "equal_weight", howTime = "almon", by = "month", + lag = 5, ordersAlm = 1:3, do.inverseAlm = TRUE) +sentMeas1 <- sento_measures(corpus, lex, ctr1) + +ctr2 <- ctr_agg(howWithin = "tf-idf", howDocs = "proportional", howTime = c("equal_weight", "linear", "own"), by = "year", + lag = 2, weights = data.frame(q1 = c(0.25, 0.75), q3 = c(0.75, 0.25))) +sentMeas2 <- sento_measures(corpus, lex, ctr2) + +# sento_measures test_that("Number of columns coincide with provided dimensions", { - expect_equal(nmeasures(sentMeas), length(sentMeas$features) * length(lex) * length(sentMeas$time)) + expect_equal(nmeasures(sentMeas1), length(sentMeas1$features) * length(sentMeas1$lexicons) * length(sentMeas1$time)) + expect_equal(nmeasures(sentMeas2), length(sentMeas2$features) * length(sentMeas2$lexicons) * length(sentMeas2$time)) }) diff --git a/tests/testthat/test_methods_sentomeasures.R b/tests/testthat/test_methods_sentomeasures.R index 8e2c143..e14f353 100644 --- a/tests/testthat/test_methods_sentomeasures.R +++ b/tests/testthat/test_methods_sentomeasures.R @@ -42,13 +42,19 @@ test_that("Scaling is properly done", { expect_equal(s1$stats["sd", ], s4$stats["sd", ]) }) -# plot +# summary.sentomeasures, print.sentomeasures +test_that("No output returned when object summarized or printed", { + expect_null(summary(sentMeas)) + expect_null(print(sentMeas)) +}) + +# plot.sentomeasures p <- plot(sentMeas, group = sample(c("features", "lexicons", "time"), 1)) test_that("Plot is a ggplot object", { expect_true(inherits(p, "ggplot")) }) -# get_measures (to_long) +# get_measures, to_long measuresLong <- get_measures(sentMeas, format = "long") test_that("Proper long formatting of sentiment measures", { expect_true(nrow(measuresLong) == nobs(sentMeas) * nmeasures(sentMeas)) diff --git a/tests/testthat/test_model_specification.R b/tests/testthat/test_model_specification.R index 99c9bbf..8a102cf 100644 --- a/tests/testthat/test_model_specification.R +++ b/tests/testthat/test_model_specification.R @@ -57,6 +57,7 @@ out8 <- sento_model(sentomeasures, y, x = x, ctr = ctrM8) ctrM9 <- ctrM8 ctrM9$nSample <- N - 1 - 2 + 1 +# sento_model test_that("Different model specifications give specified output", { expect_equal(N - 8, nrow(out1$x)) expect_equal(N - 1, nrow(out2$x)) @@ -73,5 +74,27 @@ test_that("Different model specifications give specified output", { expect_true(all(sapply(c(list(out1, out2, out3, out4, out5, out7), out8$models), function(out) stats::coef(out$reg)[c("x1", "x2"), ]) != 0)) expect_error(sento_model(sentomeasures, y, x = x, ctr = ctrM9)) + expect_null(summary(out1)) + expect_null(summary(out5)) + expect_null(summary(out6)) + expect_null(summary(out8)) +}) + +# summary.sentomodel, summary.sentomodeliter, print.sentomodel, print.sentomodeliter +test_that("No output returned when object summarized or printed", { + expect_null(summary(out1)) + expect_null(summary(out5)) + expect_null(summary(out6)) + expect_null(summary(out8)) + expect_null(print(out1)) + expect_null(print(out5)) + expect_null(print(out6)) + expect_null(print(out8)) +}) + +# plot.sentomodeliter +p <- plot(out8) +test_that("Plot is a ggplot object", { + expect_true(inherits(p, "ggplot")) }) diff --git a/tests/testthat/test_sentiment_computation.R b/tests/testthat/test_sentiment_computation.R index 6016b1a..00ca80b 100644 --- a/tests/testthat/test_sentiment_computation.R +++ b/tests/testthat/test_sentiment_computation.R @@ -10,12 +10,12 @@ data("usnews") corpus <- sento_corpus(corpusdf = usnews[1:250, ]) data("list_lexicons") -lex <- setup_lexicons(list_lexicons[c("GI_en", "LM_en", "HENRY_en")], - list_valence_shifters[["en"]]) +lex <- setup_lexicons(list_lexicons[c("GI_en", "LM_en", "HENRY_en")], list_valence_shifters[["en"]]) +lexSplit <- setup_lexicons(list_lexicons[c("GI_en", "LM_en", "HENRY_en")], list_valence_shifters[["en"]], do.split = TRUE) ### tests from here ### -sentiment <- list( +sentimentList <- list( s1 = compute_sentiment(quanteda::texts(corpus), lex, how = "counts"), s2 = compute_sentiment(quanteda::texts(corpus), lex, how = "tf-idf"), s3 = compute_sentiment(quanteda::texts(corpus), lex, how = "proportional"), @@ -23,15 +23,18 @@ sentiment <- list( s5 = compute_sentiment(quanteda::corpus(usnews[1:250, "texts"]), lex, how = "counts"), s6 = compute_sentiment(quanteda::corpus(usnews[1:250, c("texts", "wsj", "economy")], text_field = "texts"), lex, how = "counts"), - s7 = compute_sentiment(corpus, lex, how = "counts") + s7 = compute_sentiment(corpus, lex, how = "counts"), + s8 = compute_sentiment(quanteda::texts(corpus), lexSplit, how = "counts") ) test_that("Agreement between sentiment scores across input objects", { - expect_true(all(unlist(lapply(sentiment, function(s) nrow(s[["sentiment"]]) == 250)))) - expect_true(all(unlist(lapply(sentiment, function(s) all(s[["sentiment"]][["word_count"]] - == sentiment$s1$sentiment$word_count))))) - expect_equivalent(sentiment$s1$sentiment[, c("GI_en", "LM_en", "HENRY_en")], - sentiment$s5$sentiment[, c("GI_en", "LM_en", "HENRY_en")]) - expect_equivalent(sentiment$s6$sentiment[, -c(1:2)], - sentiment$s7$sentiment[, colnames(sentiment$s6$sentiment)[-c(1:2)], with = FALSE]) + expect_true(all(unlist(lapply(sentimentList, function(s) nrow(s$sentiment) == 250)))) + expect_true(all(unlist(lapply(sentimentList, function(s) all(s$sentiment$word_count + == sentimentList$s1$sentiment$word_count))))) + expect_equivalent(sentimentList$s1$sentiment[, c("GI_en", "LM_en", "HENRY_en")], + sentimentList$s5$sentiment[, c("GI_en", "LM_en", "HENRY_en")]) + expect_equivalent(sentimentList$s6$sentiment[, -c(1:2)], + sentimentList$s7$sentiment[, colnames(sentimentList$s6$sentiment)[-c(1:2)], with = FALSE]) + expect_true(all(sentimentList$s8$sentiment[, c("GI_en_POS", "LM_en_POS", "HENRY_en_POS")] >= 0)) + expect_true(all(sentimentList$s8$sentiment[, c("GI_en_NEG", "LM_en_NEG", "HENRY_en_NEG")] <= 0)) })