background preloader

Articles / Tables

Facebook Twitter

Correctly Reporting P-Values in Summary Tables Reported with xtable | Bearded Analytics. Often when writing a manuscript in using knitr and xtable I am flustered by my p-values. In simple summary tables, R conveniently rounds my p-values to be 0: a mathematically inappropriate task. A colleague recently commented on the poor reporting of my table (shown below using print.xtable with the type="html" argument), inspiring a much needed change.

The fix is actually fairly straight forward, and can be summarized in a simple function: "fixp", with the code shown below: All that's going on: the function is pulling in the summary table (usually through a $coef), trying to turn it into a dataframe (some already are, though some tables are numeric (e.g. lm)), throwing a warning if the last heading doesn't begin with "Pr" (as it may not be the column that contains p-values), and editing any values that were rounded to 0 (at the user specified rounding point) to be < the smallest number that could be rounded to (e.g. <.01). Then we output the edited table, all ready for reporting! Making use of external R code in knitr and R markdown | Technical Tidbits From Spatial Analysis & Data Science. For scientific projects we often using knitr and R markdown in RStudio to share results and code either in HTML or PDF format.

These are great tools for reproducible research. We find, however, that keeping all of our R code in an R markdown (.Rmd) file creates two problems. First, you need to completely re-run the report to evaluate new bits of R code and secondly the .Rmd file can become unwieldy very quickly. Fortunately, there is an easy way to make use of external code. 1. Create your external R script You would create an R script as normal and, within the R script, you create ‘chunks’ using the ## @knitr syntax. X<-1:100 y<-x+rnorm(100) plot(x,y) 2. Now you can create your R markdown (.Rmd) file. ```{r echo=FALSE} read_chunk('junk.R') ``` ```{r first} <<variablesXY>> head(data.frame(x,y)) ``` ```{r second} <<plotXY>> ``` 3. ## @knitr variablesXY x<-1:100 y<-x+rnorm(100)head(data.frame(x,y)) ## @knitr plotXY plot(x,y) And then the myreport.Rmd can be simplified to look like: Table as an Image in R. Usually, it's best to keep tables as text, but if you're making a lot of graphics, it can be helpful to be able to create images of tables.

Creating the Table After loading the data, let's first use this trick to put line breaks between the levels of the effect variable. Depending on your data, you may or may not need or want to do this. library(OIdata) data(birds) library(gridExtra) # line breaks between words for levels of birds$effect: levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect)) Next let's make our table: xyTable <- table(birds$sky, birds$effect) Now we can create an empty plot, center our table in it, and use the grid.table function from the gridExtra package to display the table and choose a font size. plot.new() grid.table(xyTable, # change font sizes: gpar.coltext = gpar(cex = 1.2), gpar.rowtext = gpar(cex = 1.2)) Now you can view and save the image just like any other plot. The code is available in a gist. Citations and Further Reading. Highlight cells in markdown tables | rapporter. It has been over 2 months since the first satRday conference ended and I shared some quick impressions on that, where I also promised a follow-up post on my experiences as ending up being the main organizer of the event -- this is what you are reading now.

First of all, if you have the chance (and resources) to organize a satRday event, please do -- as it's great and extremely useful for the local community, and it's a very rewarding (although often also demanding) experience for yourself :) Do it only if you are passionate! As said it's fun to take an active part in the project, but it's a full-time job for at least a month. I've started the actual first steps (looking for venue, picking the date etc) at the beginning of June, and I thought it takes a few hours a week until September to get ready for the event. Actually, it became 2-3 hours a day fun project of my life for 3 months -- including weekends, which was sometimes pretty cumbersome besides doing a full-time job. Build a team. Reproducible reports & research with knitr in R Studio | Pairach Piboonrungroj.

Making interactive slides with Org mode and googleVis in R | Flowmentum. There’s been a lot of justifiable excitement in the R community about Yihui Xie’s great work, and most recently the incorporation of his knitr package into the RStudio software. Knitr is seen, justifiably, as a worthy successor to SWeave for dynamic, beautiful report generation. It is all that, but as an Org mode user, I already have something better than Sweave for both reproducible research and literate programming, which works with more than 30 different computer languages, not just R.

This is not to mention the astonishing amount of functionality that Org mode provides for any number of problems. I mean, really: it’s Emacs! (There are probably some great use cases for using knitr together with Org mode, but I haven’t come across any myself.) But then Markus Gesmann wrote a interesting blog post about using knitr and the googleVis package to produce interactive HTML presentations by converting the knit-produced markdown to Slidy, and I wanted to do the same in Org mode.

Function for Generating LaTeX Tables with Decimal Aligned Numbers. Categorized as: R, R-Bloggers, LaTeX, tables The xtable package is tremendously useful for generating LaTeX tables from data frames. It is also pretty easy to customize the output to handle some special cases of LaTeX formatting. The xtable.decimal function will create a LaTeX table where numeric columns will be vertically aligned on the decimal point.

In addition to specifying the LaTeX alignment code it will also create appropriate column titles so that the column name spans the two resulting columns. In the following example, we create a data frame with five columns, three of which are numeric that we want to display with aligned decimal places. The xtable.decimal function (source code below) has five parameters: x the data frame to convert. cols the columns to align. Here we will create the LaTeX table for the data frame created above. xtable.decimal(df, digits=3, colAlignment=c(Numbers='c'), caption.placement='bottom', caption='Test Data Frame') Link to xtable.decimal.r as a Gist.

Print glm-output to HTML table. We often use logistic regression models in our analyses and we also often need to publish the results as tables in our papers. And, we always use MS Word since this is the standard office application in our department. So I thought about an easy way of how to transfer the results of fitted (generalized) linear models from R to Word. An appropriate way – for me – is to create HTML tables, simply open them in Word and copy’n’paste them into my document.

This works much better than all things I have tried with SPSS tables (if someone has an easier solution, let me know!). I wrote two small functions called sjt.lm resp. sjt.glm, which are included in my sjPlot-R-package. First, compute two fitted models and create labels: Now, generate the tables: Default table style Table with p-values as numbers Table with separated column for CI Table with p-values as numbers and separated column for CI Gefällt mir: Gefällt mir Lade...

Creating HTML5 slides with RStudio, knitr and pandoc « Data Analysis Visually Enforced. Making Reproducible Research Enjoyable | Yihui Xie. Note: this is a contributed article for the ICSA Bulletin and the basic idea can be summarized in this picture. It is hard to convince people to think about reproducible research (RR).

There are two parts of difficulties: (1) tools used to be for experts only and (2) it is still common practice to copy and paste. For some statisticians, RR is almost equivalent to Sweave (R + LaTeX). I love LaTeX, but LaTeX is still hell to many people. But RR is really important. The basic idea was the same: to mix code and text together, then compile the whole document with code being executed, and you get a report without copying/pasting anything since the code will faithfully give you results. I use Github extensively and learned markdown there.

Later RStudio (the IDE of R) saw the work of knitr and decided to add support to it. After the Sweave work was done, I suggested markdown to RStudio developers, and fortunately they listened. There are many other features in knitr which make RR enjoyable. Knitr. \documentclass[nohyper,justified]{tufte-handout} %\documentclass{article} %great guides at epslatex.pdf %check miniplot for potential use %\usepackage{graphics} %\usepackage{caption} %\usepackage{sidecap} %\usepackage{textpos} %\usepackage[section]{placeins} \title{Performance Report from knitr} \author{Timely Portfolio} \begin{document} \maketitle \begin{abstract} We will pretend that HAM1 is real and investable with a marketing team that can raise billions of dollars.

\end{abstract} \SweaveOpts{concordance=TRUE} <<eval=TRUE,echo=FALSE,results='hide',warning=FALSE>>= #do requires and set up environment for reporting require(ggplot2) require(directlabels) require(reshape2) require(lattice) require(latticeExtra) require(xtable) require(dprint) require(quantmod) require(PerformanceAnalytics) #trying some new colors out mycolors=c(brewer.pal(9,"Blues")[c(7,5)],brewer.pal(9,"Greens")[6]) #mycolors=c(brewer.pal(6,"Blues)[c(3,5)],"slategray4") #function to get numbers in percent format #will use \\ to play well with tikz.