DataM: Inclass Exercise 0413
In-class exercise 1.
Run the following R script to see what happens. Change the ‘df’ parameter to a slightly larger integer and do it again. What statistical concept does this script illustrate?
x <- seq(-pi*2, 2*pi, .05)
z <- dnorm(x)
y <- dt(x, df=3)
plot(x, z, type="l", bty="L", xlab="Standard unit", ylab="Density")
polygon(c(x, rev(x)), c(y, rev(z)), col='aliceblue')
lines(x, y, col='cadetblue')[Solution and Answer]
Put plotting codes into a function to avoid massive replicated codes.
f_plot <- function(df) {
x <- seq(-pi*2, 2*pi, .05)
z <- dnorm(x)
y <- dt(x, df=df)
plot(x, z, type="l", bty="L", xlab="Standard unit", ylab="Density")
polygon(c(x, rev(x)), c(y, rev(z)), col='aliceblue')
lines(x, y, col='cadetblue')
text(-4, 0.3, paste0('df = ', df)) # add this command to show df value for each plots
}Use different values of df to call f_plot to find the change.
From these plots, we can know that these codes try to illustrate that the t distribution gets closer to the standard normal distribution as the degree of freedom increases.
In-class exercise 2.
Doll (1955) showed per capita consumption of cigarettes in 11 countries in 1930, and the death rates from lung cancer for men in 1950. Use R base graphics to generate the plot shown below.
Source: Freedman, et al. (1997). Statistics. pp. 148-150.
- Column 1: Country names
- Column 2: Cigarettes consumption (per million)
- Column 3: Death rate (per capita)
[Solution and Answer]
Load in the data set
'data.frame': 11 obs. of 3 variables:
$ Country : Factor w/ 11 levels "Australia","Canada",..: 1 2 3 4 10 5 6 7 8 9 ...
$ consumption: int 480 500 380 1100 1100 230 490 250 300 510 ...
$ death : int 180 150 170 350 460 60 240 90 110 250 ...
In-class exercise 3.
Use R base graphics to create the national flag of Denmark.
Target output
[Solution and Answer]
op <- par(bg='red')
plot.new()
par(mar = c(0, 0, 0, 0)) # close the inner margins
par(oma = c(0, 0, 0, 0)) # close the outer margins
plot.window(xlim=c(0, 37), ylim=c(0, 28))
abline(h=14, col='white', lwd=96*4) # since one unit in lwd equal 1/96 inch
abline(v=14, col='white', lwd=96*4)In-class exercise 4.
Run the R script to see what happens first and then explain how the effect is achieved by the script.
[Solution and Answer]
n <- 60
t <- seq(0, 2*pi, length=n)
x <- sin(t)
y <- cos(t)
#
par(pty = "s")
#
for (i in 1:n) {
plot.new()
plot.window(c(-1, 1), c(-1, 1))
lines(x*y, -y, col="gray")
points(x[i]*y[i], -y[i], pch=16,
col=gray((i-1)/(n+1)))
Sys.sleep(.05)
}This script drew n (e.g., 60 here) plots to demonstrate a point roll along with a 8-shape curve. The color of the point get lighter when rolling.
In-class exercise 5.
Draw a pie chart to represent 50 shades of gray. Hint: Use ?gray to examine the gray level specification documented for the gray{grDevices}. Use ?pie to study the function for making pie charts documented for pie{graphics}.
[Solution and Answer]
par(mar = c(0, 0, 0, 0)) # close the inner margins
par(oma = c(0, 0, 0, 0)) # close the outer margins
pie(table(1:50), col = gray(seq(0, 1, by=1/50)))