Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You...

69
Graphics Why plot data? 1) Plotting your data should usually be one of the first items done after you obtain data in order to Look for trends Discover unusual observations (outliers) Suggest items to examine in a more sophisticated statistical analysis 2) During a sophisticated statistical analysis, plots may be helpful to lead to particular conclusions (e.g., residual plots in regression analysis) 3) Once the sophisticated statistical analysis is complete, one should use plots to help explain the results to yourself and to others. This is especially helpful for statisticians consulting with subject- matter researchers. We will focus on 1) now and 3) will be useful after this sub-section 3.1

Transcript of Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You...

Page 1: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Graphics

Why plot data?

1)Plotting your data should usually be one of the first items done after you obtain data in order to Look for trends Discover unusual observations (outliers) Suggest items to examine in a more sophisticated

statistical analysis 2)During a sophisticated statistical analysis, plots may be

helpful to lead to particular conclusions (e.g., residual plots in regression analysis)

3)Once the sophisticated statistical analysis is complete, one should use plots to help explain the results to yourself and to others. This is especially helpful for statisticians consulting with subject-matter researchers.

We will focus on 1) now and 3) will be useful after this sub-section

Notes for this sub-section: You will notice a lot of code is sometimes needed to

construct plots! Please think of my code as a template. You can modify the template for your own data sets.

I will primarily use the cereal data to illustrate plotting. This is because the data set is small (helpful for the first time you see a plot) and using the same data set allows you to make comparisons across the different types of plots.

3.1

Page 2: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Two-dimensional plots

Scatter plot! See example in the Introduction to R notes.

When there are more than one two variables, side-by-side scatter plots (a.k.a., scatter plot matrix) may be of interest.

Example: Cereal data (cereal_graphics.r, cereal_data.xls)

The pairs() function creates side-by-side scatter plots:

> pairs(formula = ~ sugar + fat + sodium, data = cereal)

sugar

0.00 0.02 0.04 0.06 0.08

0.0

0.1

0.2

0.3

0.4

0.5

0.00

0.02

0.04

0.06

0.08

fat

0.0 0.1 0.2 0.3 0.4 0.5 0 2 4 6 8 10

02

46

810

sodium

3.2

Page 3: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> cor(cereal[,8:10]) sugar fat sodiumsugar 1.0000000 0.2397225 -0.1635699fat 0.2397225 1.0000000 -0.0661432sodium -0.1635699 -0.0661432 1.0000000

Examine the plot with respect to the estimated correlation matrix.

I have included another version of this plot in the program where the plotting point corresponds to shelf.

Alternatively, one can use the scatterplotMatrix() function in R’s car package > library(car)> scatterplotMatrix(formula = ~ sugar + fat + sodium, data = cereal, reg.line = lm, smooth = TRUE, span = 0.5, diagonal = "histogram")

3.3

Page 4: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

x

Freq

uenc

y

sugar

0.00 0.02 0.04 0.06 0.08

0.0

0.1

0.2

0.3

0.4

0.5

0.00

0.02

0.04

0.06

0.08

x

Freq

uenc

y

fat

0.0 0.1 0.2 0.3 0.4 0.5 0 2 4 6 8 10

02

46

810

x

Freq

uenc

y

sodium

3.4

Page 5: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Three-dimensional plots

The purpose is to incorporate three separate variables into one plot.

Scatter plot

Two variables can be plotted against each other where a characteristic of a plotting point corresponds to a qualitative variable.

Example: Cereal data (cereal_graphics.r, cereal_data.xls)

The plot() function can create a simple scatter plot:

> plot(x = cereal$sugar, y = cereal$fat, xlab = "Sugar", ylab = "Fat", main = "Fat vs. Sugar \n Each variable is adjusted for the serving size", panel.first = grid(col = "lightgray", lty = "dotted"))

3.5

Page 6: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

0.0 0.1 0.2 0.3 0.4 0.5

0.00

0.02

0.04

0.06

0.08

Fat vs. Sugar Each variable is adjusted for the serving size

Sugar

Fat

Do you see any trends or unusual observations?

Now, the shelf of the cereal is incorporated as a characteristic of a plotting point. I do this three ways (color, size, symbol), but one or two ways are typically only needed in application:

> plot(x = cereal$sugar, y = cereal$fat, xlab = "Sugar", ylab = "Fat", main = "Fat vs. Sugar \n Each variable is adjusted for the serving size", panel.first = grid(col = "lightgray", lty = "dotted"), type = "n")

3.6

Page 7: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> shelf.color<-rep(x = c("black", "red", "darkgreen", "blue"), each = 10)> shelf.symbol<-rep(x = 1:4, each = 10)> #shelf.color> #shelf.symbol> points(x = cereal$sugar, y = cereal$fat, pch = shelf.symbol, col = shelf.color, cex = 1 + 0.3*shelf.symbol)> legend(x = 0.5, y = 0.08, title = "Shelf", legend = 1:4, bty = "n", col = c("black", "red", "darkgreen", "blue"), pch = 1:4, pt.cex = c(1.3, 1.6, 1.9, 2.2))> #If the legend labels were not numbers, just use something like c("1", "2", "3", "4") where the numbers are replaced with actual names

0.0 0.1 0.2 0.3 0.4 0.5

0.00

0.02

0.04

0.06

0.08

Fat vs. Sugar Each variable is adjusted for the serving size

Sugar

Fat

Shelf1234

3.7

Page 8: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Of course, a legend is needed for these types of plots!

Do you see any trends or unusual observations?

The previous code took advantage of the data being sorted by shelf number. What if the data was not already sorted like this? > #"Unsort" the data > set.seed(9012)> new.order<-sample(x = 1:40, size = nrow(cereal), replace = FALSE)> cereal2<-cereal[new.order,] > options(width = 70) #Helps format for my notes> head(cereal2)

ID Shelf Cereal size_g10 10 1 Food Club Crispy Rice 3324 24 3 Kellogg's Corn Pops 3129 29 3 Post Raisin Bran 5918 18 2 Food Club Toasted Oats 3319 19 2 Cocoa Pebbles 2939 39 4 Post Fruit and Fibre - Dates, Raisons, Walnuts 55 sugar_g fat_g sodium_mg sugar fat sodium10 2 0.0 330 0.06060606 0.00000000 10.00000024 14 0.0 120 0.45161290 0.00000000 3.87096829 20 1.0 300 0.33898305 0.01694915 5.08474618 10 1.5 150 0.30303030 0.04545455 4.54545519 13 1.0 160 0.44827586 0.03448276 5.51724139 17 3.0 280 0.30909091 0.05454545 5.090909

> #Sorting by shelf> cereal3<-cereal2[order(cereal2$Shelf),]> head(cereal3[,c(1:3,8:10)])

ID Shelf Cereal sugar fat10 10 1 Food Club Crispy Rice 0.06060606 0.000000003 3 1 Kellog's Corn Flakes 0.07142857 0.000000008 8 1 Capn Crunch's Peanut Butter Crunch 0.33333333 0.092592599 9 1 Post Honeycomb 0.37931034 0.017241384 4 1 Food Club Toasted Oats 0.06250000 0.062500006 6 1 Food Club Frosted Flakes 0.35483871 0.00000000

sodium10 10.000000

3.8

Chris2, 08/22/13,
Examine shelf #2 with respect to sugar
Page 9: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

3 10.7142868 7.4074079 7.5862074 8.7500006 5.806452

> tail(cereal3[, c(1:3,8:10)]) ID Shelf Cereal sugar fat sodium32 32 4 Food Club Wheat Crunch 0.1000000 0.00000000 5.00000035 35 4 Cookie Crisp 0.4000000 0.03333333 6.00000033 33 4 Oatmeal Crisp Raisin 0.3454545 0.03636364 4.00000034 34 4 Food Club Bran Flakes 0.1612903 0.01612903 7.09677431 31 4 Total Raisin Bran 0.3454545 0.01818182 4.36363637 37 4 Food Club Low Fat Granola 0.2545455 0.05454545 1.818182

Bubble Plot

This is a scatter plot of two variables with the size of the plotting character proportional to a third variable. The third variable’s plotting character is usually a circle (bubble).

Example: Cereal data (cereal_graphics.r, cereal_data.xls)

Bubble plots are created with the symbols() function. Below is a bubble plot for the adjusted data.

> symbols(x = cereal$sugar, y = cereal$fat, circles = cereal$sodium, xlab = "Sugar", ylab = "Fat", main = "Fat vs. Sugar with symbol proportional to sodium \n Each variable is adjusted for the serving size", panel.first = grid(col = "lightgray", lty = "dotted"))

3.9

Page 10: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

0.0 0.1 0.2 0.3 0.4 0.5 0.6

0.00

0.02

0.04

0.06

0.08

0.10

Fat vs. Sugar with symbol proportional to sodium Each variable is adjusted for the serving size

Sugar

Fat

The plot is difficult to interpret due to the size of some points. This is an example of where two additions can be helpful to the code to better interpret the plot:1)Control the maximum size of the plotting symbol with

the inches argument; the default is set to a value of 12)Rescale the plotting symbol by using a transformation

of the argument value for circles

I added inches = 0.5 to the symbols() function code:

3.10

Chris2, 08/28/13,
Function's documentation says this is the "dimension". It does not appear to be area, radius, or diameter though
Page 11: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

0.0 0.1 0.2 0.3 0.4 0.5 0.6

0.00

0.02

0.04

0.06

0.08

0.10

Fat vs. Sugar with symbol proportional to sodium Each variable is adjusted for the serving size

Sugar

Fat

Do you see any trends in the data?

Notice the two points in the lower left side of the plot. We can identify these and any other observations on the plot by using the identify() function:

identify(x = cereal$sugar, y = cereal$fat)

3.11

Chris2, 08/22/13,
Difficult to see any trends. IT IS VERY important to notice the two very small points in the lower left side. These correspond to sodium = 0.
Page 12: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

0.0 0.1 0.2 0.3 0.4 0.5 0.6

0.00

0.02

0.04

0.06

0.08

0.10

Fat vs. Sugar with symbol proportional to sodium Each variable is adjusted for the serving size

Sugar

Fat

26

30

These points corresponds to observations #26 and #30:

> cereal[c(26,30),c(1:3,8:10)] ID Shelf Cereal sugar fat sodium26 26 3 Post Shredded Wheat Spoon Size 0.00 0.01020408 030 30 3 Food Club Frosted Shreded Wheat 0.02 0.02000000 0

They have no sodium! Fortunately, R still plots a point for these observations (SAS would not ). Overall, these points appear to be unusual in comparison to the rest due to their sodium AND also for their somewhat extreme fat and sugar values.

3.12

Page 13: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Question: What would happen if there were both positive and negative values for the circle argument? For example, suppose we did this plot with the standardized data values.

Alternatively, we can automatically label all of the points by also using the text() function after symbols():

> text(x = cereal$sugar, y = cereal$fat, labels = round(cereal$sodium,2))

0.0 0.1 0.2 0.3 0.4 0.5 0.6

0.00

0.02

0.04

0.06

0.08

0.10

Fat vs. Sugar with symbol proportional to sodium Each variable is adjusted for the serving size

Sugar

Fat

6.079.6410.71

8.75

7

5.81

7.41

7.41

7.59

10

6.33

1.85

4.69

7.41

7.67

3.5

5.67

4.55

5.52

5.56

5.19

6.33

9.35 3.87

2.96

0

5.82

6

5.080 4.36

5

4

7.1

62.1

1.82

4.73

5.09

6.67

3.13

Page 14: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Below is what happens to the original plot when inches = 0.5 is added and the circles argument is changed to sqrt(cereal$sodium) in the symbols() function code:

0.0 0.1 0.2 0.3 0.4 0.5 0.6

0.00

0.02

0.04

0.06

0.08

0.10

Fat vs. Sugar with symbol proportional to sodium Each variable is adjusted for the serving size

Sugar

Fat

Why are the points generally more similar in size than in the last plot?

Overall, rescaling the values in the circles argument is most useful when there are only a few big circles in

3.14

Page 15: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

comparison to the rest. Try this on your own with a data set that you create! Also, the rescaling can be done with some other mathematical function too.

Question: How could you add shelf type to this plot?

In the last example, there were not many trends found. Below are examples of other types of trends:

Trend among all three variables:

No trend between x and y, but a trend exists with the third variable:

3.15

Chris2, 08/26/13,
The col argument does not work with symbols(). Instead, use fg.
Page 16: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Bubble plots are also useful for regression model diagnostic plots.

Below is an example plot from my STAT 875 class based on the placekicking data set described previously. The red line is an estimated logistic regression model relating the distance of a placekick to its probability of success. The plotting points are drawn at the proportion of successes for a given distance, where the plotting point is proportional to the number of trials at a particular distance.

DistanceFailure

s SuccessesObserved proportion

18 1 2 2/3 = 19 0 7 7/7 = 1.020 13 776 776/78921 1 19 19/20

3.16

Page 17: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

22 2 12 12/14

55 1 2 2/356 0 1 1/159 0 1 1/162 1 0 0/163 1 0 0/166 1 0 0/1

20 30 40 50 60 70

0.0

0.2

0.4

0.6

0.8

1.0

Distance (yards)

Est

imat

ed p

roba

bilit

y3.17

Page 18: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

3D Scatter Plot

The plot3d() function from the rgl package provides one nice way to do these plots in R. There are other packages that produce these types of plots as well, but not as well as this package.

Example: Cereal data (cereal_graphics.r, cereal_data.xls)

Similar to what we saw with the previous rgl package examples, these plots are rotatable. Below is an example plot.

> library(rgl)> plot3d(x = cereal$sugar, y = cereal$fat, z = cereal$sodium, xlab = "Sugar", ylab = "Fat", zlab = "Sodium", col = "red", size = 6) #Default size is 3 (see help file)> grid3d(side = c("x", "y", "z"), col = "lightgray") #Use "+" and "-" after "x" to change location of grid

3.18

Page 19: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

It is best to rotate the plot to view it at a few different angles in order to decide on trends and/or unusual observations.

Below is another version of the plot where “needles” are used to help see the plotting points:

> plot3d(x = cereal$sugar, y = cereal$fat, z = cereal$sodium, xlab = "Sugar", ylab = "Fat", zlab = "Sodium", type = "h")> plot3d(x = cereal$sugar, y = cereal$fat, z = cereal$sodium, add = TRUE, col = "red", size = 6)> grid3d(side = c("x", "y", "z"), col = "lightgray")

3.19

Page 20: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

How could you construct these types of plots when each plotting point corresponds to shelf? This would essentially add a fourth dimension to the plot.

I show how to produce 3D scatter plots with three other packages in the corresponding program.

3.20

Page 21: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Plots of Higher Dimensional Data

Previous examples were given for how to add a few additional dimensions to plots. The plots to be discussed next can be used to plot many more dimensions.

Chernoff Faces

This plot is briefly discussed here more for historical purposes. The star plots to be discussed shortly are better to represent multivariate data in this type of format.

Faces are used to represent multidimensional data. Each face is for a particular experimental unit Face characteristics (eyes, hair, mouth,…) are scaled

to correspond to a variable.

Below is an example plot for the cereal data set:

3.21

Chris2, 08/26/13,
Chris Malone example of examining grad schools
Page 22: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35

36 37 38 39 40

Star Plots

This is how these types of plots are constructed Each star (or sun) represents a particular experimental

unit. The center of the star denotes the minimum value for

each variable. For each variable, a line or “ray” extends out from the

center at a length corresponding to the variable value.

3.22

Page 23: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

These plots are useful to Help detect outliers – stars that are very different from

the others indicate possible outliers Validate cluster analysis results – observations grouped

within the same cluster should have similar stars

Example: Cereal data (cereal_graphics.r, cereal_data.xls)> stars(x = cereal[,8:10], nrow = 4, ncol = 10, draw.segments = FALSE, key.loc = c(3,12))

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

sugar

fat

sodium

> stars(x = cereal[,8:10], nrow = 4, ncol = 10, draw.segments = TRUE, key.loc = c(3,12))

3.23

Page 24: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

sugar

fat

sodium

Comments: Cereals 26 and 30 (Post Shredded Wheat Spoon Size

and Food Club Frosted Shredded Wheat) appear to be low in sugar, fat, and sodium (relative to the other cereals).

Cereals appear to be high in fat. Cereals appear to be high in sugar. Cereals appear to be high in sodium. The researcher’s hypothesis before the data collection

was that shelf 1 and 2 tend to have the higher sugar content cereals. From this plot, what do you think?

Compare what you see in this plot with the previous plots.

3.24

Chris Bilder, 08/22/13,
2=Post Toasties Corn Flakes, 3=Kellogg’s Corn Flakes, 10=Food Club Crispy Rice, 23 Rice Chex
Chris Bilder, 08/22/13,
12=Kellogg’s Smacks, 16=Marshmallow Blasted Froot Loops
Chris Bilder, 08/22/13,
8=Capn Crunch’s Peanut Butter Crunch, 14=Capn Crunch’s Peanut Butter Crunch (same – just on different shelf!), 20=Oreo’s O’s, 25=Post morning traditions – Raison, Date, and Pecan
Page 25: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

The number of stars can be quite large for a large data set because each star represents an experimental unit. Thus, there are limits to the usefulness of these types of plots.

Remember that the placekicking data set has 1,425 observations! Maybe a star could be used to represent each placekicker by averaging over variable values??? This idea of averaging over experimental units (when there is something logical to average over) is a great way to still use this type of plot for large samples.

Parallel Coordinate plots

The variables are each assigned to their own vertical axis. Each variable of interest is rescaled so that minimum observation value corresponds to the bottom of the vertical axis and the maximum observation value corresponds to the top of the vertical axis. Each experimental unit’s variable value is plotted upon the corresponding axis. Lines are drawn to connect values for the same experimental unit.

This type of plot is ideal to allow you to see trends across variables for a particular experimental unit.

There are a number of functions that can be used to construct these plots. One of the main functions is parcoord() from the MASS package. This package is already installed with R, but it needs to be called first

3.25

Chris, 08/23/13,
(Value - Minimum) / (Maximum - Minimum);
Chris2, 08/26/13,
There may still be creative ways to use these plots
Page 26: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

with library(MASS). Other functions that can be used for these plots include ipcp() from the iplots package and ggpcp() from the ggplot2 package.

Example: Cereal data (cereal_graphics.r, cereal_data.xls)> library(MASS)> shelf.color<-rep(x = c("black", "red", "green", "blue"), each = 10)> parcoord(x = cereal[,c(1,8:10)], col = shelf.color, main = "Cereal data")> legend(locator(1), legend = c("1", "2", "3", "4"), lty = c(1,1,1,1), col=c("black", "red", "green3", "blue"), bty = "n", cex = 0.75)

Cereal data

ID sugar fat sodium

1234

3.26

Page 27: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

The horizontal axis gives each variable name, and plotted above these names are the corresponding values for each experimental unit. These values are scaled so that the minimum and maximum values for each variable appear at the bottom and top, respectively, of the vertical axis. A line is drawn for each experimental unit indicating its position across the variables.

Comments:1.Parallel coordinates do not need to be drawn with the

line colors corresponding to a variable. I chose to do this here because it helps particular patterns stand out better.

2.Shelf 2 cereals tend to be from the middle to the top for the sugar variable (0 is on the bottom and 1 is at the top on the vertical axis). This provides a preliminary indication that those cereals have some of the higher sugar content cereals in comparison with the other shelves.

3.There are a few unusual observations for the fat variable as indicated by their large values.

4.Examine what happens when you follow the cereal lines from one variable to another. For example, the highest in sugar content cereals do not necessarily have high fat content.

Below is how I used the ipcp() function to create a similar plot:

> library(iplots)

3.27

Page 28: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> ipcp(vars = cereal[,c(1,8:10)])

I used the Snipping Tool in Windows to copy this plot into Word.

Notice the function opens a separate window through the use of Java. One nice feature of this plot is that it can be “brushed” so that you can see particular experimental units across all variables in the plot. This is done by highlighting a set of lines (left click and drag the mouse over a set of lines; release the left mouse button when complete). Below is what the plot looks like when I do this for the shelf #4 cereals.

3.28

Page 29: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Box plots can be included on each vertical access by selecting VIEW > PCP OVER BOXES from the menu.

3.29

Page 30: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Example: JSCS paper

Monte Carlo simulation studies often produce multivariate data!

Bilder and Tebbs (JSCS, 2009) examined four different ways to estimate a regression model in order to compare

their estimates of a slope parameter, say . A large number of data sets were simulated where the true slope parameter, say 1, was known. For each data set, the four estimation methods were applied in order to obtain

. Thus, the multivariate data had the following form:

3.30

Page 31: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

MethodSimulated data set Individual Alike Random Different

1 -0.5 -0.4 0.1 -0.12 0.1 0.0 -0.1 0.5

where the numerical values are given just for illustrative purposes and are not actual values found in the simulation study.

Below is a parallel coordinates plot that appeared in the paper (color has no special purpose in this plot).

3.31

Page 32: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

There is one small difference with this plot than with the parallel plots shown so far. Specifically, I did not rescale each “variable”. This was purposely done because all four variables were measuring the same item – estimate of 1 – and I wanted to compare the variability among the estimation methods.

From examining this plot, what can you conclude about the estimation methods?

In order to construct a plot of this type, I examined the code in parcoord()

> getAnywhere(parcoord)A single object matching ‘parcoord’ was foundIt was found in the following places namespace:MASSwith value

function (x, col = 1, lty = 1, var.label = FALSE, ...) { rx <- apply(x, 2L, range, na.rm = TRUE) x <- apply(x, 2L, function(x) (x - min(x, na.rm = TRUE))/(max(x, na.rm = TRUE) - min(x, na.rm = TRUE))) matplot(1L:ncol(x), t(x), type = "l", col = col, lty = lty, xlab = "", ylab = "", axes = FALSE, ...) axis(1, at = 1L:ncol(x), labels = colnames(x)) for (i in 1L:ncol(x)) { lines(c(i, i), c(0, 1), col = "grey70") if (var.label) text(c(i, i), c(0, 1), labels = format(rx[, i], digits = 3), xpd = NA, offset = 0.3, pos = c(1, 3), cex = 0.7) } invisible()}<bytecode: 0x0547a35c>

3.32

Chris2, 08/23/13,
1) Look at the variability, 2) Look at largest value for random is smallest value for different
Page 33: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

<environment: namespace:MASS>

I tested the code in parcoord() line-by-line in order to understand what it did, and this led to my own function based on it:

parcoord2<-function (x, col = 1, lty = 1, ...) { matplot(1:ncol(x), t(x), type = "l", col = col, lty = lty, xlab = "", ylab = "", axes = FALSE, ...) axis(1, at = 1:ncol(x), labels = colnames(x)) axis(side = 2) for (i in 1:ncol(x)) lines(c(i, i), c(min(x), max(x)), col = "grey70") invisible() }

Overall, parallel coordinate plots are one of my most favorite types of plots. However, one needs to watch out for the overlapping of lines. This is especially important when a variable has few values (e.g., 0 or 1) and/or there are a very large number of observations.

Trellis plots (co-plots)

These plots allow for the viewing of multidimensional relationships between variables through conditioning. An old website about trellis plots is available at http://netlib.bell-labs.com/cm/ms/departments/sia/project/trellis/wwww.html.

3.33

Chris2, 08/27/13,
Developed at AT&T Bell Labs in the early 1990s
Page 34: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

The picture to the right is a trellis. The important part of the picture is the squares that make up the trellis. Imagine one plot (possibly a scatter plot) within each square and that the plots are from the same data set. However, each plot represents a different subset (possibly overlapping) of the data set. The subsets are determined by conditioning on variable values.

Below is an often-shown trellis plot containing dot plots of yields for various barley varieties from an experiment. Notice the 6 different rectangles or panels of the trellis. They each represent a particular farming location in Minnesota. Therefore, the barley varieties vs. yield dot plots are represented conditionally on farming location.

3.34

Christopher Bilder, 08/23/13,
From Trellis graphics homepage
Page 35: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

3.35

Page 36: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Here is a description from the trellis plots website of the story behind this data:

The barley experiment was run in the 1930s. The data first appeared in a 1934 report published by the experimenters. Since then, the data have been analyzed and re-analyzed. R. A. Fisher presented the data for five of the sites in his classic book, The Design of Experiments. Publication in the book made the data famous, and many others subsequently analyzed them, usually to illustrate a new statistical method.

Then in the early 1990s, the data were visualized by Trellis Graphics. The result was a big surprise. Through 60 years and many analyses, an important happening in the data had gone undetected. The above figure shows the happening, which occurs at Morris. For all other sites, 1931 produced a significantly higher overall yield than 1932. The reverse is true at Morris. But most importantly, the amount by which 1932 exceeds 1931 at Morris is similar to the amounts by which 1931 exceeds 1932 at the other sites. Either an extraordinary natural event, such as disease or a local weather anomaly, produced a strange coincidence, or the years for Morris were inadvertently reversed. More Trellis displays, a statistical modeling of the data, and some background checks on the experiment led to the conclusion that the data are in error. But it was Trellis displays such as the above figure that provided the ``Aha!'' which led to the conclusion.

The lattice and ggplot2 packages are the two main ways that trellis plots can be produced. I will discuss the lattice package here because its code is more like the code that we have been using. Again, there will be a lot of code for the plots. A great way to learn the code is to remove one argument at a time from my own code to see the effect on the plot.

3.36

Chris2, 08/27/13,
coplot() in the graphics package can too, but it is not as good as these packages
Page 37: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Example: Cereal data (cereal_graphics.r, cereal_data.xls)

Data used in a trellis plot setting often needs to be “reshaped” for plotting purposes. To illustrate the process, below is an example of a simple longitudinal data set that is reshaped from a “wide” format to a “long” format:

> #Construct a simple data set – typical format in a repeated measures setting> set1<-data.frame(ID.name = c("subject1", "subject2", "subject3"), ID.number = c(1, 2, 3), age = c(19, 16, 21), time1 = c(1, 0 ,0), time2 = c(0, 0, 1))> set1 ID.name ID.number age time1 time21 subject1 1 19 1 02 subject2 2 16 0 03 subject3 3 21 0 1 > #Long format> set2<-reshape(data = set1, idvar = "ID.name", varying = c("time1", "time2"), v.names = "response", direction = "long", drop = "ID.number")> set2 ID.name age time responsesubject1.1 subject1 19 1 1subject2.1 subject2 16 1 0subject3.1 subject3 21 1 0subject1.2 subject1 19 2 0subject2.2 subject2 16 2 0subject3.2 subject3 21 2 1 > #Remove unnecessary row names> row.names(set2)<-NULL> set2 ID.name age time response1 subject1 19 1 12 subject2 16 1 03 subject3 21 1 0

3.37

Page 38: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

4 subject1 19 2 05 subject2 16 2 06 subject3 21 2 1 > #Back to wide format> set3<-reshape(data = set2, timevar = "time", idvar = "ID.name", direction = "wide")> set3 ID.name age.1 response.1 age.2 response.21 subject1 19 1 19 02 subject2 16 0 16 03 subject3 21 0 21 1

Reshaping the data

Below is how I reshape the original cereal data and also its corresponding standardized format:

> cereal.long<-reshape(data = cereal, idvar = "ID", drop = c("size_g", "sugar_g", "fat_g", "sodium_mg"), varying = c("sugar", "fat", "sodium"), timevar = "content", v.names = "amount", times = c("sugar", "fat", "sodium"), direction = "long")> row.names(cereal.long)<-NULL > head(cereal.long) ID Shelf Cereal content1 1 1 Kellog's Razzle Dazzle Rice Crispies sugar2 2 1 Post Toasties Corn Flakes sugar3 3 1 Kellog's Corn Flakes sugar4 4 1 Food Club Toasted Oats sugar5 5 1 Frosted Cheerios sugar6 6 1 Food Club Frosted Flakes sugar amount1 0.357142862 0.071428573 0.071428574 0.062500005 0.433333336 0.35483871

3.38

Page 39: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> #Construct long format of the data with standardized values> Z.cereal<-data.frame(cereal[,1:3], scale(cereal[,8:10]))> head(Z.cereal) ID Shelf Cereal content1 1 1 Kellog's Razzle Dazzle Rice Crispies sugar2 2 1 Post Toasties Corn Flakes sugar3 3 1 Kellog's Corn Flakes sugar4 4 1 Food Club Toasted Oats sugar5 5 1 Frosted Cheerios sugar6 6 1 Food Club Frosted Flakes sugar amount1 0.357142862 0.071428573 0.071428574 0.062500005 0.433333336 0.35483871

> Z.cereal.long<-reshape(data = Z.cereal, idvar = "ID", varying = c("sugar", "fat", "sodium"), timevar = "content", v.names = "amount", times = c("sugar", "fat", "sodium"), direction = "long")> row.names(Z.cereal.long)<-NULL > head(Z.cereal.long) ID Shelf Cereal content1 1 1 Kellog's Razzle Dazzle Rice Crispies sugar2 2 1 Post Toasties Corn Flakes sugar3 3 1 Kellog's Corn Flakes sugar4 4 1 Food Club Toasted Oats sugar5 5 1 Frosted Cheerios sugar6 6 1 Food Club Frosted Flakes sugar amount1 0.45284412 -1.45752333 -1.45752334 -1.51722235 0.96227546 0.4374379

Histograms:

3.39

Page 40: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> library(lattice)

> histogram(x = ~ amount | content + Shelf, data = Z.cereal.long, type = "percent", layout = c(3,4), xlab = "Standardized amount", main = "Histograms of cereal content")

Histograms of cereal content

Standardized amount

Per

cent

of T

otal

01020304050

-2 -1 0 1 2

fatShelf

sodiumShelf

-2 -1 0 1 2

sugarShelf

fatShelf

sodiumShelf

01020304050

sugarShelf

01020304050

fatShelf

sodiumShelf

sugarShelf

fatShelf

-2 -1 0 1 2

sodiumShelf

01020304050

sugarShelf

Do you see any trends in the data?

Notice the shelf number is incorporated into the plot by vertical dark green lines. If this is not your preference, there are ways to include the actual shelf number by

3.40

Page 41: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

having R treat the shelf as a factor (i.e., character) rather than a numerical value. > Z.cereal.long2<-data.frame(Z.cereal.long, Shelf.char = factor(Z.cereal.long$Shelf))> histogram(x = ~ amount | content + Shelf.char, data = Z.cereal.long2, type = "percent", layout = c(3,4), xlab = "Standardized amount", main = "Histograms of cereal content")

Histograms of cereal content

Standardized amount

Per

cent

of T

otal

01020304050

-2 -1 0 1 2

fat1

sodium1

-2 -1 0 1 2

sugar1

fat2

sodium2

01020304050

sugar2

01020304050

fat3

sodium3

sugar3

fat4

-2 -1 0 1 2

sodium4

01020304050

sugar4

We can even include “Shelf = “ on the plot:

> Z.cereal.long2<-data.frame(Z.cereal.long, Shelf.char =

3.41

Page 42: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

factor(paste("Shelf =", Z.cereal.long$Shelf)))> histogram(x = ~ amount | content + Shelf.char, data = Z.cereal.long2, type = "percent", layout = c(3,4), xlab = "Standardized amount", main = "Histograms of cereal content")

Histograms of cereal content

Standardized amount

Per

cent

of T

otal

01020304050

-2 -1 0 1 2

fatShelf = 1

sodiumShelf = 1

-2 -1 0 1 2

sugarShelf = 1

fatShelf = 2

sodiumShelf = 2

01020304050

sugarShelf = 2

01020304050

fatShelf = 3

sodiumShelf = 3

sugarShelf = 3

fatShelf = 4

-2 -1 0 1 2

sodiumShelf = 4

01020304050

sugarShelf = 4

Finer control of the panels in a trellis plot can be controlled by the panel argument:

3.42

Page 43: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> #Add kernel density estimate to plot > histogram(x = ~ amount | content + Shelf, data = Z.cereal.long, type = "density", layout = c(3,4), xlab = "Standardized amount", main = "Histograms of cereal content", panel = function(x, ...) { panel.histogram(x, ...) panel.densityplot(x, col = "black", ...) } )

Histograms of cereal content

Standardized amount

Den

sity

0.00.20.40.60.8

-2 -1 0 1 2

fatShelf

sodiumShelf

-2 -1 0 1 2

sugarShelf

fatShelf

sodiumShelf

0.00.20.40.60.8

sugarShelf

0.00.20.40.60.8

fatShelf

sodiumShelf

sugarShelf

fatShelf

-2 -1 0 1 2

sodiumShelf

0.00.20.40.60.8

sugarShelf

The panel argument allows for a function to be called (in this case, it is written within the original function call) and applied to each panel. The x argument of the new

3.43

Page 44: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

function takes the contents from the original x in histogram() for a particular content and Shelf.

Scatter plots

> xyplot(x = sugar ~ fat | factor(paste("Shelf =", cereal.long$Shelf)), data = cereal, main = "Scatter plots by shelf")

Scatter plots by shelf

fat

suga

r

0.0

0.1

0.2

0.3

0.4

0.5

0.00 0.02 0.04 0.06 0.08

Shelf = 1 Shelf = 2

Shelf = 3

0.00 0.02 0.04 0.06 0.08

0.0

0.1

0.2

0.3

0.4

0.5

Shelf = 4

Notice how I simply formed the “Shelf = ” panel label within the function call rather than create a new data frame as I did before.

3.44

Page 45: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> xyplot(x = sugar ~ fat | factor(paste("Shelf =", cereal.long$Shelf)), data = cereal, main = "Scatter plots with linear regression model", panel = function(x, y, ...) { panel.xyplot(x, y, col = "black") panel.grid(lty = "dotted", col = "lightgray") panel.lmline(x, y, col = "red", lty = "solid") } )

Scatter plots with linear regression model

fat

suga

r

0.0

0.1

0.2

0.3

0.4

0.5

0.00 0.02 0.04 0.06 0.08

Shelf = 1 Shelf = 2

Shelf = 3

0.00 0.02 0.04 0.06 0.08

0.0

0.1

0.2

0.3

0.4

0.5

Shelf = 4

> cloud(x = sugar ~ fat*sodium | factor(paste("Shelf =", cereal.long$Shelf)), data = cereal, main = "Scatter plots by shelf")

3.45

Page 46: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Scatter plots by shelf

fatsodium

sugar

Shelf = 1

fatsodium

sugar

Shelf = 2

fatsodium

sugar

Shelf = 3

fatsodium

sugar

Shelf = 4

Forming a shingle

There may be times when you want to condition on a continuous variable. This can be done by forming a “shingle” for each panel.

3.46

Page 47: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

> sodium.group<-equal.count(cereal$sodium, number = 3, overlap = 0.1)> sodium.group

Data: [1] 6.071429 9.642857 10.714286 8.750000 7.000000 [6] 5.806452 7.407407 7.407407 7.586207 10.000000[11] 6.333333 1.851852 4.687500 7.407407 7.666667[16] 3.500000 5.666667 4.545455 5.517241 5.555556[21] 5.185185 6.333333 9.354839 3.870968 2.962963[26] 0.000000 5.818182 6.000000 5.084746 0.000000[31] 4.363636 5.000000 4.000000 7.096774 6.000000[36] 2.096774 1.818182 4.727273 5.090909 6.666667

Intervals: min max count1 -0.003081664 5.003082 142 4.996918336 6.336415 143 6.330251669 10.717367 15

Overlap between adjacent intervals:[1] 1 2

> levels(sodium.group) [,1] [,2][1,] -0.003081664 5.003082[2,] 4.996918336 6.336415[3,] 6.330251669 10.717367

> xyplot(x = sugar ~ fat | sodium.group, data = cereal, groups = factor(Shelf), auto.key = TRUE)

3.47

Page 48: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

fat

suga

r

0.0

0.1

0.2

0.3

0.4

0.5

0.00 0.02 0.04 0.06 0.08

sodium.group sodium.group0.0

0.1

0.2

0.3

0.4

0.5

sodium.group

1234

The equal.count() function found 3 separate groups for the sodium content where a 10% overlap in number of observations was allowed between the groups. The darker orange color in the panel title regions indicate the approximate range of sodium values for a particular group.

Example: Communications in Statistics paper

3.48

Page 49: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

I have constructed trellis plots for a few of my papers to summarize Monte Carlo simulation results. This example describes the plot that appeared in Bilder et al. (Communications in Statistics: Simulation and Computation, 2000).

The purpose of the trellis plot was to summarize the Monte Carlo simulation results that evaluated 8 different hypothesis testing methods. Here’s the set-up: There were 500 data simulated using settings that

specified the null hypothesis of interest to be true. For each simulated data set, the hypothesis testing

methods were applied using = 0.05. If the hypothesis testing method held the correct size,

one would expect the approximate proportion of null hypothesis rejections to be equal to 5%.

Due to randomness of Monte Carlo simulation, I would expect all rejection rates to be within

This same process was applied to 62 different data simulation settings. The multivariate data set produced had the following form:

MethodsSimulation

runMarginal

table Marginals n OR RS Bon1 22 0.6, 0.8 20 All 2 0.052 0.038

3.49

Page 50: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

MethodsSimulation

runMarginal

table Marginals n OR RS Bon2 22 0.6, 0.8 20 All 2 0.056 0.052

62 510 0.05, …, 0.95

200 Different 0.070 0.046

where the numerical values are given just for illustrative purposes and are not actual values found in the simulation study.

3.50

Page 51: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

3.51

Page 52: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Do you see any trends?

Note that it would be very difficult to do this exact plot completely in R. Instead, one can construct as much of the plot as possible in R, copy it into PowerPoint (or some other software package), and add annotations to the plot.

3.52

Page 53: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

Final notes

Below are a few R books dedicated to graphics:1)R Graphics, 2nd edition by Paul Murrell2)Lattice: Multivariate Data Visualization with R by

Deepayan Sarkar (library has PDF: http://library.unl.edu/record=b4154543)

3)ggplot2: Elegant Graphics for Data Analysis by Hadley Wickham (library has PDF: http://library.unl.edu/record=b4155947)

Departments of Statistics sometimes offer entire courses on graphics. One course website available is at:

http://homepage.stat.uiowa.edu/~luke/classes/295-vis

which also contains links to other websites.

The Iowa State University Department of Statistics has a few researchers in the graphics area: Diane Cook: http://www.public.iastate.edu/~dicook/ Hieke Hoffman: http://www.public.iastate.edu/~hofmann/

Two other important packages for graphics are: ggplot2 Rggobi (connects to the Ggobi package -

http://www.ggobi.org)

3.53

Page 54: Chris Bilder's STAT Web Portal - Scatter plot · Web viewPlease think of my code as a template. You can modify the template for your own data sets. I will primarily use the cereal

A statistics research journal that often publishes papers on graphics is the Journal of Computational and Graphical Statistics.

3.54