Lab Exercise 4 for R: Normal Distribution

Exercise 1: Generate a sample from a normal distribution and throw the textbook at it

...

In R you can generate a variable filled with 100 random values from a normal distribution centered at mean 0 with a standard deviation of 1

> randNormVec <- rnorm(100,0,1)
> randNormVec

You can also utilize some functions in package 'pastecs' or 'psych' to obtain descriptive statistics.

> install.packages("pastecs")
> library(pastecs)
> options(digits=4)
> stat.desc(randNormVec)

or ...

> install.packages("psych")
> library(psych)
> describe(randNormVec)

you can generate a frequency histogram of this generated sample either as a histogram, a density plot, or a cumulative distribution function ...

> hist(randNormVec)

> plot(density(randNormVec))

> plot(ecdf(randNormVec))

you can overlay the histogram with a normal distribution of the sample's mean and SD ...  and test for significant deviations from normality using Shapiro-Wilks

> hist(randNormVec, breaks=20, prob=TRUE, xlab="generated", main="Normal overlay")
> yBar <- mean(randNormVec)
> ySD <- sqrt(var(randNormVec))
> curve(dnorm(x, mean=yBar, sd=ySD), col="red", lwd=2, add=TRUE)

> shapiro.test(randNormVec)

to save any of these generated graphs into an editable .svg document flank them with svg() ... and dev.off()  ...

> svg("freqPlot.svg",width=8,height=5)
> hist(randNormVec)
> dev.off()


last modified: 2/2/15