Stat 421, Fall 2007



Stat 421, Fall 2008, Fritz Scholz

Homework 2, Solutions

Problem 1 (CDF and Density): Execute the following command lines in R and try to understand what each detail in them means by using the documentation (e.g. ?hist):

y=rnorm(30)

hist(y,probability=T,xlim=c(-3.5,3.5),col=c("blue","orange"))

x=seq(-3.5,3.5,.01)

fx=dnorm(x)

lines(x,fx,col="red")

abline(h=0)

Next try

plot.stepfun(y,xlim=c(-3.5,3.5),main=”empirical CDF”,xlab=”y”,ylab=”F(y)”)

CDF=pnorm(x)

lines(x,CDF,col="red")

Now create an R function

CDF.density.plot(mu=100,sigma=5,n=50){ you fill in the rest}

that plots the histogram of a random sample y of size n from a normal distribution with mean mu and standard deviation sigma. Then superimpose the corresponding true density from which you sampled (as was done in the example above, you just have to make small changes to incorporate mu and sigma, see ?dnorm).

In a separate plot show the empirical cumulative distribution function (CDF) of the same sample (as illustrated by the step function in the second code snippet above) and superimpose the corresponding true CDF from which you sampled.

Arrange it so that the two plots appear on the same page using the command par(mfrow=c(2,1)) at the start of the function body (see ?par). Make sure that the two plots use the same specifications for xlim in the call to hist and plot.stepfun.

Provide your function code and two sample plot pages, one with n=40 and one with n=75.

Solution:

The code of the requested function, followed by the sample plots for n=40 and n=75.

CDF.density.plot ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download