Download presentation
Presentation is loading. Please wait.
1
Stats Lab #3
2
Frequency Distribution Tables
Create a set of data. Include at least 20 scores, and make sure there are many repetitions. X = c(..., ..., ...) First, look at the values in the sample and the frequencies of those values
3
Frequency Distribution Tables
The function factor(X) tells R to treat X as a nominal variable. If we input this to the summary() function, we get counts of all the values (treating X as a nominal variable leads summary() to give us simple counts instead of statistics like median and mean). f = summary(factor(X)) f
4
Frequency Distribution Tables
This is a frequency distribution table. It shows you the frequency f(x) for every value x. Notice that the total of the frequencies equals the size of the sample. n = length(X) n sum(f)
5
Frequency Distribution Tables
f is very similar to a histogram. In fact we can make a bar plot of f to get a histogram. barplot(f)
6
Cumulative Distributions
Pick any value in your sample. y= ___ To find the value of the cumulative distribution at x, we need to know how many scores are less than or equal to x. There are two ways to do this.
7
Cumulative Distributions
First, we can count the raw scores. Recall that this command: y<= x will give a TRUE/FALSE vector showing which entries in X are less than or equal to x. If we take the sum of this vector, sum() will add up all the TRUEs, which tells us the total number of scores that are less than or equal to x. count = sum(y <= x)
8
Cumulative Distributions
The second solution is to use the frequency table, and add up the frequencies for all values less than or equal to x. To do this, you need to find which entry x is in the table. If it were the third entry (meaning x is the third SMALLEST of the values that appear in the sample), then you would enter count = sum(f[1:3])
9
Cumulative Distributions
These two uses of sum() work in different ways. The first counts how many times X is less than or equal to x, whereas the second adds up the frequencies in f that correspond to values less than or equal to x. You should be able to see why both methods give the same result.
10
Cumulative Distributions
Either way you do it, you can then get the cumulative distribution by dividing the count by the sample size. count/n To get the whole cumulative distribution function, use ecdf(), which stands for empirical cumulative distribution function. F = ecdf(X)
11
Cumulative Distributions
ecdf() is an unusual function because the result it gives, f, is a function itself. You can use the function ff to evaluate the cumulative distribution at any value. Start with x to verify you get the same result as above. Then try some other values, including values inside, below, and above the raw distribution. f(x)
12
Cumulative Distributions
To see the whole function at once, plot it. Notice how it starts at zero, it jumps up at every value in the sample, and it ends at one. plot(f)
13
Percentiles To find what percentile a score is at, count the number of scores less than or equal to it, divide by n to get the proportion of the total sample, and multiply by 100 to get a percentage. count = sum(y<= x) proportion = count/n percentile = proportion * 100 Notice that the middle step, proportion, is the same as the cumulative distribution, f(x). You can also use F directly to get percentiles. f(x) * 100
14
Normal Distributions Here's a custom function that plots Normal distributions: (Theoretical) normal = function(mu,sigma) r = (-4000:4000)/1000 mu=mean(z) sigma=sqrt(sum((z-mu)^2)) x =mu + sigma*r Density = 1/(sigma*sqrt(2*pi))*exp(-z^2/2) plot(x,Density,type="l")
15
Normal Distributions If you’re interested, the important line is the one that defines Density. This is the mathematical equation that defines a Normal distribution. Go to this address and paste the code for the normal() function into the R window.
16
Normal Distributions Here’s how to use the normal() function. mu and sigma are numbers you supply or variables you define. normal(mu, sigma) Start by plotting the Standard Normal, which means with a mean of zero and standard deviation of one (this is the standardized distribution, or distribution of z-scores, for any other Normal distribution). normal(0,1)
17
Normal Distributions Plot a few more Normal distributions using other values of mu and sigma. Notice how the values you use affect the position and size of the distribution, although the shape always stays the same.
18
Z-scores To find a z-score for any value x, all you need is the mean and standard deviation. You should know how to compute these statistics already. x = ___ z = (x-mu)/sigma Compare your choice of x to the distribution of X to verify that the result is a sensible z-score. Is x above or below the mean? Is it close to the center or far away?
19
mu=mean(x) sigma=sqrt((x-mu)^2) z=(x-mu)/sigma z
20
Z-scores If you have a z-score, you can convert back to the raw score. Recall that z tells you how many standard deviations the raw score is above (or below) the mean. So, to get the raw score, we start at the mean and add z standard deviations. z = ___ x = mu + z*sigma
21
Z-scores Again, compare your result to the distribution of X to verify it is sensible based on the z-score you started with. We can compute all the z-scores at once using vector arithmetic: Z = (X-mu)/sigma Z Z is now the standardized distribution.
22
Z-scores Plot the cumulative distribution functions for both the raw scores and the standardized scores, and notice how they are the same and how they are different. Before creating these plots, enter the following command, which tells R that you want to look at multiple plots at once (in this case, in a 12 arrangement). par(mfrow=c(1,2))
23
Probabilities of z-scores
A useful function for inferential statistics is pnorm(), which is the cumulative distribution function for the standard Normal. Therefore pnorm(z) gives the probability that an observation will have a z-score less than or equal to z, assuming that the distribution is Normal. Try a few values, including 0, positive numbers, negative numbers, large numbers, and numbers close to 0. Compare your results to the plot of the standardized Normal from above (you will have to redraw it). pnorm(___)
24
Probabilities of z-scores
If you want the probability of a z-score greater than or equal to z, just subtract from 1: 1 - pnorm(___) To get a percentile, multiply by 100: > pnorm(___) * 100
25
Probabilities of z-scores
The inverse of pnorm() is qnorm(). qnorm() tells you what z-score is at a particular quantile. For example, the z-score for the first quartile is: qnorm(.25)
26
Probabilities of z-scores
Try finding the z-scores for the median and the 62nd percentile. Think about what the result should be before you do each calculation. Also notice that you can’t give qnorm() inputs less than 0 or greater than 1 (try it), and test what happens if you input 0 or 1. Notice that the probability of being less than any quantile is the same as the number that defined that quantile. pnorm(qnorm(___)) Also, the quantile for any probability is the same as the original z-score: qnorm(pnorm(___))
27
Assignment #3 Imagine you are conducting a memory experiment, in which each subject is asked to recall 20 items. The possible scores are thus 0:20. Start by generating fake data for 200 subjects using this command: X = round(runif(200)* ) The runif() function creates random numbers that are uniformly distributed between 0 and 1. The rest of this command converts those numbers into integers from 0 to 20.
28
Assignment #3 1. Create a frequency distribution table for the data. Use the table to make a histogram of the distribution (include the commands, but not the plot, in your submission). Write a brief comment on the shape of the distribution – how does it compare to a Normal?
29
Assignment #3 2. Pick a value x in your distribution (use a value between 13 and 18) and use the cumulative distribution function to find its percentile. 3. Find the z-score for x. 4. Use the pnorm() function to predict the proportion of scores less than or equal to x. Convert the result to a percentile. 5. Notice the answers from questions 2 and 4 are different. Write two reasons why.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.