In_class exercise 0413
In_class exercise1
Run the 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')
legend(5,0.4, legend="df=3")x <- seq(-pi*2, 2*pi, .05)
z <- dnorm(x)
y <- dt(x, df=5)
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')
legend(5,0.4, legend="df=5")x <- seq(-pi*2, 2*pi, .05)
z <- dnorm(x)
y <- dt(x, df=10)
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')
legend(5,0.4, legend="df=10") As degrees of freedom increases, the t-distribution has thinner tails and closer to normal distribution.
In_class exercise2
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
## '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 ...
with(dta, plot(consumption, death, type='n', xlab= "Consumption(per capita)", ylab="Death rate(per million per year)", main='Lung Cancer and Cigarettes consumption '))
# add regression line
abline(lm(death~consumption , data=dta),lty=2)
# label states
with(dta, text(consumption, death,
labels= dta$Country, cex=.5))In_class exercise3
Use R base graphics to create the national flag of Denmark :
According to wikipedia, the legal proportions of the National flag is 28:34 (14:17) to 28:37, and 3:1:3 in width and anywhere between 3:1:4.5 and 3:1:5.25 in length.
op <- par(bg="white")
plot.new()
plot.window(xlim=c(0, 37),
ylim=c(0, 37))
rect(0, 0, 12, 12, col="red",
border="red")
rect(16,0 , 34, 12, col="red",
border="red")
rect(0,16 , 12, 28, col="red",
border="red")
rect(16,16 , 34, 28, col="red",
border="red")
segments(0, 0, 34, 0)
segments(0, 0, 0,28 )
segments(0, 28, 34, 28)
segments(34, 0, 34, 28)
title(main='National flag of Denmark')In_class exercise4
Run the R script to see what happens first and then explain how the effect is achieved by the script.
n <- 60
t <- seq(0, 2*pi, length=n)
x <- sin(t)
y <- cos(t)
#
par(pty = "s")
for (i in 1:n) { # The script drew 60 plots with the loop executed 60 times.
plot.new()
#set the scales on along the sides of the plot.
plot.window(c(-1, 1), c(-1, 1))
# drew a 8-shape curve in gray
lines(x*y, -y, col="gray") #
# drew points at the specified coordinates
points(x[i]*y[i], -y[i], pch=16,
# adjust color that they appear lightened while "i" increases. The command "gray(0)= black, gray(1)=white".
col=gray((i-1)/(n+1)))
# a specified time interval
Sys.sleep(.05)
}In_class exercise5
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}.
ㄑ