Beautiful table outputs in R, part 2 #rstats #sjPlot

First of all, I’d like to thank my readers for the lots of feedback on my last post on beautiful outputs in R. I tried to consider all suggestions, updated the existing table-output-functions and added some new ones, which will be described in this post. The updated package is already available on CRAN.

This posting is divided in two major parts:

  1. the new functions are described, and
  2. the new features of all table-output-functions are introduced (including knitr-integration and office-import)

Read on …

No need for SPSS – beautiful output in R #rstats

Note: There’s a second part of this series here.

About one year ago, I seriously started migrating from SPSS to R. Though I’m still using SPSS (because I have to in some situations), I’m quite comfortable and happy with R now and learnt a lot in the past months. But since SPSS is still very wide spread in social sciences, I get asked every now and then, whether I really needed to learn R, because SPSS meets all my needs…

Well, learning R had at least two major benefits for me: 1.) I could improve my statistical knowledge a lot, simply by using formulas, asking why certain R commands do not automatically give the same results like SPSS, reading R resources and papers etc. and 2.) the possibilities of data visualization are way better in R than in SPSS (though SPSS can do well as well…). Of course, there are even many more reasons to use R.

Still, one thing I often miss in R is a beautiful output of simple statistics or maybe even advanced statistics. Not always as plot or graph, but neither as “cryptic” console output. I’d like to have a simple table view, just like the SPSS output window (though the SPSS output is not “beautiful”). That’s why I started writing functions that put the results of certain statistics in HTML tables. These tables can be saved to disk or, even better for quick inspection, shown in a web browser or viewer pane (like in RStudio viewer pane).

All of the following functions are available in my sjPlot-package on CRAN.

Read on …

Easily plotting grouped bars with ggplot #rstats

Summary
This tutorial shows how to create diagrams with grouped bar charts or dot plots with ggplot. The groups can also be displayed as facet grids.

Importing the data from SPSS
All following examples are based on an imported SPSS data set. Refer to this posting for more details on how to do that and to my script page to download the scripts. This is important to know because the way the variable and value labels are accessed may depend on whether you use an imported SPSS dataset or not (i.e. you may have to change parameters to get the sample running).

You can, for instance, import your SPSS data like this, if you are using my script:
source(“sjImportSPSS.R”)

efc <- importSPSS("GER_Services_FU_PV_dt.sav")
efc_vars <- getVariableLabels(efc)
efc_labels <- getValueLabels(efc)

The R script
You can download the script from my script page. I will not describe the code in detail because the source code is (hopefully) well commented. Basically, the script just transforms the data from two variables (one count variable with categories and one grouping variables) to fit into the ggplot-requirements for plotting bar charts. You can use a lot of parameters to change the style of the output, e.g. you can plot bars or dots, dodged or stacked bars, change colors etc. and you don’t need to know how this works in ggplot. You simply pass your “preferred settings” as parameters.

You can include the script via this single line:

source("sjPlotGroupFrequencies.R")

Continue reading this post…

Simplify frequency plots with ggplot in R #rstats

Update March 5th
All downloads are now accessible from my script page!

Summary
This posting shows how to plot frequency plots using the ggplot-package in R. Compared to SPSS standard outputs, you will learn how to create appealing diagrams ready for use in your papers.

Frequency plots in SPSS
In SPSS, you can create frequencies of variables by using this short script:

FREQUENCIES VARIABLES=c96cop15
  /ORDER=ANALYSIS.

which gives you following overview:

spss_freq_01

If you add another line to your syntax script, you can plot either bar charts (/BARCHARTS) or histograms (/HIST), too:

FREQUENCIES VARIABLES=c96cop15
  /ORDER=ANALYSIS.
  /BARCHART FREQ

which gives you following results:

spss_bar_01

spss_hist_01

It seems to be more effort creating graphs like the ones above in R, but actually it’s almost easier – and you even have more beautiful plots. The only preparation you need is a general function for plotting frequencies in R.

Continue reading this post…

Simplify your R workflow with functions #rstats

Update/ Thanks to Bernd I could improve the function of how to import the data, so here’s the updated script! /Update

In R, you often may have scripts or code snippets that will be reused. In such cases, you can write functions for your every-day-tasks. For instance, importing and converting data is such a task. I have written a small function importSPSS.R to do this:

importSPSS <- function(path, enc=NA) {
  # init foreign package
  require("foreign")
  # import data as data frame
  data.spss <- read.spss(path, to.data.frame=TRUE, use.value.labels=FALSE, reencode=enc)
  # return data frame
  return(data.spss)
}
getValueLabels <- function(dat) {
  a <- lapply(dat, FUN = getValLabels)
  return (a)
}
getValLabels <- function(x){
  rev(names(attr(x, "value.labels")))
}
getVariableLabels <- function(dat) {
  return(attr(dat, "variable.labels"))
}

This small function only gives little benefits regarding the saved typing effort. Referring to the code example under Migration, step 3: Importing (SPSS) variable and value labels, following things will change:

Continue reading this posting…

Migrating from SPSS to R #rstats

Preface
I will every now and then post my experience with R, a package for statistical analyses. I try to show some solutions for common types of analyses or problems you are facing when you start working with R. These “tutorials” especially address people who are used to work with SPSS or maybe also Strata.

Since I myself am new to R, my solutions probably are not the most elegant ones! Thus, any feedback is welcome!

Summary
This post just shows how to properly import SPSS data and get access to data values, variable and value labels. We need this basics for later tutorials where I focus on proper graphical output.

Why R?
I recently started using the statistical package R to do my statistical analyses at work. We all have SPSS licences at work, but still I was interested in testing R for some reasons:

  • It’s free and runs on Windows, Mac and Linux
  • The amount of different statistical analyses / modeling
  • The various possibilities of creating graphics (see, e.g., here, here or here)

Migration, step 1: Installation
First of all, R only provides a console for any input and output and has no GUI (graphical user interface). This is probably the most hindering reason for migrating from SPSS to R, because calculating cross tabs on the fly, for instance, is not as easy as in SPSS. So, the first step when you have downloaded R and want to use it is to download a nice editor for it, too.

I would recommend R Studio, because it’s also free, runs on Windows/Mac/Linux, it’s beautiful and supports much the work with R.

Continue reading this posting…