Tables - Descriptive Statistics

From Q
Jump to: navigation, search

Creates a table of descriptive statistics (i.e., the mean, N, number of missing values, standard deviation, range, sum, minimum, 25th percentile, median, 75th percentile, and maximum).

Creates a table of descriptive statistics (i.e., the mean, N, number of missing values, standard deviation, range, sum, minimum, 25th percentile, median, 75th percentile, and maximum).

How to Create a Table of Descriptive Statistics

  1. Add the object:
    1. In Displayr: Insert > More > Tables > Descriptive Statistics
    2. In Q: Create > Tables > Descriptive Statistics
  2. In Inputs > Variables, specify the variables you wish to see in the rows of the table.

Example

The table below includes the descriptive statistics for the variables Q2. Gender, Q3. Age, RD34 070622, Q1. Coke, Q1. Diet Coke, and Q4. Frequency.

Options

The options in the Object Inspector are organized into two tabs: Inputs and Properties.

Inputs

Variables The variables to appear in the rows, as categories.

Variable names Displays Variable Names in the output.

Filter The data is automatically filtered using any filters.

Weight The data is automatically weighted filtered using any weights.

Properties

This tab contains options for formatting the size of the object, as well as the underlying R code used to create the visualization, and the JavaScript code use to customize the Object Inspector itself (see Object Inspector for more details about these options). Additional options are available by editing the code.

More Information

Code

var displayr = Q.isOnTheWeb();
var variables_prompt = displayr ? "Variables are shown in the 'Data' tree (bottom-left)." :
       "Variables are shown in the 'Variables and Questions' tab.";

form.setHeading('Descriptive Statistics');
form.dropBox({label: "Variables", 
            types:["Variable: Numeric, Date, Money, Categorical, OrderedCategorical"], 
            name: "formVariables",
            multi:true, prompt: variables_prompt})
form.checkBox({label: "Variable names", name: "formNames", default_value: false, prompt: "Display names instead of labels"})
library(flipFormat)
library(flipTransformations)
raw.data = ProcessQVariables(data.frame(if (length(formVariables) == 1) formVariables[[1]] else formVariables))
names(raw.data) <- if(!formNames) lapply(formVariables, Labels) else lapply(formVariables, flipFormat::Names)
raw.data = AsNumeric(raw.data[QFilter,, drop = FALSE], binary = FALSE)
summarize <- function(x)
{
    w = QPopulationWeight[QFilter]
    n <- length(x)
    if (is.null(w))
        w <- rep(1, n)
    if (length(w) != n)
        stop("The weight is from a different data file to the variables.")
    w[is.na(w) | w < 0 | is.na(x)] <- 0
    x <- x[w > 0]
    w <- w[w > 0]
    N <- length(x)
    require(Hmisc)
    five = wtd.quantile(x, w, c(0, .25, .5, .75, 1))
    names(five)[c(1,3,5)] <- c("Min.", "Med.", "Max.")
    c("Ave." = ave <- wtd.mean(x, w), 
      N = N,
      "Miss." = n - N,
      "St.Dev." = sqrt(wtd.var(x, w)),
      "Range" = as.numeric(five[5] - five[1]),
      Sum = ave * sum(w),
      five)
}
summary <- t(sapply(raw.data, summarize))