### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# ANLYS STATS 0153

UCLA

GPA 3.67

### View Full Document

## 81

## 0

## Popular in Course

## Popular in Statistics

This 4 page Class Notes was uploaded by Isobel Stanton on Friday September 4, 2015. The Class Notes belongs to STATS 0153 at University of California - Los Angeles taught by A. Yuille in Fall. Since its upload, it has received 81 views. For similar materials see /class/177954/stats-0153-university-of-california-los-angeles in Statistics at University of California - Los Angeles.

## Similar to STATS 0153 at UCLA

## Popular in Statistics

## Reviews for ANLYS

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/04/15

STATISTICS 153 R TUTORIAL Instructor Prof AILI Yuille Fall 2005 Invoking R in the Boelter Hall 9413 lab The Boelter Hall lab has R installed Survival Kit a To exit R7 type b To start a help window7 type helplstartOI c To get text only help on a command7 type helpcommand or 7command d To start a graphics window7 type XllOI e To close a graphics window7 type devloffol f To get help on a function eg cor 7 type helpcorI Vectors The simplest data type of R is vector A scalar is just a vector with length 1 The following examples show how to create vectorsl gt c1 3 5 9 1 1 3 5 9 gt 110 112345678910 gt seq1 2 01 110 11 12 13 14 15 16 17 18 19 20 gt rep2 4 1 2 2 2 2 gt rep23 2 1 2 3 2 3 gt 1ength39 1 7 Expressions You can use R to calculate any expression you typed R will evaluate it7 the result will be printed7 and then discarded For arithmetical operations7 if the two operands are not of the same length7 the shorter vector is recycled as often as need to match the length of the longer vectorl For example7 gt 14 2 1 3 4 5 6 gt 14 2 1 05 10 15 20 gt 23 23 note exponentiation has higher precedence 1 4 27 gt logc2 3 1 06931472 10986123 gt sqrt4 1 2 Assignments An assignment evaluates an expression and passes the value to a variable but the result is not printed Assignments are indicated by the assignment operators 77lt77 and 7773 They operate the same For example gt x lt c135789 gt X 1 1 3 5 7 8 9 Vector and Matrix indexing In R the subscripts of vectors and matrices start from 1 A negative index means all the elements in the vector except it If the index is out of bound the result is an NA NA is the value R uses for a missing or unde ned value gt x lt c135789 gt x3 1 1 5 7 8 9 gt x10 1 NA You can create a matrix With the cbind rbind and matrix functions cbind binds vectors together in columns rbind binds vectors together in rows and matrix lls a matrix With the elements of a vector gt K lt cbindx y16 1 2 3 4 5 6 gt Y lt matrix023 gt Y 1 2 3 1 O O O 2 O O O gt Y lt matrixx23 gt Y 1 2 3 1 1 5 8 2 3 7 9 gt Y12 extract element on the first row and the second column 1 5 gt Y1 extract the first row 1 1 5 8 gt Y1 extract the first column gt Y2c13 of row 2 extract elements 13 1 3 9 To multiply two matrices together use the operator t is the matrix transpose operator and function solve can be used to inverse matrices or solve linear systems gt X Y gt tx gt solveY12 Listing Removing objects for storage All top level assignments are nonpermanent until they are saved using the saveiimagequot lenameRDataquot command With the lename between quotation marks otherwise it Will search for an object With the name lenamei The extension lliRData77 is not added by default To list all objects in the iData directory use lsi To remove an object from the list use rmi To load an RData le using the load command gt ls 1 quotXquot quotYquot quotlastwarningquot quotxquot gt rmX gt ls 1 quotYquot quotlastwarningquot quotxquot gt q All will be lost unless saved prior to quitting Save workspace image ync Input and output functions The function readitable is useful for reading data from an external ascii le into R and storing it in a data frame A data frame is both a matrix and a list of column vectors WriteitableX 77 lepath Writes X into a le scan lepath reads a le into a vector See the help les on these functions for more details Example gt writetableX quotfooquot gt readtablequotfooquot sepquotquot mt separator is comma gt scanquotfooquot sepquotquot skip1 mt skip the first line Sort rank order and permutation The sort and order commands are helpful gt a lt c4 7 1 9 5 gt sorta 1 1 4 5 7 9 gt ordera 1 3 1 5 2 4 The sample command is crucial for bootstrappingi Function samplex size replaceFALSE produces a vector of random partial permutation of the vector x Where size is the length of the output vectori If the argument size is omitted a random full permutation of vector x is returned If replace is set to TRUE a bootstrap sample is obtained For example gt sample110 143102856197 gt sampleareplaceT 1 7 7 7 7 5 gt samplearT t for short The interpretation of r is unambiguous 1 4 9 1 4 9 gt samplea5T mt Also possible because size argument was added 117549 Graphics output R provides comprehensive graphics facilities Most frequently used tools probably Will be scatter plots and histograms By default plot function produces scatter plots You can change the graph style to line plot by providing argument type 77173 You can have both points and lines by providing argument type 77b There are a Whole bunch of options you can specify in the plot function a lt rnorm20 b lt rnorm20 plota plota typequot1quot plota typequotbquot plotsorta sortb typequot1quot gt plota b mainquotLine Plotquot x1abquotXquot ylabquotYquot gt hista VVVVVV Saving a plot to PDF le requires opening the le using the pdf command plotting the graph again and closing the le For example gt pdf f i1equothistogrampdfquot encodingquotMacRomanquot gt hista gt dev off X11 2 Another useful function is par Which enable you to set or ask about graphics parametersl Calling parmfrowc22 Will divide the graphics Window into 4 cells 2 rows and 2 columns it is handy if you want to put more plots on one page To restore to 1 cell setting use parmfrowclll Writing your own functions Technically R is a function language As you have seen it has a lot of builtin functions but you Will soon come upon situations Where you want to Write one of your own Here is a simple example stddev lt functionx it Input a vector x it Output the standard deviation of x it returnsqrt varx The function takes one argument a vector X and returns a scalar The lines beginning With are comments To invoke the function on a vector X you can type stdldevxl To see the commands Which make up the function just type stdldev Without any brackets You can Write a function With multiple outputs as shown belowl meanstdev lt functionx m lt meanx stdev lt sqrtvarx return1istmeanm stdevstdev gt meanstdev15 mean 1 3 stdev 1 1581139

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

#### "I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.