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.

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.
Lecture 5.
Introduction to MATLAB The language of Technical Computing.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to UNIX Working in a multi-user environment.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Mx? A programming language for scientific computation. Related Languages: Matlab IDL Maple, Mathcad, Mathematica.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
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.
Lecture 2 LISAM. Statistical software.. LISAM What is LISAM? Social network for Creating personal pages Creating courses  Storing course materials (lectures,
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.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
The R Language How to find scientific truths buried between the spots.
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.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Introduction to MATLAB. Windows in MATLAB Command Window – where you enter data, run MATLAB code, and display results Command History - displays a log.
Introduction to to R Emily Kalah Gade University of Washington Credit to Kristin Siebel for development of much of this PowerPoint.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
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.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
INTRODUCTION TO MATLAB LAB# 01
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Using the R software R is an open source comprehensive statistical package, more and more used around the world. R project web site:
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to R Introductions What is R? RStudio Layout Summary Statistics Your First R Graph 17 September 2014 Sherubtse Training.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
STAT 534: Statistical Computing Hari Narayanan
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Intro to Matlab Yipei Wang & Qihui Li {yipeiw,
Chris Knight Beginners’ workshop.
Lecture 11 Introduction to R and Accessing USGS Data from Web Services Jeffery S. Horsburgh Hydroinformatics Fall 2013 This work was funded by National.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
16BIT IITR Data Collection Module If you have not already done so, download and install R from download.
Programming in R Intro, data and programming structures
R programming language
LISAM. Statistical software.
L – Modeling and Simulating Social Systems with MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Communication and Coding Theory Lab(CS491)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
R Course 1st Lecture.
Matlab Basics Tutorial
Presentation transcript:

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 reports-.  One written final exam (computer)  Course book: R Cookbook, by Paul TeetorR Cookbook ◦ knowledge of R at the advanced level: The Art of R Programming by Norman Matloff.The Art of R Programming  Lab schedule: A44 Programming in R

 R is an open source comprehensive statistical package, more and more used around the world.  R is a real programming language (compare to SAS or Minitab)  R is not low-level language as C or Visual Basic  Can be compared to Matlab by structure, by origin – free version of S + Has a lot of contributed packages covering a lot of statistical methods (also recent ones!) - Documentation is badly structured 732A44 Programming in R

Installing the R software R project web site: Find the mirror nearest to you when downloading 732A44 Programming in R

Start  (All )Program  R  R A44 Programming in R

Notepad++: choose ”Language”  ”R” 732A44 Programming in R

 Specific function ◦ help(function)  Help browser ◦ help.start()  Search for something in help ◦ help.search(“expression”)  Quick reminder of function arguments: ◦ args(function)  Examples of how to use function: ◦ example(function)  If some method is not installed on the computer: ◦ RSiteSearch(”expression") 732A44 Programming in R

 R is case-sensitive  Separate commands by semicolon (;)  Comments: #R is a very cool language! Data assignment: Use -> or <- a<-3; 3->b; 732A44 Programming in R

 Assign a vector 732A44 Programming in R The function c() combines individual values (comma-spaced) to a vector Printing the value on screen: Either enter the variable or use the function print() Note that the output begins with [1]. This is the row number, and in this case x is interpreted as a row vector

732A44 Programming in R Listing defined objects (vectors, matrices, data frames): Use the function ls() with no arguments Removing objects: Use the function rm()

 Use either ‘: ‘ or seq() 732A44 Programming in R

Important: In R, operations with vectors are performed element- by-element Some operations:  Element-wise: +-*/^  log exp sin cos sqrt  length –number of elements  sum - sum of all elements  mean  max min order Logicals: TRUE or FALSE: a<-TRUE; > >= < <= != & (and) | (or) 732A44 Programming in R

Use the function matrix() a<-matrix( values,nrow= m,ncol= n ) values is a list of values enclosed in c(), i.e. a row vector or an already defined vector. m is the number of rows and n is the number of columns of the matrix. The number of values must be dividable by both m and n. The values are entered column-wise. The identifiers nrow= and ncol= can be omitted Note the double indexing, first number for row and second number for column 732A44 Programming in R Working with matrices

 Positive integral indexing x[1,6] x[2:10]  Negative integral indexing x[-(1:5)] all except elements 1:5  Indexing entire row or column x[2,] entire row 2  Finding elements satisfying specific condition: ◦ x[x>mean(x)] 732A44 Programming in R

 Matrix operators/functions:  transpose b=t(a) b = a T  Inverse b=solve(a) b = a -1 (when needed) 732A44 Programming in R

 List is a collection of objects 732A44 Programming in R

Collecting vectors and matrices with the same number of rows in a data frame Use the function data.frame( object 1, object 2, …, object k ) Matrices need to be protected, otherwise each column of a matrix will be identified as a single object in the data frame. Protection is made with the function I() 732A44 Programming in R Data frames

Objects within a data frame can be called upon using the syntax dataframe $ object 732A44 Programming in R Data frames

 Vectors, lists, data frames can be entered in R: ◦ edit(variable)  Example: myframe<-data.frame(); edit(myframe); 732A44 Programming in R

Names of objects within a data frame can be called, set or changed by handling the object names() 732A44 Programming in R Data frames

1. Save file as comma-separated file (csv) 2. Set current working directory: ◦ setwd(directory) 3. Use read.csv2(filename) Example: setwd("Z:/732A44"); mydata<-read.csv2("Counties.csv"); 732A44 Programming in R

if (expr) { … } else{ … } If you need to connect several conditions, use ’&’, ’&&’, ’| ’ or ’||’ 732A44 Programming in R

for (name in expr1 ) { … } while (condition) { … } 732A44 Programming in R

 Function writing must always end with writing the value which should be returned!  You may also use ”return(value)” to show what value the function should return 732A44 Programming in R

 If several arguments need to be returned, list may be used 732A44 Programming in R

 Obligatory arguments and arguments by default  Variables can be specified in any order when you call the function 732A44 Programming in R

 plot(x,..) plots time series  plot(x,y) scatter plot  plot(x,y) followed by points(x,y) plots several scatterplots in one coordinate system  hist(x,..) plots a hitogram  persp(x,y,z,…) creates surface plots 732A44 Programming in R

 99% of all moderate-size codes contain mistakes in the first version  Often not easy to find mistake  debug Way 1: debug a function step-by-step from the beginning:  Use debug(function)  Use undebug(function) Example: Find a step when … becomes equal to 500 (find mistake) ! 732A44 Programming in R myfun<-function(x) { i<-1; r<-0; while (r!=x) { r<-r+i; i<-i*2; } return(i); } debug(myfun); a<-myfun(500); #after some steps undebug(myfun);

Things to do in debug mode  n or : run next line  c : continue until you exit from the loop or from the function  Any R command, for ex. find out the value of a variable  where : prints stack trace  Q : quit the debugger 732A44 Programming in R

Way 2: insert browser() in the suspicious place in the code: 732A44 Programming in R myfun<-function(x) { i<-1; r<-0; while (r!=x) { r<-r+i; if(r>500) browser(); i<-i*2; } return(i); }

 Solving a linear system of equations 732A44 Programming in R

Regression model

732A44 Programming in R Use help(”lm”) to learn more about this function

732A44 Programming in R

Huge more to find out! Read course books Use PDF manuals Use the help function help(” function ” ) or ? function Use Google (search for “R: what you are looking for”) 732A44 Programming in R