Students will learn how to …
R has several built-in functions for known distributions. Let’s learn a little bit more about them!
For this example please use the rnorm()
function. To
learn about the arguments that this function takes you can type
?rnorm
to read the help file.
Generate 10000 observations from the Normal distribution using the defaults in the rnorm function.
### GENERATE
x<-rnorm(10000)
Create a plot that estimates the density based on these data
### PLOT DENSITY
## BASE
plot(density(x))
## GGPLOT
library(tidyverse)
as.data.frame(x)%>%
ggplot(aes(x))+
geom_density()
We can find the height
### DENSITY AT 0
dnorm(0)
## [1] 0.3989423
We can evaluate the area under the curve (to the left)
### PROBABILITY
pnorm(0)
## [1] 0.5
What observations do you have about the normal distribution based on your answers above?
### TWO-DIM KERNEL DENSITY
### n=50 GRID
xx.kde<-kde2d(xx[,1], xx[,2], n=50)
Plots built into base R:
### PLOT (SCATTER)
plot(xx[,1], xx[,2], pch=16)
### CONTOUR
contour(xx.kde, lwd = 2, add = TRUE,
col = hcl.colors(10, "Spectral"))
### HEATMAP
image(xx.kde)
### PERSPECTIVE
persp(xx.kde, phi=45, theta=30)
Generate data from the multivariate normal distribution that has zero correlation
## ZERO CORR
yy<-mvrnorm(1000, mu=c(0,0), Sigma=matrix(c(1, 0, 0, 1), 2))
### ESTIMATED DENSITY
yy.kde<-kde2d(yy[,1], yy[,2], n=50)
Plots built into base R:
### PLOT (SCATTER)
plot(yy[,1], yy[,2], pch=16)
### CONTOUR
contour(yy.kde, lwd = 2, add = TRUE,
col = hcl.colors(10, "Spectral"))
### HEATMAP
image(yy.kde)
### PERSPECTIVE
persp(yy.kde, phi=45, theta=30)
Compare the sets of plots. What do you observe?