Skip to content

Overview of Analysis in R

Mark edited this page Oct 12, 2017 · 23 revisions

Purpose

This page defines my data analysis goals and provides guidelines for how these are are implemented, specifically in R-Studio. It is a description of file structure of data and analysis scripts, as well as my workflow in R-Studio. For a light intro, see Guide to R/R-Studio.

File Structure

Folders
_original_data - .csv, .xlsx, .emerge files for visual inspection and r.
archive_content - Historical r scripts, coding, and data structures.

Files
.Rproj - Open this first.
name_analysis.r - Open this within R-Studio (within the project environment).
name_data.rda - data file to be used with the analysis.R file.

name_data_structures.R - file used to generate cleaned data structures (.rda files). Dependents: .csv files in _original_data

Analysis

  1. Open (double-click) the .Rproj file. If it opens in R-Studio, it should set the working directory to the current folder.
  2. In R-Studio, navigate to the File tab on the bottom-right panel (default position). Open name_analysis.r from here.
  3. Run the code from this .r script through the library-loading, and data structure loading commands. Something like this:
library("ddply")
library("lmSupport")
# Here are some notes about what this script does.
load("datastructure.rda")
  1. Run lines of analyses.

name_analysis.r

The main analysis script is meant to accomplish two tasks: statistical analysis and graph generation. The script starts with the read in of globally cleaned data, that is, trial exclusions that should apply accross the board, and column recodings that might be generally useful, as well as restructuring of full data sets (e.g., long vs wide format). You can expect three types of code sections here.

  1. local recoding or data reshaping for statistics or graphs.
# Let's look at a summary by-subject, of the full data set.
subject_summary = d %>%
    ddply(.(subject), summarize, average_accuracy = mean(accuracy)) %>%
    mutate(pass_fail = ifelse(average_accuracy > 50, "Pass", "Fail"))
  1. analysis for statistics.
subject_performance = lmer( average_accuracy ~ 1 + (1|subject), data = subject_summary) %>%
     summary()
  1. analysis for graphs.
subject_performance = lmer( average_accuracy ~ 1 + (1|subject), data = subject_summary) %>%
    summary() %>%
    propLaterTable(1,100)
  1. graphs.
# likely you'll see some lines combining summary statistics into a single table:
for_graph_1 = rbind(model_1, model_2, model_3, model_4)

# and a line that labels the new table for the graph.
for_graph_1$categories = c("Category 1","Category 2","Category 1","Category 2")

# Then code for the graph.
graph_1 = ggplot(data_set, aes(x= x_variable, y = y_variable, fill = grouping_variable)) +
    geom_bar(stat="identity", position = "dodge")
# etc.

Clone this wiki locally