New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable storing full variable names in attributes, and automatic use by likert function #7

Closed
wants to merge 6 commits into
base: master
from
Copy path View file
@@ -39,3 +39,6 @@ Collate:
'align.R'
'plot.histogram.R'
'reverse.levels.R'
'likert.add.fullnames.R'
'plot.likert.groups.R'
'likert.store.groups.R'
Copy path View file
@@ -4,11 +4,14 @@ S3method(print,likert.bar.plot)
S3method(summary,likert)
S3method(xtable,likert)
export(likert)
export(likert.store.groups)
export(plot.likert.groups)
export(likert.bar.plot)
export(likert.density.plot)
export(likert.heat.plot)
export(likert.histogram.plot)
export(recode)
export(likert.add.fullnames)
export(reverse.levels)
export(shinyLikert)
import(ggplot2)
Copy path View file
@@ -44,14 +44,21 @@ likert <- function(items,
stop('All items (columns) must have the same number of levels')
}

# pull in long names from attributes, if exist
for (n in names(items)) {
if (!is.null(attr(items[[n]], "fullname"))) {
names(items)[names(items) == n] <- attr(items[[n]], "fullname")
}
}

lowrange <- 1 : ceiling(nlevels / 2 - nlevels %% 2)
highrange <- ceiling(nlevels / 2 + 1 ) : nlevels

results <- data.frame()
if(!is.null(grouping)) {
results <- data.frame(
Group = rep(unique(grouping), each=nlevels),
Response = rep(1:nlevels, length(unique(grouping)))
Group = rep(unique(na.omit(grouping)), each=nlevels),
Response = rep(1:nlevels, length(unique(na.omit(grouping))))
)
for(i in 1:ncol(items)) {
t <- as.data.frame(table(grouping, as.integer(items[,i])))
Copy path View file
@@ -0,0 +1,20 @@
#'Takes a list of dataframe column names, and long names, and stores the long names in the attribute "fullname",
#'which can later be accessed by R.
#'
#'Example:
#'db <- add_likert_fullnames(db, c(
#' "X7"="Do you use sites like Facebook, Twitter, or GPlus?",
# "X8"="Do you participate in online communities organised around your interests?",
#' "X10"="Do you know of online communities relevant to your discipline or the courses you are taking now?"))

likert_add_fullnames <- function(to, fnames) {

if(length(fnames) > length(unique(fnames))) {
stop("All names must be unique")
}

for(x in names(fnames)) {
attr(to[[x]], "fullname") <- fnames[[x]]
}
to
}
Copy path View file
@@ -0,0 +1,17 @@
#'Stores metadata about groups into a data frame, for future use by plot_likert_group.
#'Example usage (X7 etc are column names, titles are used as titles for group plots)
#'
#'groups <- list(
#' "Use of social media"=c("X7","X8","X10","X11","X12"),
#' "Social learning"=c("X13","X14","X15","X16","X17","X18","X19","X20"),
#' "Connection with other students"=c("X21","X22","X23","X24","X25"),
#' "Interaction with professors"=c("X26","X27","X28","X29","X30","X31"),
#' "Experience of your major"='c("X32","X33","X34","X35","X36""),
#' "Identification with your discipline/major"=c("X40","X41","X42"))
#'
#' db <- likert_store_groups(db, groups)

likert_store_groups <- function(db, groups) {
attr(db, "likert.groups") <- groups
db
}
Copy path View file
@@ -0,0 +1,25 @@
#'Plots a group of Likert-items, as stored by likert_store_groups. groups= can be one group, indexed by title or by number
#'or a list of groups. all=T prints all groups.

plot_likert_groups <- function(db, all=F, groups=NA, ...) {
attrgroups <- attr(db, "likert.groups")

if(is.null(attrgroups)) {
stop("You have not stored any groups using likert_store_groups")
}

if(all) {
groups <- names(attrgroups)
}

if(all(is.na(groups))) {
stop("You have not specified a group name using groups=, or all=F")
}

for(e in groups) {
group <- attrgroups[[e]]
ligroup <- likert(db[,group], ...)
print(plot(ligroup) + ggtitle(names(attrgroups[e])) )
}

}
Copy path View file
@@ -0,0 +1,38 @@
options(digits=2)

require(likert)
data(pisaitems)

# adding metadata to pisaitems, including a reading group, and full names
# this will be stored together with the pisaitems object, and can be used later
attr(pisaitems, "reading") <- c(
"ST24Q01",
"ST24Q02",
"ST24Q03",
"ST24Q04",
"ST24Q05",
"ST24Q06",
"ST24Q07",
"ST24Q08",
"ST24Q09",
"ST24Q10",
"ST24Q11")

pisaitems <- add_likert_fullnames(pisaitems, c(
ST24Q01="I read only if I have to.",
ST24Q02="Reading is one of my favorite hobbies.",
ST24Q03="I like talking about books with other people.",
ST24Q04="I find it hard to finish books.",
ST24Q05="I feel happy if I receive a book as a present.",
ST24Q06="For me, reading is a waste of time.",
ST24Q07="I enjoy going to a bookstore or a library.",
ST24Q08="I read only to get information that I need.",
ST24Q09="I cannot sit still and read for more than a few minutes.",
ST24Q10="I like to express my opinions about books I have read.",
ST24Q11="I like to exchange books with my friends."))

# using the group reading to select the group of questions we want to plot
l24 <-pisaitems[,attr(pisaitems, "reading")]

# directly plotting, the full names have already been stored
plot(likert(l24))
ProTip! Use n and p to navigate between commits in a pull request.