R statistic program onlY !!!! do not ask me to do it if you do not know
Econ 4650 Spring 2019
Introduction to R Basic Statistics & R
This chapter provides a good review of basic statistics that we will use throughout the course. It should serve as a reference guide for you as we discuss estimation, sampling distributions, and hypothesis testing. We will utilize normal, F, and t distributions frequently. Tables for these distributions are provided at the end of the text.
In this module, you will learn how to enter data into R from a CSV file, describe the data, and produce simple plots.
Assuming you have R installed on your computer you can type (or copy and paste) everything you see in the shaded area (written in this font) and you should see the results (after the ##) just as you see them in this PDF. (Note,[1] if you need to install R, or would like to find more information on the software, you can do so https://cran.r-project.org/,[2] there are a few tasks that we will use R for in this course in which you must be working on a computer in which you have adminstrative rights in order to complete).
For example, let’s use R as a simple calculator to find 2 + 2. What I do is type “2 + 2” and press the enter key 2 + 2
## [1] 4
We see that R returns the correct answer.
Now, let’s try 2 x 3 2 * 3
## [1] 6
and 23
2^3
## [1] 8
We can also get the natural log log(2.71821)
## [1] 0.9999736
log(exp(1))
## [1] 1
Let’s input the data for the height of a random sample of U.S. women as in the text (see page 9 of the custom edition of the text). As in the text, we will call the data X X <- c(61.0, 63.5, 66.0, 68.5, 71.0)
We can see, in R, what is in our workspace by typing ls()
1
ls()
## [1] "X"
R knows that there is something called X and can report back the values in X X
## [1] 61.0 63.5 66.0 68.5 71.0
The textbook states that the mean (µ) of these data is known to be equal to 66 with a known standard deviation (σ) of 2.5 inches. Let’s use R to calculate the standardized Z-values from our data Z <- (X - 66)/2.5
and we can see the results Z
## [1] -2 -1 0 1 2
In R, we can calculate the mean, variance, and standard deviation for a particular sample of data mean(X)
## [1] 66
var(X)
## [1] 15.625
sd(X)
## [1] 3.952847
Let’s assume that heights are distributed according to the normal distribution with mean 66 and standard deviation 2.5. We can have R randomly sample from this distribution. Let’s create a sample of 100 draws X100 <- rnorm(100, mean = 66, sd = 2.5)
and find mean(X100)
## [1] 66.2055
sd(X100)
## [1] 2.910129
Now let’s see what a simple histogram for these data look like
2
hist(X100)
Histogram of X100
X100
F re
q u
e n
cy
60 65 70
0 5
1 0
1 5
2 0
2 5
3 0
When you do this on your computer, you will get different results. That is because your random draw will be different.
We can have the same draw by doing this set.seed(100) X100a <- rnorm(100, mean = 66, sd = 2.5)
And we find mean(X100a)
## [1] 66.00728
sd(X100a)
## [1] 2.551776
With the same histogram hist(X100a)
3
Histogram of X100a
X100a
F re
q u
e n
cy
60 62 64 66 68 70 72 74
0 5
1 0
1 5
2 0
2 5
3 0
The quantiles of the data can be determined by quantile(X100a)
## 0% 25% 50% 75% 100% ## 60.32019 64.47788 65.85145 67.63973 72.45490
So the mean of this particular random sample is 66.007 and the median is 65.85. As an aside, let’s look at the distribution of a draw of size 20,000 from a standard normal set.seed(666) zdraw <- rnorm(20000, mean = 0, sd = 1) mean(zdraw)
## [1] 0.007248452
quantile(zdraw, prob = c(0.01, 0.05, 0.5, 0.95, 0.99))
## 1% 5% 50% 95% 99% ## -2.34318795 -1.63898524 0.01879137 1.65229300 2.30314467
You can compare this draw with the theoretical values for our requested percentiles (.01, .05, .5, .95, .99) of -2.326, -1.645, 0, 1.645, and 2.326.
Here is a plot of the large draw plot(density(zdraw))
4
−4 −2 0 2 4
0 .0
0 .1
0 .2
0 .3
0 .4
density.default(x = zdraw)
N = 20000 Bandwidth = 0.1242
D e
n si
ty
Throughout the class we will generate random data to learn about econometrics and for results to match, setting the random number seed via set.seed() is a useful R command. Later on we will learn how to use many of the built-in random distribution functions in R. (Just like we did here by generating a sample of size 100 drawn from a normal distribution.)
Read Data
Entering data the way we did before is awkward. So now let’s see how to read in a set of data that comes in a csv file (comma separated values). These files can be read by Excel and Excel can export csv files. They are easily read in R (and other statistical packages).
Let’s begin with data from Table 1 on a sample of 22 single-family homes in Diamond Bar, California.
I downloaded the Excel file and saved it as a csv file under STAT17.csv. Before saving the file in R, I changed my working directory (i.e., the place on my computer that R will read and write files to) with the command setwd(). I can ensure the STAT17.csv is in my working directory by asking R to report all of the file names that I have saved in this folder with the dir() command setwd("C:/myfile/University of Utah/2019 Spring/Econometrics") dir()
## [1] "Assignment 1.pdf" "Chapter 1.pdf" "STATS17.csv"
Indeed, the file is listed as being saved in my working directory. I can read it into R and name it data17 by typing data17 <- read.csv("STATS17.csv", header = T)
5
Note that R is case sensitive, so Data17 will be different than data17, or datA17, etc. (In the read command, the header comment is telling R that the first row contains the variable names, or labels.)
Here are what the data look like data17
## X OBS PRICE SQFT ## 1 1 1 425000 1349 ## 2 2 2 451500 1807 ## 3 3 3 508560 1651 ## 4 4 4 448050 1293 ## 5 5 5 500580 1745 ## 6 6 6 524160 1900 ## 7 7 7 500580 1759 ## 8 8 8 399330 1740 ## 9 9 9 442020 1950 ## 10 10 10 537660 1771 ## 11 11 11 515100 2078 ## 12 12 12 589000 2268 ## 13 13 13 696000 2400 ## 14 14 14 540750 2050 ## 15 15 15 659200 2267 ## 16 16 16 492450 1986 ## 17 17 17 567047 2950 ## 18 18 18 684950 2712 ## 19 19 19 668470 2799 ## 20 20 20 733360 2933 ## 21 21 21 775590 3203 ## 22 22 22 788888 2988
Here are the summary statistics for data17 summary(data17)
## X OBS PRICE SQFT ## Min. : 1.00 Min. : 1.00 Min. :399330 Min. :1293 ## 1st Qu.: 6.25 1st Qu.: 6.25 1st Qu.:494483 1st Qu.:1762 ## Median :11.50 Median :11.50 Median :530910 Median :2018 ## Mean :11.50 Mean :11.50 Mean :565829 Mean :2164 ## 3rd Qu.:16.75 3rd Qu.:16.75 3rd Qu.:666153 3rd Qu.:2634 ## Max. :22.00 Max. :22.00 Max. :788888 Max. :3203
We can generate a scatterplot of these data plot(data17$SQFT,data17$PRICE)
6
1500 2000 2500 3000
4 e
+ 0
5 5
e +
0 5
6 e
+ 0
5 7
e +
0 5
8 e
+ 0
5
data17$SQFT
d a
ta 1
7 $
P R
IC E
Here is another way to be able to access the variable names or labels for the SQRT and PRICE data from the data17 frame attach(data17)
## The following object is masked _by_ .GlobalEnv: ## ## X
plot(SQFT, PRICE)
7
1500 2000 2500 3000
4 e
+ 0
5 5
e +
0 5
6 e
+ 0
5 7
e +
0 5
8 e
+ 0
5
SQFT
P R
IC E
Above, we have attached the data17 so that we could easily work with the variables in the file.
We can also get the correlation between the variables SQFT and PRICE cor(SQFT, PRICE)
## [1] 0.8768234
We see that the correlation coefficient aligns with the graphical representation of the scatterplot.
Attaching a dataframe is a nice way to be able to grab labels. It is always a good idea to detach labels from dataframes when we are done. We can do this by using the detach() command. detach(data17)
Let’s see once again what R has in its memory. ls()
## [1] "data17" "X" "X100" "X100a" "Z" "zdraw"
We can clean (i.e., erase) all objects from memory in R. rm(list=ls()) ls()
## character(0)
That covers some of the R basics you will use in this course. We recommmend that you consult this reference throughout the course.
8
- Basic Statistics & R
- Read Data