DOC PREVIEW
UNL STAT 870 - Introduction to R

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to RThe R installation file for Windows can be downloaded from http://cran.r-project.org/bin/windows/base/. Select the “Download R 2.*.* for Windows” link. You can simply execute the file on your computer to install (all the installation defaults are o.k. to use). Basics of RThe R Console window is where commands are typed. 2012 Christopher R. BilderR Intro. 1The Console can be used like a calculator. Below are some examples:> 2+2[1] 4> qchisq(0.95,1)[1] 3.841459> pnorm(1.96)[1] 0.9750021> (2-3)/6[1] -0.1666667> 2^2[1] 4> sin(pi/2)[1] 1> cos(pi/2)[1] 6.123032e-17> log(1)[1] 0Results from these calculations can be stored in an object. The <- is used to make the assignment and is read as “gets”. > save<-2+2> save[1] 4The objects are stored in R’s database. When you closeR you will be asked if you would like to save or delete them. This is kind of like the SAS WORK library, but R gives you the choice to save them.  2012 Christopher R. BilderR Intro. 2To see a listing of the objects, you can do either of the following: > ls()[1] "save"> objects()[1] "save"To delete an object, use rm() and insert the object name in the parentheses. FunctionsR does various calculations using functions. For example, the qchisq() and the pnorm() commands used earlier are functions. Writing your own function is fairly simple. For example, suppose you want to write a function to calculate the standard deviation. Below is an example where 5 observations are saved to an object using the concatenate or combine function c(). A function called sd2() is written to find the standard deviation simply by using the square root of the variance. The sd2 object is now stored in the R database. > x<-c(1,2,3,4,5) > sd2<-function(numbers) { sqrt(var(numbers)) } 2012 Christopher R. BilderR Intro. 3> sd2(x)[1] 1.581139Note that there already is a function in R to calculate the standard deviation. It is sd(). When a function has multiple lines of code in it, the last line corresponds to the returned value. For example, > x<-c(1,2,3,4,5) > sd2<-function(numbers) { cat(“Print the data \n”, numbers) sqrt(var(numbers)) } > save<-sd2(x)Print the data 1 2 3 4 5 > save[1] 1.581139Note that the cat() function is used to print text and the\n character tells R to go to a new line. HelpTo see a listing of all R functions which are “built in”, open the Help by selecting HELP > HTML HELP from the main R menu bar.  2012 Christopher R. BilderR Intro. 4Under REFERENCE, select the link called PACKAGES. All built in R functions are stored in a package.  2012 Christopher R. BilderR Intro. 5We have been using functions from the base and stats package. By selecting stats, you can scroll down to find help on the pnorm() function. Note the full syntax for pnorm() is  2012 Christopher R. BilderR Intro. 6pnorm(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE)The q value corresponds to the 1.96 that was entered earlier. So > pnorm(1.96)[1] 0.9750021> pnorm(q=1.96)[1] 0.9750021> pnorm(q=1.96, mean=0, sd=1)[1] 0.9750021produce the same results. The other entries in the function have default values set. For example, R assumes you want to work with the standard normal distribution by assigning mean=0 and sd=1 (standard deviation). If you know the exact name of the function, simply type help(function name) at the R Console command prompt to bring up its help in a window inside of R. For example, > help(pnorm)brings up  2012 Christopher R. BilderR Intro. 7Using R functions on vectorsMany R functions are set up to work directly on vectors. For example, > pnorm(q = c(-1.96,1.96))[1] 0.02499790 0.97500210> qt(p = c(0.025, 0.975), df = 9) 2012 Christopher R. BilderR Intro. 8[1] -2.262157 2.262157The qt() function finds the 0.025 and 0.975 quantiles from a t-distribution with 9 degrees of freedom. As another example, suppose I want to find a 95% confidence interval for a population mean:> x<-c(3.6771004, -3.6250945, 0.8013501, 3.0265685, -9.8599757, -8.6644204, -2.3809956, 8.9420148, 0.5174142, 1.2531389)> x [1] 3.6771004 -3.6250945 0.8013501 3.0265685 -9.8599757 -8.6644204 [7] -2.3809956 8.9420148 0.5174142 1.2531389> mean(x) + qt(p = c(0.025, 0.975), df = length(x)-1) * sd(x)/sqrt(length(x))[1] -4.707622 3.445042> t.test(x = x, mu = 2, conf.level = 0.95) One Sample t-testdata: x t = -1.4602, df = 9, p-value = 0.1782alternative hypothesis: true mean is not equal to2 95 percent confidence interval: -4.707622 3.445042 sample estimates: mean of x -0.6312899  2012 Christopher R. BilderR Intro. 9In this case, a random sample of size 10 is taken from a population (actually the distribution was N(2, 52)) and put into an object called x. Notice how the calculations are done automatically even though the qt() function produces a vector with two elements in it. I checked my confidence interval calculation with the results from t.test(), which automatically calculates the confidence interval and does a hypothesis test for a specified mean (mu). Please be careful when intermixing vectors and scalar values when doing calculations like this so that unintended results do not occur.PackagesIf you want to use functions that are in other packages, you may need to install and then load the package into R. For example, we will be using the RODBC package later to read in Excel files containing our data. While in the R console, select PACKAGES > INSTALL PACKAGE(S) from the main menu. 2012 Christopher R. BilderR Intro. 10A number of locations aroundthe world will come up.Choose one close to you (Iusually choose USA(IA), whichis at Iowa State U.). Next, thelist of packages will appear.Select the RODBC packageand select OK. The package will now beinstalled onto your computer.This only needs to be doneonce on your computer. Toload the package into yourcurrent R session, type library(package =RODBC) at the R Consoleprompt. This needs to be done 2012 Christopher R. BilderR Intro. 11only once in an R session. If you close R and reopen, you will need to use the library() function again.CharactersObject names can include periods and underscores. Forexample, “mod.fit” could be a name of an object and it is often said as “mod dot fit”. R IS CASE SENSITIVE! 2012 Christopher R. BilderR Intro. 12Program editorsOften, you will have a long list of commands that you would like to execute all at once – i.e., a program.


View Full Document

UNL STAT 870 - Introduction to R

Download Introduction to R
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to R and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to R 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?