Docsity

R Lab: Probability Distributions, Histograms, Descriptive Stats, Confidence Intervals - Pr, Lab Reports of Statistics

Toggle sidebar
bg1
Lab 1 STAT 3000
Introduction to R: Probability Distributions, Histograms,
Descriptive Statistics, and Confidence
Intervals
• Start the program R by finding it in the 'Start Menu' on a Windows
computer where it has been installed.
• R is a command-line language, which means, you type in what you
want it to do at a prompt (in the big white box) and R immediately
processes your request and returns the requested value. This is
different than the SAS program which compiles a set of commands by
default. It is also different than Excel, which operates by
manipulating spreadsheets, clicking on buttons, and using menus.
• R has several useful built-in functions that we will use in this class.
For example, many common pdfs and cdfs are available and can help
you calculate various probabilities and/or quantiles of distributions.
Consider the binomial probability distribution. The PMF for the
binomial distribution is referred to as 'dbinom' in R. Suppose we
wanted to know the probability of getting 3 heads in 5 fair coin flips:
> dbinom(3,5,.5)
Notice that the first argument issued to the 'dbinom' function is the
realization of interest, the second is the number of trials, and the third
is the probability of a success. We can check that this is indeed the
correct probability by using R and the actual equation of the PDF
directly:
> choose(5,3)*(.5^3)*((1-.5)^(5-3))
Notice that the 'choose' function calculates combinations for us. If
you want or need to compute actual factorials, use the 'factorial'
command (rather than the '!' symbol, which will not work).
pf3
pf4
pf5

Partial preview of the text

Download R Lab: Probability Distributions, Histograms, Descriptive Stats, Confidence Intervals - Pr and more Lab Reports Statistics in PDF only on Docsity!

Lab 1 STAT 3000

Introduction to R: Probability Distributions, Histograms,

Descriptive Statistics, and Confidence

Intervals

  • Start the program R by finding it in the 'Start Menu' on a Windows computer where it has been installed.
  • R is a command-line language, which means, you type in what you want it to do at a prompt (in the big white box) and R immediately processes your request and returns the requested value. This is different than the SAS program which compiles a set of commands by default. It is also different than Excel, which operates by manipulating spreadsheets, clicking on buttons, and using menus.
  • R has several useful built-in functions that we will use in this class. For example, many common pdfs and cdfs are available and can help you calculate various probabilities and/or quantiles of distributions. Consider the binomial probability distribution. The PMF for the binomial distribution is referred to as 'dbinom' in R. Suppose we wanted to know the probability of getting 3 heads in 5 fair coin flips:

    dbinom(3,5,.5) Notice that the first argument issued to the 'dbinom' function is the realization of interest, the second is the number of trials, and the third is the probability of a success. We can check that this is indeed the correct probability by using R and the actual equation of the PDF directly: choose(5,3)(.5^3)((1-.5)^(5-3)) Notice that the 'choose' function calculates combinations for us. If you want or need to compute actual factorials, use the 'factorial' command (rather than the '!' symbol, which will not work).

  • R has built in help documentation and can be accessed to find out more about particular R functions. For example, for more information on the 'dbinom' function, see the help documentation:

    help(dbinom)

  • Probabilities can be computed in R using the CDF for the probability distribution of interest. For example, if the random variable of interest is Normal with a mean of 10 and standard deviation of 3, then the probability that the random variable will be between, say, 7 and 9, can be computed by:

    pnorm(9,10,3)-pnorm(7,10,3)

  • Quantiles can also be computed easily in R. Consider the exponential distribution with parameter € λ = 1 / 2 ; in order to find the 75th percentile of the distribution:

    qexp(.75,1/2)

  • R has excellent graphical capabilities as well. For example, if we wanted to view the PMF of a binomial random variable considered previously:

    barplot(dbinom(0:5,5,.5),names=0:5,ylab="f(x)", xlab="x") The CDF can be viewed using a similar command: barplot(pbinom(0:5,5,.5),names=0:5,ylab="F(x)", xlab="x")

  • Distributions for continuous random variables can also be graphed in R. For example, the PDF for the normal random variable, with mean

Now you have created a data frame that contains two components: the data for UU students and the data for USU students. You can access each one separately in a couple of different ways:

weights.df$USU weights.df[,2] The first uses the dollar sign to select a portion of the data frame while the latter uses matrix indices. The advantage in using the matrix indices is that you can identify rows as well as columns. For example, suppose we wanted to view (or use) the third and fourth observations from both samples: weights.df[3:4,1:2] To find the size of the data frame you should use the 'dim' command. This command outputs the number of rows and columns, respectively: dim(weights.df)

  • Summary Statistics : R has several built-in functions for performing routine tasks. For example, to find the minimum, maximum, sum, sample mean, sample variance, sample standard deviation, sample median, sample quartiles, and sample IQR of the UU student weights:

    min(weights.df$UU) max(weights.df$UU) sum(weights.df$UU) mean(weights.df$UU) var(weights.df$UU) sd(weights.df$UU) median(weights.df$UU) q1=quantile(weights.df$UU,.25) q3=quantile(weights.df$UU,.75) q3-q Typing all of these commands can be tedious if you want to examine several datasets. However, if each variable is stored in the same data frame, some commands can be applied to all elements of the data frame simultaneously. For example, the sample mean and standard deviation work this way:

mean(weights.df) sd(weights.df) Another useful function that will simultaneously provide several sample statistics is the 'summary' function: summary(weights.df)

  • Histograms and Boxplots : Creating plots is very easy in R as there are several built-in plotting functions. For example, as we saw in class, creating a histogram takes only one line. To create a histogram (with a default number of bins) for UU student weights:

    hist(weights.df$UU) We can also plot several histograms together. To construct a plot containing both histograms from USU and UU students: first define the bins you want to use, and then issue the histogram functions twice (with different options for plot specifics like color and shading): bins=seq(130,200,,8) hist(weights.df$UU,breaks=bins,ylim=c(0,5),col=2, density=10,main="histogram") hist(weights.df$USU,breaks=bins,col=4, angle=-45,density=10,add=TRUE) To create a boxplot from a sample, we use the 'boxplot' function in R: boxplot(weights.df$UU,range=0) Boxplots are excellent tools for graphically viewing numerous samples simultaneously. Consider the side-by-side boxplots for UU and USU student weights: boxplot(weights.df,col=c(2,4),range=0)

  • Confidence Intervals : Recall that the construction of confidence intervals requires the calculation of the sample mean, t-score, and standard error. To compute 95% confidence interval for the mean weight of UU students, simply apply the appropriate formula in R, utilizing the necessary functions:

Lab Assignment 1 1.) If X is a Poisson random variable with mean 10, find the following probabilities using R (hint: use the 'dpois' and 'ppois' function): a. P(X=12) b. P(X>12) c. € P ( 3 ≤ X < 13 ) 2.) Create a plot of the PMF for a Binomial random variable resulting from 15 trials and with success probability equal to one third. Print out, label, and hand it with your assignment. 3.) Production lines in a manufacturing plant are set to make steel ball bearings with a diameter of 1 micron. Ten ball bearings were randomly selected from two production lines. The diameters of the ball bearings measured in microns were as follows: Line 1 Line 2 1.18 1. 1.42 1. 0.69 1. 0.88 0. 1.62 1. 1.09 0. 1.53 1. 1.02 1. 1.19 1. 1.32 0. a. Use R to calculate and report the following statistics for each line: sample mean, sample median, sample standard deviation, sample IQR. b. Construct side-by-side boxplots using data from the two production lines. c. Pool the data together and use it to construct a single histogram for measured bearing diameters from this plant. Use 'bins=seq(.5,2,,6)'; this will give you 5 bins of equal width between a half micron and 2 microns. d. Estimate the mean bearing size for each of the production lines (separately) with 90% confidence intervals (assume the data are normally distributed). e. Based on answers to the above questions, do you think the mean diameter sizes from the two production lines are significantly different? Explain. f. Construct a 90% confidence interval for the mean bearing size of the plant (like in part c). That is, consider data from both lines as one dataset (consisting of 20 observations). Is this interval narrower or wider than the other two you computed? Why?