STAT 571A Advanced Statistical Regression Analysis. Introduction to R NOTES

Similar documents
Lecture 1: Getting Started and Data Basics

Yet Another R FAQ, or How I Learned to Stop Worrying and Love Computing 1. Roger Koenker CEMMAP and University of Illinois, Urbana-Champaign

An Introduction to R- Programming

Chapter 1 Introduction to using R with Mind on Statistics

IN-CLASS EXERCISE: INTRODUCTION TO R

Goals of this course. Crash Course in R. Getting Started with R. What is R? What is R? Getting you setup to use R under Windows

STAT 540 Computing in Statistics

Basic R Part 1 BTI Plant Bioinformatics Course

Getting Started with R

R Short Course Session 1

Instruction: Download and Install R and RStudio

Regression III: Advanced Methods

An Introduction to Using R

Introduction to R Commander

R basics workshop Sohee Kang

The Very Basics of the R Interpreter

STAT 113: R/RStudio Intro

Reading in data. Programming in R for Data Science Anders Stockmarr, Kasper Kristensen, Anders Nielsen

An Introduction to R 1.1 Getting started

Introduction to R & R Commander

Biology 345: Biometry Fall 2005 SONOMA STATE UNIVERSITY Lab Exercise 2 Working with data in Excel and exporting to JMP Introduction

R (and S, and S-Plus, another program based on S) is an interactive, interpretive, function language.

Statistics for Biologists: Practicals

Introduction to R. UCLA Statistical Consulting Center R Bootcamp. Irina Kukuyeva September 20, 2010

STAT 213: R/RStudio Intro

MBV4410/9410 Fall Bioinformatics for Molecular Biology. Introduction to R

Introduction to R Software

Module 1: Introduction RStudio

Programming R. Manuel J. A. Eugster. Chapter 1: Basic Vocabulary. Last modification on April 11, 2012

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

An Introduction to the R Commander

Barry Grant

TI-SmartView Emulator Software for the TI-84 Plus Family (Windows and Macintosh )

Student Success Guide

Introduction to R Cmdr

Description/History Objects/Language Description Commonly Used Basic Functions. More Specific Functionality Further Resources

the star lab introduction to R Day 2 Open R and RWinEdt should follow: we ll need that today.

Introduction to Statistics using R/Rstudio

Microsoft Excel 2007

POL 345: Quantitative Analysis and Politics

Use of Technology. Perform statistical computations on stored data or entered statistics.

Installing and running R

Getting started with Minitab 14 for Windows

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to R Benedikt Brors Dept. Intelligent Bioinformatics Systems German Cancer Research Center

ENGR 1181 MATLAB 05: Input and Output

Advanced Econometric Methods EMET3011/8014

Unit A451: Computer systems and programming. Section 3: Software 1 Intro to software

Tutorial (Unix Version)

Introduction to R. Dataset Basics. March 2018

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

R Website R Installation and Folder R Packages R Documentation R Search R Workspace Interface R Common and Important Basic Commands

Computers for Beginners. Tuesday, July 7, p.m. Instructor: Anne Swanson

Introduction to R. Introduction to Econometrics W

STA 250: Statistics Lab 1

Controlling the Control Palette

Lab 1: Getting started with R and RStudio Questions? or

General Guidelines: SAS Analyst

Spectroscopic Analysis: Peak Detector

Stat 302 Statistical Software and Its Applications Introduction to R

2. Getting started with MLwiN

MATLAB Project: Getting Started with MATLAB

TI-SmartView Guidebook for Macintosh

Week 1: Introduction to R, part 1

Computers for Seniors. Wed May 29 th 2014 Instructor: Wyatt Fertig

Introduction to MATLAB

A system for statistical analysis. Instructions for installing software. R, R-studio and the R-commander

STAT 20060: Statistics for Engineers. Statistical Programming with R

Stat 302 Statistical Software and Its Applications Introduction to R

Short Introduction to R

The R and R-commander software

GETTING STARTED WITH MINITAB INTRODUCTION TO MINITAB STATISTICAL SOFTWARE

1 Introduction to Using Excel Spreadsheets

Creating Word Outlines from Compendium on a Mac

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

2 The Stata user interface

Excel for Gen Chem General Chemistry Laboratory September 15, 2014

MATLAB Project: Getting Started with MATLAB

Introduction to R for Time Series Analysis Lecture Notes 2

Lab 1. Introduction to R & SAS. R is free, open-source software. Get it here:

Mails : ; Document version: 14/09/12

EGR 111 Introduction to MATLAB

Working with Mailbox Manager

EXCEL 2007 TIP SHEET. Dialog Box Launcher these allow you to access additional features associated with a specific Group of buttons within a Ribbon.

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Lab 1 Intro to MATLAB and FreeMat

An introduction to R WS 2013/2014

A Brief Introduction to R

Getting Started with MATLAB

LECTURE NOTES FOR ECO231 COMPUTER APPLICATIONS I. Part Two. Introduction to R Programming. RStudio. November Written by. N.

Getting PC SAS to Do What You Want, When You Want, How You Want Jodie Gilmore, Fulcrum Communications, Washougal, WA

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

Getting Started With UNIX Lab Exercises

limma: A brief introduction to R

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

EPIB Four Lecture Overview of R

Section 1: Numerical Calculations

Your Name: Section: INTRODUCTION TO STATISTICAL REASONING Computer Lab #4 Scatterplots and Regression

Instruction Of Computer Keyboard Shortcut Keys Windows 7 Ppt

Transcription:

STAT 571A Advanced Statistical Regression Analysis Introduction to R NOTES 2015 University of Arizona Statistics GIDP. All rights reserved, except where previous rights exist. No part of this material may be reproduced, stored in a retrieval system, or transmitted in any form or by any means electronic, online, mechanical, photoreproduction, recording, or scanning without the prior written consent of the course instructor.

Computer Implementation Regression analysis can often be performed by hand calculation, but this is almost always tedious (and sometimes difficult!). Instead, we turn to the computer. Available packages include SAS, SPSS, Stata, Minitab, etc. In STAT 571A, we focus on the (freeware) program R.

R The R language has many qualities: possibly the most powerful combination of flexibility and functionality in modern statistical computing highly effective graphics subsystem extensive user group creates external packages (we will appeal to some of these) it s free (!): download at http://cran.r-project.org

R Overview This short overview introduces R and a few of its features. Students are expected to develop skills with R separate from the course s core material. (Sorry, but this isn t an R class.) A recommended source is The R Guide by W.J. Owen (version 2.5): http://cran.r-project.org/doc/contrib/owen-therguide.pdf

Command-Line Driven One setback with R is that it uses command-line input. (Holdover from the 70 s...) So, very few point and click features. Can try the R-commander add-on for more of a GUI feel, if desired (see the Rcmdr package). Use at own risk.

What is R? R is an object-oriented statistical programming environment. (And, a useful tool for understanding statistical thinking...) After loading R on your computer, start R by double-clicking the R icon. This brings up the CONSOLE WINDOW within which the CURRENT WORKSPACE is accessed.

R Workspace To quite R, type > q() or use mouse clicks on the program GUI. Notice the > to represent the R input prompt. While in R, we essentially are manipulating the workspace with the data we enter and analyze.

Data Entry The simplest way to enter data in R is via the c() (for concatenate) command: > four.numbers <- c(7, 9, 14, pi) This assigns the four numbers {7, 9, 14, } to the object (here, a vector) four.numbers Notice the assignment operator: <- sort of like a left arrow can also use =

R commands R can act on an object in a variety of ways: > ls() #lists components of the workspace > four.numbers/2 #divide all elements by 2 > new.numbers = four.numbers+3 #add 3 to all elements & assign to new object > rm(new.numbers) #remove object It is recommended to keep the workspace clean of unneeded/old objects.

R help To get HELP in R, use > help(log) #get help on log function or just >?log Can also do keyword searching: > apropos( log ) #lists all functions with string log >??log

R Functions A function in R is an operation (or set thereof) that acts on other R objects. There are LOTS of R functions already inbuilt (and, you can always write your own; see Owen s RGuide, 8.3) E.g., matrix() creates matrices (see?matrix)

matrix() > a.matrix = matrix( c(1,2,3,4,5,6,7,8), nrow=2, ncol=4, byrow=false ) > print( a.matrix ) will create and display the 2x4 matrix: [,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8

Output To print output in R, you can: print directly from the R console with Print from the File menu (which prints everything in the window...) on Windows or MacOS copy portions of your work to a word processor use Courier font and edit/print from there (better option!) To save a workspace, choose Save Workspace from File menu (a good idea!)

A calculator on steroids... R can act as a basic calculator, with some extra features. Try these: > 2+3 > 2^3 > 4^2 3*2 > sqrt(2) > abs(2-4) > atan(4*pi) > log(0) > factorial(6) > sum(four.numbers) > prod(four.numbers) Can also do matrix arithmetic (Owen, 2.3)

Other Handy Functions > seq(1,9) #sequence from 1 to 9 > seq(1,5, by=.5) #increment by 0.5 > 1:9 #shortcut for seq(1,9) > 1.5:10 > rep(9, times=4) #repeat 9 four times > rep( c( A, B, C ), 2) #can do characters too > length( four.numbers) > round( pi, digits=4 ) #rounding

The I() function If you need to operate on a variable within an R formula, the basic arithmetic operations don t work. Need to use R s Inhibit Interpretation function, I(): > X = c(1.1, -2.2, 5.25, pi) > formula = ( Y ~ X + I(X^2) ) > I(X^2) [1] 1.2100 4.8400 27.5625 9.8696

Entering Data, Redux Other ways to enter data in R: Scan in keyboard input until 2 consecutive carriage returns are entered: > scan() Scan in from external data file via dialog window: > scan( file.choose() ) Scan in from external TXT file (notice forward slash): > scan( C:/datafile.txt )

Entering Data, Redux Read in formatted data (from TXT file): > read.table( file.choose(), header=true ) Read in comma-separated values (from TXT file): > read.csv( file.choose(), header=true ) Read in external R source code from TXT file (tricky; see CRAN online manuals): > source( file.choose() )

R packages To access the many useful (well, some are useful) external R packages: First download the package and install it on your hard drive. (May need to select a mirror site.) Do this only once per machine. use install.packages() command or use menu options Once installed, load the package every time you need it in your R session. use library() or require() or use menu options

R Graphics R s graphical subsystem is felt by some to be its strongest feature: high-quality outputs very flexible (if you known all the subcommands) easy to save as PDF, EPS, JPG, TIFF, etc., or just Print from File menu Default is to overwrite every new graphic, so plot device needs careful management. See Owen s RGuide ( 4).