outline mixed models in r using the lme4 package part 2
play

Outline Mixed models in R using the lme4 package Part 2: Lattice - PowerPoint PPT Presentation

Outline Mixed models in R using the lme4 package Part 2: Lattice graphics Presenting data Scatter plots Douglas Bates Histograms and density plots University of Wisconsin - Madison and R Development Core Team


  1. Outline Mixed models in R using the lme4 package Part 2: Lattice graphics Presenting data Scatter plots Douglas Bates Histograms and density plots University of Wisconsin - Madison and R Development Core Team <Douglas.Bates@R-project.org> Box-and-whisker plots and dotplots Max Planck Institute for Ornithology Seewiesen July 21, 2009 Exploring and presenting data The formula/data method of specifying graphics ◮ When possible, use graphical presentations of data. Time ◮ The first two arguments to lattice graphics functions are spend creating informative graphical displays is well invested. usually formula and data . ◮ Ron Snee, a friend who spent his career as a statistical ◮ This specification is also used in model-fitting functions ( lm , consultant for DuPont, once said, “Whenever I am writing a aov , lmer , ...) and in other functions such as xtabs . report, the most important conclusion I want to communicate ◮ The formula incorporates a tilde, ( ∼ ), character. A one-sided is always presented as a graphic and shown early in the report. formula specifies the value on the x-axis. A two-sided formula On the other hand, if there is a conclusion I feel obligated to specifies the x and y axes. include but would prefer people not notice, I include it as a table.” ◮ The second argument, data , is usually the name of a data ◮ One of the strengths of R is its graphics capabilities. frame. ◮ Many optional arguments are available. Ones that we will use ◮ There are several styles of graphics in R. The style in frequently allow for labeling axes ( xlab , ylab ), and controlling Deepayan Sarkar’s lattice package is well-suited to the type of the type of information displayed, type . data we will be discussing. ◮ The lattice package is not attached by default. You must ◮ Deepayan’s book, Lattice: Multivariate Data Visualization enter library(lattice) before you can use lattice functions. with R (Springer, 2008) provides in-depth documentation and explanations of lattice graphics.

  2. A simple scatterplot in lattice Scatterplots in lattice ◮ A scatter plot is the most versatile plot in applied statistics. It > xyplot(optden ~ carb, Formaldehyde) is simply a plot of a numeric response, y , versus a numeric covariate, x . 0.8 ◮ The lattice function xyplot produces scatter plots. I typically ● specify type = c("g","p") requesting a background grid in addition to the plotted points. ● 0.6 ◮ The type argument takes a selection from ● optden ”p” points ● 0.4 ”g” background grid ”l” lines ● ”b” both points and lines 0.2 ”r” reference (or “regression”) straight line ”smooth” scatter-plot smoother lines ● ◮ In evaluating a scatterplot the aspect ratio (ratio of vertical 0.2 0.4 0.6 0.8 carb size to horizontal size) can be important. In particular, differences in slopes are most apparent near 45 o . General principles of lattice graphics An enhanced scatterplot of the Formaldehyde data ◮ The formula is of the form ∼ x or y ∼ x or y ∼ x | f where x is the variable on the x axis (usually continuous), y is the 0.8 variable on the y axis and f is a factor that determines the ● panels. ◮ Titles can be added with xlab , ylab , main and sub . Titles can ● 0.6 be character strings or, more generally, expressions that allow ● Optical density for special characters, subscripts, superscripts, etc. See ● help(plotmath) for details. 0.4 ◮ The groups argument, if used, specifies different point styles and different line styles for each level of the group. If lines are ● calculated, each group has separate lines. 0.2 ◮ If groups is used, we usually also use auto.key to add a key ● relating the line or point styles to the groups. 0.2 0.4 0.6 0.8 ◮ The layout specifies the number of columns and rows of Amount of carbohydrate (ml) panels.

  3. Saving plots Histograms and density plots ◮ A histogram is a type of bar plot created from dividing numeric data into adjacent bins (typically having equal width). ◮ The purpose of a histogram is to show the distribution or ◮ I recommend using the facilities in the R application to save density of the observations. It is almost never a good way of plots and transcripts. doing this. ◮ To save a plot, ensure that the graphics window is active and ◮ A densityplot is a better way of showing the density or, even use the menu item File → Save To Clipboard → Windows better, comparing the densities of observations associated Metafile . (On a Mac, save as PDF.) Then switch to a word with different groups. Also, densityplots for different groups processor and paste the figure. can be overlaid. ◮ Adjust the aspect ratio of the graphics window to suit the ◮ If you have only a few observations you may want to use a pasted version of the plot before you copy the graphic. comparative box-and-whisker plot ( bwplot ) or a comparative ◮ Those who want more control (and less cutting and pasting) dotplot instead. Density plots based on a small number of could consider the Sweave system or the odfWeave package. observations tend to be rather “lumpy”. ◮ If the data are bounded, perhaps because the data must be positive, a density plot can blur the boundary. However, this may indicate that the data are more meaningfully represented on another scale. Histogram of the InsectSprays data Density plot of the InsectSprays data > histogram(~count, InsectSprays) > densityplot(~count, InsectSprays) 0.06 30 0.05 25 0.04 20 Percent of Total Density 0.03 15 0.02 10 0.01 5 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0 0.00 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0 5 10 15 20 25 −10 0 10 20 30 count count

  4. Density plot of the square root of the count Density plot of the square root with fancy label > densityplot(~sqrt(count), InsectSprays, xlab = "Square root of > densityplot(~sqrt(count), InsectSprays, xlab = expression(sqrt("count"))) 0.25 0.25 0.20 0.20 0.15 0.15 Density Density 0.10 0.10 0.05 0.05 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.00 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.00 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0 2 4 6 0 2 4 6 count Square root of count Comparative density plot of square root Comparative density plot, separate panels > densityplot(~sqrt(count), InsectSprays, groups = spray, > densityplot(~sqrt(count) | spray, InsectSprays, layout = c(1, + auto.key = list(columns = 6)) + 6)) A B C D E F F 1.5 1.5 1.0 0.5 ● ● ● ● ● ● ● ● ● ● ● 0.0 E 1.5 1.0 0.5 0.0 ● ● ● ● ● ● ● ● ● ● ● ● D 1.0 1.5 1.0 0.5 Density Density ● ● ● ● ● ● ● ● ● ● ● ● 0.0 C 1.5 1.0 0.5 0.5 0.0 ● ● ● ● ● ● ● ● ● ● ● ● B 1.5 1.0 0.5 ● ● ● ● ● ● ● ● ● ● ● ● 0.0 A 1.5 1.0 ● ● 0.0 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.5 0.0 ● ● ● ● ● ● ● ● ● ● ● ● 0 2 4 6 0 2 4 6 count count

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend