In: Statistics and Probability
R studio questions
Write up your answers and paste the R code
Copy and paste all plots generated.
First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution (area under the curve); qnorm gives you the value of x associated with a specific cumulative probability (inverse probability); rnorm allows you to create random samples.
1. Create the variable x by taking a random sample of size 100 from a normal population with mean 75 and standard deviation 10, that is x<-rnorm(100,75,10).
(a) Calculate the mean, median, standard deviation, and plot the data using a boxplot. Now I want to show you how to create a dummy variable that gives us a 1 if the value satisfies a condition and zero otherwise. To do this you use the command ifelse command. You can search this in help as well. 2. Create a dummy variable p72 that gives you a 1 if the value of x is less than 72 and 0 if not. p72<- ifelse(x<72,1,0). (a) calculate the mean. Note this is the proportion of observations of x below 72.
(b) use pnorm funciton to find out the probability that the random variable X is less than 72. Compare to your sample proportion.
(c) use the math operators in R to construct the lower and upper ends of a 95% confidence interval for the population proportion.
Step 1. Created the variable x by taking a random sample of size 100 from a normal population with mean 75 and standard deviation 10 using the command x<-rnorm(100,75,10).
Step 2. Calculated the mean, median, standard deviation, and plot the data using a boxplot using the command mean(), median(), sd() and boxplot().
Step 3. Create a dummy variable p72=0, Created a p72 variable which will increase by 1 if x<72 otherwise not. p_hat is the sample proportion.
Step 4. Use the pnorm funciton to find out the probability that the random variable X is less than 72.
(c) 95% confidence interval for the population proportion using the sample proportion.