STAT 534: Statistical Computing Hari Narayanan

Slides:



Advertisements
Similar presentations
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Advertisements

R for Macroecology Aarhus University, Spring 2011.
Training on R For 3 rd and 4 th Year Honours Students, Dept. of Statistics, RU Empowered by Higher Education Quality Enhancement Project (HEQEP) Department.
Maths for Computer Graphics
Introduction to MATLAB Simon O’Keefe Non-Standard Computation Group
Introduction to MATLAB Week 13 – 4/21/09. Instructor: Kate Musgrave Time: Tuesdays 3-5pm Office Hours: Tuesdays 1:30-3pm
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Introduction to Exploratory Descriptive Data Analysis in S-Plus Jagdish S. Gangolly State University of New York at Albany.
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
How to Use the R Programming Language for Statistical Analyses Part I: An Introduction to R Jennifer Urbano Blackford, Ph.D. Department of Psychiatry Kennedy.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Training on R-language Mārtiņš Liberts Central Statistical Bureau of Latvia.
LISA Short Course Series Basics of R Lin Zhang Feb. 16, 2015 LISA: Basics of RFeb. 16, 2015.
Basic R Programming for Life Science Undergraduate Students Introductory Workshop (Session 1) 1.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
FOR 520: Geospatial data analysis with Matlab
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB
732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of.
Data, graphics, and programming in R 28.1, 30.1, Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Piotr Wolski Introduction to R. Topics What is R? Sample session How to install R? Minimum you have to know to work in R Data objects in R and how to.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Installing R CRAN: –(R homepage: –Windows 95 and later  Base –rw2001.exe.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to R. Why use R Its FREE!!! And powerful, fairly widely used, lots of online posts about it Uses S -> an object oriented programing language.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
STAT 534: Statistical Computing Hari Narayanan
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
STAT 251 Lab 1. Outline Lab Accounts Introduction to R.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Exploratory Descriptive Data Analysis in S-Plus Jagdish S. Gangolly State University of New York at Albany.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
PROGRAMMING IN R Introduction to R. In this session I will: Introduce you to the R program and windows Show how to install R Write basic programs in R.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Intro to Matlab Yipei Wang & Qihui Li {yipeiw,
STAT 534: Statistical Computing
Lecture 11 Introduction to R and Accessing USGS Data from Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National.
Introduction to R Chris Free. Introduction to R Free! Superior (if not comparable) to commercial alternatives Available on all platforms Not just for.
16BIT IITR Data Collection Module If you have not already done so, download and install R from download.
Working with data in R 2 Fish 552: Lecture 3. Recommended Reading An Introduction to R (R Development Core Team) –
1 Matrix Math ©Anthony Steed Overview n To revise Vectors Matrices.
Sihua Peng, PhD Shanghai Ocean University
ECE 1304 Introduction to Electrical and Computer Engineering
Programming in R Intro, data and programming structures
R basics workshop Sohee Kang Math and Stats Learning Centre
Introduction to R Samal Dharmarathna.
Introduction to Matlab
L – Modeling and Simulating Social Systems with MATLAB
Naomi Altman Department of Statistics (Based on notes by J. Lee)
Matrix Multiplication
Introduction to Programming for Mechanical Engineers (ME 319)
R basics workshop Sohee Kang Math and Stats Learning Centre
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Statistics 540 Computing in Statistics
INTRODUCTION TO MATLAB
Introduction to MATLAB
Experiment No. (1) - an introduction to MATLAB
R Course 1st Lecture.
Matlab Training Session 2: Matrix Operations and Relational Operators
Presentation transcript:

STAT 534: Statistical Computing Hari Narayanan

Course objectives Write programs in R and C tailored to specifics of statistics problems you want to solve Familiarize yourself with: – optimization techniques – Markov Chain Monte Carlo (mcmc)

Logistics Class: – Tuesdays and Thursdays 12:00pm – 1:20pm Office hours: – Thursday 2:30pm – 4pm (Padelford B-301) or by appt Textbooks: – Robert & Casella : Introducing Monte Carlo Methods with R – Kernighan & Ritchie : C Programming Language Evaluation: – 4 assignments – 2 quizzes – Final project

Introduction to R R is a scripting language for statistical data manipulation and analysis R is the successor of S/S Plus R standard for professional statisticians R is free and available on major platforms (Windows, Unix, Mac) It is: general, object oriented It is an interpreted programming language

Getting R Main website: ~25 standard packages come with a default download, many more contributed packages can be obtained from the main website Development environment/GUI: – Rstudio

First R interactive session Type interactive commands at the prompt > > 2==4 FALSE > 5/0 Inf > 0/0 NaN Note that R is case sensitive Getting help – help(FALSE) – ?FALSE Ending session: – >q()

R workspaces R creates and manipulates objects: variables, arrays of numbers, list of character, functions, structures build from these components: > a = 4 > b = 5 > objects() # list all the objects in this workspace [1] "a" "b" > ls() # same as objects() [1] "a" "b" > rm(a) # remove an object from this workspace > ls() [1] "b“ Objects of the current session are stored in.Rdata in the current folder and command history is stored in.Rhistory – These are reloaded every time you start R from the same directory

Assignment Multiple ways to assign values [ primitive values or results of the evaluation of an expression] to variables: > a = > a < > > a > assign(“a”, 2+3)

Vectors Created using the c (concatenation) function: > v = c(1,2) > v [1] 1 2 A number by itself is considered a vector of length 1 No nesting > u=c(-4, v, 4) > u [1] Missing values c(1, NA, 4)

Operations on vectors Regular arithmetic operations apply (+, -, *, /, ^). Shorter vector is recycled to match needed length: > a=c(1,2) # becomes > b=c(1,2,3) > r=3*a+b-1 Warning message: In 3 * a + b : longer object length is not a multiple of shorter object length > r [1] Other math functions can be applied element wise : sqrt, log,.. Other functions: max, min, length, sum, prod, mean, var, sort > sort(c(4,3,7)) [1] 3 4 7

Logical operations Operators, >=, ==, &, |, ! > a=c(2,4) > r1=a>3 [1] FALSE TRUE > r2=a>4 [1] FALSE FALSE > r1 & r2 [1] FALSE FALSE > r1 | r2 [1] FALSE TRUE > ! r1 [1] TRUE FALSE Can be used in arithmetic operations, FALSE coerced to 0, TRUE to 1 > r1 + 1 [1] 1 2 > c(2,3) & c(0,1) [1] FALSE TRUE

Generating vectors : operator (high precedence in an expression) > a=3 > 1:a [1] > 1:a+1 [1] > 1:(a+1) [1] seq function > seq(from=2, to=4) # named arguments same as seq(2,4) [1] > seq(to=4, from=2) [1] > seq(from=2, length=3) [1] rep function > a=c(1,2) > rep(a, times=2) [1] > rep(a, each=2) [1]

Manipulating vector data Simple indexing: > a=c(2,3,8) > a[1] [1] 2 > a[5] [1] NA > a[-1] [1] 3 8 More complex: > a[1:2] [1] 2 3 > a[a>2 & a<7] [1] 3 > a[c(1,1)] [1] 2 2

Matrices Associating a dimension vector with a vector allows it to be treated by R as an array/matrix: > a=c(2,3,8) > attributes(a) NULL > dim(a) = c(3,1) > a [,1] [1,] 2 [2,] 3 [3,] 8 > attributes(a) $dim [1] 3 1 > matrix(c(1,2,3,4,5,6), nrow=2) [,1] [,2] [,3] [1,] [2,] > matrix(c(1,2,3,4,5,6), nrow=2, byrow=TRUE) [,1] [,2] [,3] [1,] [2,] 4 5 6

Operations on matrices Addition/subtraction/element-wise multiplication : +,-,* Matrix multiplication : %*% Transpose : function t() e.g. t(matrix(c(1,2),nrow=1)) diag function: – If argument is a number we get identity matrix > diag(2) [,1] [,2] [1,] 1 0 [2,] 0 1 – If argument is a vector, we get diag matrix with elements of vector > diag(c(1,2)) [,1] [,2] [1,] 1 0 [2,] 0 2 – If argument is a matrix, we get the elements of its major diagonal > m [,1] [,2] [1,] 3 5 [2,] 4 6 > diag(m) [1] 3 6

Indexing Similar to indexing vectors, except we have an indexing vector for every dimension: > m [,1] [,2] [,3] [1,] [2,] > m[2,2] [1] 4 > m[c(2),c(2)] # indexing vectors [1] 4 > m[c(1,2),c(2)] # first 2 rows and 2 nd column [1] 3 4 > m[c(1,2),c(2,3)] [,1] [,2] [1,] 3 5 [2,] 4 6 > m[c(1,2),c(2,3)]=0 > m [,1] [,2] [,3] [1,] [2,] > m[c(TRUE,FALSE),TRUE] # keep first line and all columns [1] 1 0 0