{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# Quick tests of qdap lib on CoCalc\n",
"\n",
"Kernel: R (R-Project)\n",
"\n",
"https://cran.r-project.org/web/packages/qdap/"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: qdap\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: qdapDictionaries\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: qdapRegex\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: qdapTools\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: RColorBrewer\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"Attaching package: ‘qdap’\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"The following object is masked from ‘package:base’:\n",
"\n",
" Filter\n",
"\n"
]
}
],
"source": [
"require(qdap)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"Attaching package: ‘ggplot2’\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"The following object is masked from ‘package:qdapRegex’:\n",
"\n",
" %+%\n",
"\n"
]
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 2,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
}
],
"source": [
"with(rajSPLIT, gantt_plot(text.var = dialogue,\n",
"grouping.var = person, size=4))\n",
"with(rajSPLIT, gantt_plot(text.var = dialogue,\n",
"grouping.var = list(fam.aff, sex), rm.var = act,\n",
"title = \"Romeo and Juliet's dialogue\"))\n",
"with(rajSPLIT, gantt_plot(dialogue, list(fam.aff, sex), act,\n",
"transform=T))\n",
"rajSPLIT2 <- rajSPLIT\n",
"rajSPLIT2$newb <- as.factor(sample(LETTERS[1:2], nrow(rajSPLIT2),\n",
"replace=TRUE))\n",
"z <- with(rajSPLIT2, gantt_plot(dialogue, list(fam.aff, sex),\n",
"list(act, newb), size = 4))\n",
"library(ggplot2); library(scales); library(RColorBrewer); library(grid)\n",
"z + theme(panel.spacing = unit(1, \"lines\")) + scale_colour_grey()\n",
"z + scale_colour_brewer(palette=\"Dark2\")\n",
"## Fill Variable Example\n",
"dat <- rajSPLIT[rajSPLIT$act == 1, ]\n",
"dat$end_mark <- factor(end_mark(dat$dialogue))\n",
"with(dat, gantt_plot(text.var = dialogue, grouping.var = list(person, sex),\n",
"fill.var=end_mark))\n",
"## Repeated Measures with Fill Example\n",
"rajSPLIT$end_mark <- end_mark(rajSPLIT$dialogue)\n",
"with(rajSPLIT, gantt_plot(text.var = dialogue,\n",
"grouping.var = list(fam.aff), rm.var = list(act),\n",
"fill.var=end_mark, title = \"Romeo and Juliet's dialogue\"))\n",
"## Repeated Measures Sentence Type Example\n",
"with(rajSPLIT, gantt_plot(text.var = dialogue,\n",
"grouping.var = list(fam.aff, sex), rm.var = list(end_mark, act),\n",
"title = \"Romeo and Juliet's dialogue\"))\n",
"## Reset rajSPLIT\n",
"rajSPLIT <- qdap::rajSPLIT"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## tot_plot"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 3,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
}
],
"source": [
"dataframe <- sentSplit(DATA, \"state\")\n",
"tot_plot(dataframe, \"state\")\n",
"tot_plot(DATA, \"state\", tot=FALSE)\n",
"tot_plot(dataframe, \"state\", bar.space=.03)\n",
"tot_plot(dataframe, \"state\", \"sex\")\n",
"tot_plot(dataframe, \"state\", \"person\", tot = \"sex\")\n",
"tot_plot(mraja1, \"dialogue\", \"fam.aff\", tot=FALSE)\n",
"tot_plot(mraja1, \"dialogue\", \"died\", tot=FALSE)\n",
"tot_plot(mraja1, \"dialogue\", c(\"sex\", \"fam.aff\"), tot=FALSE) +\n",
"scale_fill_hue(l=40)\n",
"tot_plot(mraja1, \"dialogue\", c(\"sex\", \"fam.aff\"), tot=FALSE)+\n",
"scale_fill_brewer(palette=\"Spectral\")\n",
"tot_plot(mraja1, \"dialogue\", c(\"sex\", \"fam.aff\"), tot=FALSE)+\n",
"scale_fill_brewer(palette=\"Set1\")\n",
"## repeated measures\n",
"rajSPLIT2 <- do.call(rbind, lapply(split(rajSPLIT, rajSPLIT$act), head, 25))\n",
"tot_plot(rajSPLIT2, \"dialogue\", \"fam.aff\", facet.var = \"act\")\n",
"## add mean and +/- 2 sd\n",
"tot_plot(mraja1, \"dialogue\", grouping.var = c(\"sex\", \"fam.aff\"), tot=FALSE)+\n",
"scale_fill_brewer(palette=\"Set1\") +\n",
"geom_hline(aes(yintercept=mean(word.count))) +\n",
"geom_hline(aes(yintercept=mean(word.count) + (2 *sd(word.count)))) +\n",
"geom_hline(aes(yintercept=mean(word.count) + (3 *sd(word.count)))) +\n",
"geom_text(parse=TRUE, hjust=0, vjust=0, family=\"serif\", size = 4, aes(x = 2,\n",
"y = mean(word.count) + 2, label = \"bar(x)\")) +\n",
"geom_text(hjust=0, vjust=0, family=\"serif\", size = 4, aes(x = 1,\n",
"y = mean(word.count) + (2 *sd(word.count)) + 2, label = \"+2 sd\")) +\n",
"geom_text(hjust=0, vjust=0, family=\"serif\", size = 4, aes(x = 1,\n",
"y = mean(word.count) + (3 *sd(word.count)) + 2, label = \"+3 sd\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## word cloud"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 4,
"metadata": {
"image/svg+xml": {
"isolated": true
}
},
"output_type": "execute_result"
}
],
"source": [
"terms <- list(\n",
"I=c(\"i\", \"i'm\"),\n",
"mal=qcv(stinks, dumb, distrust),\n",
"articles=qcv(the, a, an),\n",
"pronoun=qcv(we, you)\n",
")\n",
"with(DATA, trans_cloud(state, person, target.words=terms,\n",
"cloud.colors=qcv(red, green, blue, black, gray65),\n",
"expand.target=FALSE, proportional=TRUE, legend=c(names(terms),\n",
"\"other\")))\n",
"with(DATA, trans_cloud(state, person, target.words=terms,\n",
"stopwords=exclude(with(DATA, unique(bag_o_words(state))),\n",
"unique(unlist(terms))),\n",
"cloud.colors=qcv(red, green, blue, black, gray65),\n",
"expand.target=FALSE, proportional=TRUE, legend=names(terms)))\n",
"#color the negated phrases opposite:\n",
"DATA <- qdap::DATA\n",
"DATA[1, 4] <- \"This is not good!\"\n",
"DATA[8, 4] <- \"I don't distrust you.\"\n",
"DATA$state <- space_fill(DATA$state, paste0(negation.words, \" \"),\n",
"rm.extra = FALSE)\n",
"txt <- gsub(\"~~\", \" \", breaker(DATA$state))\n",
"rev.neg <- sapply(negation.words, paste, negative.words)\n",
"rev.pos <- sapply(negation.words, paste, positive.words)\n",
"tw <- list(\n",
"positive=c(positive.words, rev.neg[rev.neg %in% txt]),\n",
"negative=c(negative.words, rev.pos[rev.pos %in% txt])\n",
")\n",
"with(DATA, trans_cloud(state, person,\n",
"target.words=tw,\n",
"cloud.colors=qcv(darkgreen, red, gray65),\n",
"expand.target=FALSE, proportional=TRUE, legend=names(tw)))\n",
"DATA <- qdap::DATA ## Reset DATA"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R (R-Project)",
"language": "r",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}