Syntax to define functions. The value returned by a function is the value of the function body, Syntax to call functions. myfct(arg1=..., arg2=...

Size: px
Start display at page:

Download "Syntax to define functions. The value returned by a function is the value of the function body, Syntax to call functions. myfct(arg1=..., arg2=..."

Transcription

1 R 프로그래밍 II

2 Function: Definition A very useful feature of the R environment is the possibility to expand existing functions and to easily write custom functions. In fact, most of the R software can be viewed as a series of R functions. Syntax to define functions myfct <- function(arg1, arg2,...) { function_body } The value returned by a function is the value of the function body, which is usually an unassigned final expression, e.g.: g:return() Syntax to call functions myfct(arg1=..., arg2=...)

3 Function: Syntax Rules General Functions are defined by (1) assignment with the keyword 'function', (2) the declaration of arguments/variables (arg1, arg2,...) and (3) the definition of operations (function_body) that perform computations on the provided arguments. A function name ('myfct') needs to be assigned to call the function (see below). Naming Function names can be almost anything. However, the usage of names of existing functions should be avoided. Arguments It is often useful to provide default values for arguments (e.g.: 'arg1=1:10'). This way they don't need to be provided in a function call. The argument list can also be left empty ('myfct <- function() { fct_body }') when a function is expected to return always the same value(s). The argument '...' can be used to allow one function to pass on argument settings to another. Function body The actual expressions (commands/operations) are defined in the function body which should be enclosed by braces. The individual commands are separated by semicolons or new lines (preferred). Calling functions Functions are called by their name followed by parentheses containing possible argument names. Empty parenthesis after the function name will result in an error message when a function requires certain arguments to be provided by the user. The function name alone will print the definition of a function. Scope Variables created inside a function exist only for the life time of a function. Thus, they are not accessible outside of the function. To force variables in functions to exist globally, one can use this special assignment operator: '<<-'. If a global variable is used in a function, then the global variable will be masked only within the function.

4 Example: Function Basics myfct <- function(x1, x2=5) { z1 <- x1/x1; z2 <- x2*x2 myvec <- c(z1, z2) return(myvec) } myfct # prints definition of function myfct(x1=2, x2=5) # applies function to values 2 and 5 myfct(2, 5) # the argument names are not necessary, but then the order of the specified values becomes important myfct(x1=2) # does the same as before but the default value '5' is myfct(x1=2) # does the same as before, but the default value 5 is used in this case

5 Example: Function with Optional Arguments myfct2 <- function(x1=5, opt_arg) { if(missing(opt_arg)) { z1 <- 1:10 } else { z1 <- opt_arg } cat("my function returns:", "\n") ") return(z1/x1) } myfct2(x1=5) myfct2(x1=5, opt_arg=30:20)

6 Control Utilities for Functions Return The evaluation flow of a function may be terminated at any stage with the 'return()' function. This is often used in combination with conditional evaluations. Stop To stop the action of a function and print an error message, one can use the stop() function. Warning To print a warning message in unexpected situations i without aborting the evaluation flow of a function, one can use the function 'warning("...")'.

7 Example: Control Utilities myfct <- function(x1) ){ if (x1>=0) print(x1) else stop("this function did not finish, i because x1 < 0") warning("value needs to be > 0") } myfct(x1=2) myfct(x1=-2)

8 Matrix Algebra

9 Introduction > A = matrix(1:12, nrow=3, ncol=4) > A [,1] [,2] [,3] [,4] [1,] [2,] [3,] > diag(a) [] [1] > a <- c(1,2,3) > b <- c(1,2,3,4) > c <- 5 > a [1] > b [] [1] > c [1] 5 #Case-sensitive, but not necessary uppercase or lowerrcase for matrix and vector

10 Basic Operation > #Basic operation > A = matrix(1:16, nrow=4, ncol=4) > B = matrix(16:1, nrow=4, ncol=4) > A [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] > t(a) #transpose [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] > sum(diag(a)) #trace [1] 34

11 Basic Operation > A + B [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] > A - B [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,]

12 Basic Operation > A * B [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] > A %*% B [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,]

13 Basic Operation > a <- c(1,2,3,4) > b <- c(2,4,7,9) > a %*% A [,1] [,2] [,3] [,4] [1,] > a * b [1] > a %*% % b [,1] [1,] 67 > t(a) %*% b [,1] [1,] 67 > 2*A [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,]

14 Determinant/Inverse Matrix > A = matrix(1:4, nrow=2) #Determinat > A [,1] [,2] [1,] 1 3 [2,] 2 4 > det(a) [1] -2 >#Inverse matrix >#Inverse matrix > solve(a) [,1] [,2] [1,] [2,] 1-0.5

15 Matrix and Linear Equation > A = matrix(c(2,3,3,5), nrow=2, ncol=2) > A [,1] [,2] [1,] 2 3 [2,] 3 5 > b = c(7,11) > b [1] 7 11 > solve(a, b) [1] 2 1

16 Eigen Value and Vector > A = matrix(c(5,25,35,25,155,175,35,175,325), ( ncol=3) > A [,1] [,2] [,3] [1,] [2,] [3,] > EA = eigen(a, symmetric=t) > EA $values [1] $vectors [,1] [,2] [,3] [1,] [2,] [3,]

17 Solving Linear and Nonlinear Equations

18 Solving Linear Equations Using Gauss Elimination > A = matrix(c(4, ( 2, 1, -2, -1, 3, 3, 1, -1), ncol=3) > A [,1] [,2] [,3] [1,] [2,] [3,] > b=c(9, 3, 4) > b [1] > solve(a) [,1] [,2] [,3] [1,] [2,] [3,] > solve(a,b) [1] [] 1 2 3

19 Choleski Decomposition > X=matrix(c(1,1,1,1,1,2,9,6,3,5,6,10,4,13,2), ncol=3) > X [1] [,1] [2] [,2] [3] [,3] [1,] [2,] [3,] [4,] [5,] > XX=t(X) %*% X > XX [,1] [,2] [,3] [1,] [2,] [3,] > G = chol(xx) > G [,1] [,2] [,3] [1,] e+01 [2,] e-16 [3,] e+00 > t(g) %*% G [,1] [,2] [,3] [1,] [2,] [3,]

20 QR Decomposition > X=matrix(c(1,1,1,1,1,2,9,6,3,5,6,10,4,13,2), ncol=3) > X [,1] [,2] [,3] [1,] [2,] [3,] [4,] [5,] > y=c(62,60,57,48,23) > qrx=qr(x)

21 QR Decomposition > qr.q(qrx) [,1] [,2] [,3] [1,] e [2,] e [3,] e [4,] e [5,] e > qr.r(qrx) [,1] [,2] [,3] [1,] e+01 [2,] e-16 [3,] e+00 > t(qr.q(qrx)) %*% qr.q(qrx) [,1] [,2] [,3] [1,] [,] [2,] [3,] > qr.q(qrx) %*% qr.r(qrx) [,1] [,2] [,3] [1,] [2,] [3,] [4,] [5,] 1 5 2

22 Application to Regression Modeling > b=solve(t(x) %*% % X) %*% % t(x) %*% % y > b [,1] [1,] 37.0 [2,] 0.5 [3,] 1.5 > yhat = X %*% solve(t(x) %*% X) %*% t(x) %*% y > yhat [,1] [1,] 47.0 [2,] 56.5 [3,] 46.0 [4,] 58.0 [5,] 42.5 > e=(diag(length(y))-x %*% solve(t(x) %*% X) %*% t(x)) %*% y > e [,1] [1,] 15.0 [2,] 3.5 [3,] 11.0 [4,] [5,] -19.5

23 Bisection Method Bisection = function(x0, x1, epsilon = 1.0e-5) { fx0 = f(x0) fx1 = f(x1) if(fx0*fx1 >0) return("wrong initial iti values") error = abs(x1-x0) N = 1 while(error > epsilon) { N = N + 1 error = error /2 x2 = (x0+x1)/2 fx2=f(x2) if(fx0*fx2 < 0) { x1 = x2 fx1 = fx2 } else { x0 = x2 fx0 = fx2 } } return(list(x=x2, x2 n=n)) }

24 Bisection Method > f = function(x){x^2-3} > Bisection(1,2) $x [1] $n [1] 18

25 Newton-Raphson Method Newton=function(x0, epsilon=1.0e-5, n=100) { e = 1 N = 1 d = epsilon while(e > epsilon) { N = N + 1 if(n > n) return("not converge after 100 iterations") x1 = x0 - f(x0) * d / (f(x0+d) - f(x0)) } e = abs(x1-x0) x0 = x1 } return(list(x=x1, n=n))

26 Newton-Raphson Method > f = function(x){x^2-3} > Newton(1) $x [1] $n [1] 6 > Newton(0) $x [1] $n [1] 24

27 Exercises

28 Loops ## Create a sample matrix myma <- matrix(rnorm(500), 100, 5, dimnames=list(1:100, paste("c", 1:5, sep=""))) ## (1.1) Compute the mean of each row in myma by applying the mean function in a for loop myve_for <- NULL for(i in seq(along=myma[,1])) { myve_for <- c(myve_for, mean(as.numeric(myma[i, ]))) } myresult <- cbind(myma, mean_for=myve_for) myresult[1:4, ] ## (1.2) Compute the mean of each row in myma by applying the mean function in a while loop z <- 1 myve_while <- NULL while(z <= length(myma[,1])) { myve_while <- c(myve_while, mean(as.numeric(myma[z, ]))) z <- z + 1 } myresult <- cbind(myma, mean_for=myve_for, mean_while=myve_while) myresult[1:4, ] ## (1.3) How can one confirm that the results from both mean calculations are identical? ## (1.3) How can one confirm that the results from both mean calculations are identical? sum(myresult[,6]!= myresult[,7])

29 Loops ## (1.4) Compute the mean of each row in myma by applying the mean function in an apply loop myve_apply <- apply(myma, 1, mean) myresult <- cbind(myma, mean_for=myve_for, mean_while=myve_while, mean_apply=myve_apply) myresult[1:4, ] ## (1.5) Compute the row means of myma by taking advantage of the built-in looping behavior ## of the mean() function. This will require the transpose of myma and its conversion into a data frame. mymean <- mean(as.data.frame(t(myma))) myresult <- cbind(myma, mean_for=myve_for, mean_while=myve_while, mean_apply=myve_apply, mean_int=mymean) myresult[1:4, ] ## (1.6) How can one confirm that the values from all four mean calculations are identical? ## The unique() and length() functions can be used for this step. myq <- apply(myresult[,6:9], 1, function(x) length(unique(x))) sum(myq!= 1) ## (1.7) Replace one mean value in the last column by '0' and repeat the identity check in step (1.6) myresult[1 8] < 0 myresult[1,8] <- 0 myq <- apply(myresult[,6:9], 1, function(x) length(unique(x))) sum(myq!= 1)

30 Functions ## (2.2) Use the following code as basis to implement a function that allows the user ## to compute the mean for any combination of columns in a matrix or data frame. ## One argument of this function should specify the input data set and another should ## allow the selection of the columns by providing a grouping vector (factor). myma <- matrix(rnorm(100000), 10000, 10, dimnames=list(1:10000, paste("c", 1:10, sep=""))) mylist <- tapply(colnames(myma), c(1,1,1,2,2,2,3,3,4,4), list) names(mylist) <- sapply(mylist, paste, collapse="_") mymamean <- sapply(mylist, function(x) apply(myma[,x], 1, mean)) mymamean[1:4,] mymacomp <- function(myma=myma, group=c(1,1,1,2,2,2,3,3,4,4), myfct=mean) { mylist <- tapply(colnames(myma), group, list) names(mylist) <- sapply(mylist, paste, collapse="_") mymamean <- sapply(mylist, function(x) apply(myma[,x], 1, myfct)) return(mymamean) } mymacomp(myma=myma, group=c(1,1,1,2,2,2,3,3,4,4), myfct=mean)[1:4,]

31 Improving Speed Performance by Avoiding Loops ## (3.1) Implement a speed improved version of your previous function from step (2.2). ## Use the following code for its implementation. In this code the R loop over the ## data intensive dimension is avoided by using the rowmeans() function. mylist <- tapply(colnames(myma), c(1,1,1,2,2,2,3,3,4,4), list) mymamean <- sapply(mylist, function(x) rowmeans(myma[,x])) colnames(mymamean) <- sapply(mylist, paste, collapse="_") mymamean[1:4,] ## (3.2) Compare the speed performance of the implementation from step 2.2 with the ## speed improved version from step (3.1) using the system.time() function.

32 Looping over lists with lapply/sapply ## Create a sample list populated with character vectors of different lengths setlist <- lapply(11:30, function(x) sample(letters, x, replace=true)) names(setlist) <- paste("s", seq(along=setlist), setlist), sep="") ## (4.1) Compute the length for all pairwise intersects of the vectors stored in 'setlist'. ## The intersects can be determined with the '%in%' function like this: ## sum(setlist[[1]] %in% setlist[[2]]) setlist <- sapply(setlist, unique) olma <- sapply(names(setlist), function(x) sapply(names(setlist), function(y) sum(setlist[[x]] %in% setlist[[y]]))) olma ## (4.2) Plot the resulting intersect matrix as heat map ## The heatmap.2() function from the gplots library can be used for this. library("gplots") heatmap.2(olma, trace= "none", Colv= "none", Rowv= "none", dendrogram= "none", col=colorpanel(40, colorpanel(40 "darkred", "orange", "yellow"))

Matrix algebra. Basics

Matrix algebra. Basics Matrix.1 Matrix algebra Matrix algebra is very prevalently used in Statistics because it provides representations of models and computations in a much simpler manner than without its use. The purpose of

More information

CMAT Language - Language Reference Manual COMS 4115

CMAT Language - Language Reference Manual COMS 4115 CMAT Language - Language Reference Manual COMS 4115 Language Guru: Michael Berkowitz (meb2235) Project Manager: Frank Cabada (fc2452) System Architect: Marissa Ojeda (mgo2111) Tester: Daniel Rojas (dhr2119)

More information

STATISTICS 579 R Tutorial: More on Writing Functions

STATISTICS 579 R Tutorial: More on Writing Functions Fall 2005 1. Iterative Methods: STATISTICS 579 R Tutorial: More on Writing Functions Three kinds of looping constructs in R: the for loop, the while loop, and the repeat loop were discussed previously.

More information

9. Writing Functions

9. Writing Functions 9. Writing Functions Ken Rice Thomas Lumley Universities of Washington and Auckland NYU Abu Dhabi, January 2017 In this session One of the most powerful features of R is the user s ability to expand existing

More information

STAT 540: R: Sections Arithmetic in R. Will perform these on vectors, matrices, arrays as well as on ordinary numbers

STAT 540: R: Sections Arithmetic in R. Will perform these on vectors, matrices, arrays as well as on ordinary numbers Arithmetic in R R can be viewed as a very fancy calculator Can perform the ordinary mathematical operations: + - * / ˆ Will perform these on vectors, matrices, arrays as well as on ordinary numbers With

More information

Introduction to R, Github and Gitlab

Introduction to R, Github and Gitlab Introduction to R, Github and Gitlab 27/11/2018 Pierpaolo Maisano Delser mail: maisanop@tcd.ie ; pm604@cam.ac.uk Outline: Why R? What can R do? Basic commands and operations Data analysis in R Github and

More information

R:If, else and loops

R:If, else and loops R:If, else and loops Presenter: Georgiana Onicescu January 19, 2012 Presenter: Georgiana Onicescu R:ifelse,where,looping 1/ 17 Contents Vectors Matrices If else statements For loops Leaving the loop: stop,

More information

Lab #10 Multi-dimensional Arrays

Lab #10 Multi-dimensional Arrays Multi-dimensional Arrays Sheet s Owner Student ID Name Signature Group partner 1. Two-Dimensional Arrays Arrays that we have seen and used so far are one dimensional arrays, where each element is indexed

More information

Basic stuff -- assignments, arithmetic and functions

Basic stuff -- assignments, arithmetic and functions Basic stuff -- assignments, arithmetic and functions Most of the time, you will be using Maple as a kind of super-calculator. It is possible to write programs in Maple -- we will do this very occasionally,

More information

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous Assignment 3 Methods Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes:

More information

MATH2070: LAB 4: Newton s method

MATH2070: LAB 4: Newton s method MATH2070: LAB 4: Newton s method 1 Introduction Introduction Exercise 1 Stopping Tests Exercise 2 Failure Exercise 3 Introduction to Newton s Method Exercise 4 Writing Matlab code for functions Exercise

More information

Description Syntax Remarks and examples Conformability Diagnostics References Also see

Description Syntax Remarks and examples Conformability Diagnostics References Also see Title stata.com solvenl( ) Solve systems of nonlinear equations Description Syntax Remarks and examples Conformability Diagnostics References Also see Description The solvenl() suite of functions finds

More information

Control Flow Structures

Control Flow Structures Control Flow Structures STAT 133 Gaston Sanchez Department of Statistics, UC Berkeley gastonsanchez.com github.com/gastonstat/stat133 Course web: gastonsanchez.com/stat133 Expressions 2 Expressions R code

More information

MATH2070: LAB 3: Roots of Equations

MATH2070: LAB 3: Roots of Equations MATH2070: LAB 3: Roots of Equations 1 Introduction Introduction Exercise 1 A Sample Problem Exercise 2 The Bisection Idea Exercise 3 Programming Bisection Exercise 4 Variable Function Names Exercise 5

More information

Twister: Language Reference Manual

Twister: Language Reference Manual Twister: Language Reference Manual Manager: Anand Sundaram (as5209) Language Guru: Arushi Gupta (ag3309) System Architect: Annalise Mariottini (aim2120) Tester: Chuan Tian (ct2698) February 23, 2017 Contents

More information

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Ordinary Differential Equation Solver Language (ODESL) Reference Manual Ordinary Differential Equation Solver Language (ODESL) Reference Manual Rui Chen 11/03/2010 1. Introduction ODESL is a computer language specifically designed to solve ordinary differential equations (ODE

More information

Intermediate Programming in R Session 2: Loops. Olivia Lau, PhD

Intermediate Programming in R Session 2: Loops. Olivia Lau, PhD Intermediate Programming in R Session 2: Loops Olivia Lau, PhD Outline When to Use Loops Measuring and Monitoring R s Performance Different Types of Loops Fast Loops 2 When to Use Loops Loops repeat a

More information

Package blockmatrix. February 19, 2015

Package blockmatrix. February 19, 2015 Package blockmatrix February 19, 2015 aintainer License GPL (>= 2) Title blockmatrix: Tools to solve algebraic systems with partitioned matrices Type Package Author Some elementary

More information

MAS115 R programming, Lab Class 5 Prof. P. G. Blackwell

MAS115 R programming, Lab Class 5 Prof. P. G. Blackwell MAS115 R programming, Lab Class 5 Prof. P. G. Blackwell 2017-18 1 Writing functions 1.1 Introduction to Functions R has a lot of built in functions that you have already used e.g. sum(), nrow(), runif(),....

More information

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

Chapter 2: Functions and Control Structures

Chapter 2: Functions and Control Structures Chapter 2: Functions and Control Structures TRUE/FALSE 1. A function definition contains the lines of code that make up a function. T PTS: 1 REF: 75 2. Functions are placed within parentheses that follow

More information

1 2 (3 + x 3) x 2 = 1 3 (3 + x 1 2x 3 ) 1. 3 ( 1 x 2) (3 + x(0) 3 ) = 1 2 (3 + 0) = 3. 2 (3 + x(0) 1 2x (0) ( ) = 1 ( 1 x(0) 2 ) = 1 3 ) = 1 3

1 2 (3 + x 3) x 2 = 1 3 (3 + x 1 2x 3 ) 1. 3 ( 1 x 2) (3 + x(0) 3 ) = 1 2 (3 + 0) = 3. 2 (3 + x(0) 1 2x (0) ( ) = 1 ( 1 x(0) 2 ) = 1 3 ) = 1 3 6 Iterative Solvers Lab Objective: Many real-world problems of the form Ax = b have tens of thousands of parameters Solving such systems with Gaussian elimination or matrix factorizations could require

More information

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50 MATLAB Premier Middle East Technical University Department of Mechanical Engineering ME 304 1/50 Outline Introduction Basic Features of MATLAB Prompt Level and Basic Arithmetic Operations Scalars, Vectors,

More information

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Using Monte Carlo to Estimate π using Buffon s Needle Problem An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Here s the problem (in a simplified form). Suppose

More information

Introduction to MATLAB

Introduction to MATLAB Chapter 1 Introduction to MATLAB 1.1 Software Philosophy Matrix-based numeric computation MATrix LABoratory built-in support for standard matrix and vector operations High-level programming language Programming

More information

Matlab and Octave: Quick Introduction and Examples 1 Basics

Matlab and Octave: Quick Introduction and Examples 1 Basics Matlab and Octave: Quick Introduction and Examples 1 Basics 1.1 Syntax and m-files There is a shell where commands can be written in. All commands must either be built-in commands, functions, names of

More information

II.Matrix. Creates matrix, takes a vector argument and turns it into a matrix matrix(data, nrow, ncol, byrow = F)

II.Matrix. Creates matrix, takes a vector argument and turns it into a matrix matrix(data, nrow, ncol, byrow = F) II.Matrix A matrix is a two dimensional array, it consists of elements of the same type and displayed in rectangular form. The first index denotes the row; the second index denotes the column of the specified

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next

More information

1.00 Lecture 19. Numerical Methods: Root Finding

1.00 Lecture 19. Numerical Methods: Root Finding 1.00 Lecture 19 Numerical Methods: Root Finding short int Remember Java Data Types Type byte long float double char boolean Size (bits) 8 16 32 64 32 64 16 1-128 to 127-32,768 to 32,767-2,147,483,648 to

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab Andreas C. Kapourani (Credit: Steve Renals & Iain Murray) 9 January 08 Introduction MATLAB is a programming language that grew out of the need to process matrices. It is used extensively

More information

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Array An array is a group of variables (called elements or components) containing

More information

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual Contents Fei Liu, Mengdi Zhang, Taikun Liu, Jiayi Yan 1. Language definition 3 1.1. Usage 3 1.2. What special feature

More information

Lecture 3: Basics of R Programming

Lecture 3: Basics of R Programming Lecture 3: Basics of R Programming This lecture introduces you to how to do more things with R beyond simple commands. Outline: 1. R as a programming language 2. Grouping, loops and conditional execution

More information

Package SSLASSO. August 28, 2018

Package SSLASSO. August 28, 2018 Package SSLASSO August 28, 2018 Version 1.2-1 Date 2018-08-28 Title The Spike-and-Slab LASSO Author Veronika Rockova [aut,cre], Gemma Moran [aut] Maintainer Gemma Moran Description

More information

Package morpheus. June 14, 2018

Package morpheus. June 14, 2018 Package morpheus June 14, 2018 Title Estimate Parameters of Mixtures of Logistic Regressions Mixture of logistic regressions parameters (H)estimation with (U)spectral methods. The main methods take d-dimensional

More information

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment What is MATLAB? MATLAB PROGRAMMING Stands for MATrix LABoratory A software built around vectors and matrices A great tool for numerical computation of mathematical problems, such as Calculus Has powerful

More information

Spring 2018 Updates. Computing Technology for All. Data Structure Essentials. Digital Design

Spring 2018 Updates. Computing Technology for All. Data Structure Essentials. Digital Design Spring 2018 Updates Computing Technology for All 1.2 Historical figures in computing 3.4 Cellular networks Type of computers Common input devices Data Structure Essentials 1.4 Algorithm analysis 4.8 BST

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Basic matrix math in R

Basic matrix math in R 1 Basic matrix math in R This chapter reviews the basic matrix math operations that you will need to understand the course material and how to do these operations in R. 1.1 Creating matrices in R Create

More information

Remember Java Data Types

Remember Java Data Types 1.00 Lecture 19 October 24, 2005 Numerical Methods: Root Finding Remember Java Data Types Size Type (bits) Range byte 8-128 to 127 short 16-32,768 to 32,767 int 32-2,147,483,648 to 2,147,483,647 long 64-9,223,372,036,854,775,808L

More information

CSC Web Programming. Introduction to JavaScript

CSC Web Programming. Introduction to JavaScript CSC 242 - Web Programming Introduction to JavaScript JavaScript JavaScript is a client-side scripting language the code is executed by the web browser JavaScript is an embedded language it relies on its

More information

Title. Description. Syntax. Remarks and examples. stata.com. stata.com. What are optional arguments? Example 1

Title. Description. Syntax. Remarks and examples. stata.com. stata.com. What are optional arguments? Example 1 Title stata.com optargs Optional arguments Description Syntax Remarks and examples Also see Description Mata functions may have various numbers of arguments. How you write programs that allow these optional

More information

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Contents 1 Introduction...2 2 Lexical Conventions...2 3 Types...3 4 Syntax...3 5 Expressions...4 6 Declarations...8 7 Statements...9

More information

Introduction to CS databases and statistics in Excel Jacek Wiślicki, Laurent Babout,

Introduction to CS databases and statistics in Excel Jacek Wiślicki, Laurent Babout, One of the applications of MS Excel is data processing and statistical analysis. The following exercises will demonstrate some of these functions. The base files for the exercises is included in http://lbabout.iis.p.lodz.pl/teaching_and_student_projects_files/files/us/lab_04b.zip.

More information

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead. Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public

More information

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1 MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED Christian Daude 1 Introduction MATLAB is a software package designed to handle a broad range of mathematical needs one may encounter when doing scientific

More information

Introduction to R gherardo varando

Introduction to R gherardo varando Introduction to R gherardo varando The R Software Environment As The R Project website states : and R is a free software environment for statistical computing and graphics. It compiles and runs on a wide

More information

The Warhol Language Reference Manual

The Warhol Language Reference Manual The Warhol Language Reference Manual Martina Atabong maa2247 Charvinia Neblett cdn2118 Samuel Nnodim son2105 Catherine Wes ciw2109 Sarina Xie sx2166 Introduction Warhol is a functional and imperative programming

More information

Lecture 3: Basics of R Programming

Lecture 3: Basics of R Programming Lecture 3: Basics of R Programming This lecture introduces how to do things with R beyond simple commands. We will explore programming in R. What is programming? It is the act of instructing a computer

More information

1 Matrices and Vectors and Lists

1 Matrices and Vectors and Lists University of Wollongong School of Mathematics and Applied Statistics STAT231 Probability and Random Variables 2014 Second Lab - Week 4 If you can t finish the log-book questions in lab, proceed at home.

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information

Introduction to Python! Lecture 2

Introduction to Python! Lecture 2 .. Introduction to Python Lecture 2 Summary Summary: Lists Sets Tuples Variables while loop for loop Functions Names and values Passing parameters to functions Lists Characteristics of the Python lists

More information

Sub-setting Data. Tzu L. Phang

Sub-setting Data. Tzu L. Phang Sub-setting Data Tzu L. Phang 2016-10-13 Subsetting in R Let s start with a (dummy) vectors. x

More information

Lab#1: INTRODUCTION TO DERIVE

Lab#1: INTRODUCTION TO DERIVE Math 111-Calculus I- Fall 2004 - Dr. Yahdi Lab#1: INTRODUCTION TO DERIVE This is a tutorial to learn some commands of the Computer Algebra System DERIVE. Chapter 1 of the Online Calclab-book (see my webpage)

More information

1.00 Lecture 25. Root Finding

1.00 Lecture 25. Root Finding 1.00 Lecture 25 Numerical Methods: Root Finding Reading for next time: Big Java: section 19.4 Root Finding Two cases: One dimensional function: f(x)= 0 Systems of equations (F(X)= 0), where X and 0 are

More information

MICROSOFT VISUAL STUDIO AND C PROGRAMMING

MICROSOFT VISUAL STUDIO AND C PROGRAMMING MICROSOFT VISUAL STUDIO AND C PROGRAMMING Aims 1. Learning primary functions of Microsoft Visual Studio 2. Introduction to C Programming 3. Running C programs using Microsoft Visual Studio In this experiment

More information

Lecture 8 Tao Wang 1

Lecture 8 Tao Wang 1 Lecture 8 Tao Wang 1 Objectives In this chapter, you will learn about: Sorting 3 numbers review Function overview Function and parameter declarations Function with empty parameter lists Default arguments

More information

Midterm Examination , Fall 2011

Midterm Examination , Fall 2011 Midterm Examination 36-350, Fall 2011 Instructions: Provide all answers in your bluebook. Only work in the bluebook will be graded. Clearly indicate which problem each answer goes with. There are 100 points

More information

ECE 204 Numerical Methods for Computer Engineers MIDTERM EXAMINATION /4:30-6:00

ECE 204 Numerical Methods for Computer Engineers MIDTERM EXAMINATION /4:30-6:00 ECE 4 Numerical Methods for Computer Engineers ECE 4 Numerical Methods for Computer Engineers MIDTERM EXAMINATION --7/4:-6: The eamination is out of marks. Instructions: No aides. Write your name and student

More information

EPIB Four Lecture Overview of R

EPIB Four Lecture Overview of R EPIB-613 - Four Lecture Overview of R R is a package with enormous capacity for complex statistical analysis. We will see only a small proportion of what it can do. The R component of EPIB-613 is divided

More information

E04FDFP.1. NAG Parallel Library Routine Document

E04FDFP.1. NAG Parallel Library Routine Document E04 Minimizing or Maximizing a Function E04FDFP NAG Parallel Library Routine Document Note: before using this routine, please read the Users Note for your implementation to check for implementation-dependent

More information

8. Control statements

8. Control statements 8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon

More information

The Mathematics of Big Data

The Mathematics of Big Data The Mathematics of Big Data Linear Algebra and MATLAB Philippe B. Laval KSU Fall 2015 Philippe B. Laval (KSU) Linear Algebra and MATLAB Fall 2015 1 / 23 Introduction We introduce the features of MATLAB

More information

Package RCEIM. April 3, 2017

Package RCEIM. April 3, 2017 Type Package Package RCEIM April 3, 2017 Title R Cross Entropy Inspired Method for Optimization Version 0.3 Date 2017-04-03 Author Alberto Krone-Martins Maintainer Alberto Krone-Martins

More information

BEGINNING MATLAB. R.K. Beatson Mathematics Department University of Canterbury. 2 Matlab as a simple matrix calculator 2

BEGINNING MATLAB. R.K. Beatson Mathematics Department University of Canterbury. 2 Matlab as a simple matrix calculator 2 BEGINNING MATLAB R.K. Beatson Mathematics Department University of Canterbury Contents 1 Getting started 1 2 Matlab as a simple matrix calculator 2 3 Repeated commands 4 4 Subscripting, rows, columns and

More information

SML: Specialized Matrix Language White Paper

SML: Specialized Matrix Language White Paper SML: Specialized Matrix Language White Paper Patty Ho pph2103@columbia.edu COMS W4115 Spring 2005 Columbia University Introduction Special mathematical functions and calculations can be quite difficult

More information

MTH309 Linear Algebra: Maple Guide

MTH309 Linear Algebra: Maple Guide MTH309 Linear Algebra: Maple Guide Section 1. Row Echelon Form (REF) and Reduced Row Echelon Form (RREF) Example. Calculate the REF and RREF of the matrix Solution. Use the following command to insert

More information

ANONYMOUS FUNCTIONS... 1 Using Anonymous Functions with Arrays... 4

ANONYMOUS FUNCTIONS... 1 Using Anonymous Functions with Arrays... 4 Contents ANONYMOUS FUNCTIONS... 1 Using Anonymous Functions with Arrays... 4 ANONYMOUS FUNCTIONS Anonymous functions are a simple and concise way to define a function that contains only a single executable

More information

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving The Bisection Method and Newton s Method. If f( x ) a function, then a number r for which f( r) 0 is called a zero or a root of the function f( x ), or a solution to the equation f( x) 0. You are already

More information

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

More information

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook Chapter 1 Section 1.4 Subprograms or functions 0 Functions Functions are essential in writing structured and well-organized code. Functions help for code to be reused. Functions help to reduce errors and

More information

1.00 Lecture 19. Packaging Functions in Objects

1.00 Lecture 19. Packaging Functions in Objects 1.00 Lecture 19 Numerical Methods: Root Finding Packaging Functions in Objects Consider writing method that finds root of function or evaluates a function, e.g., f(x)= 0 on some interval [a, b], or find

More information

R is a programming language of a higher-level Constantly increasing amount of packages (new research) Free of charge Website:

R is a programming language of a higher-level Constantly increasing amount of packages (new research) Free of charge Website: Introduction to R R R is a programming language of a higher-level Constantly increasing amount of packages (new research) Free of charge Website: http://www.r-project.org/ Code Editor: http://rstudio.org/

More information

Scope of this lecture. Repetition For loops While loops

Scope of this lecture. Repetition For loops While loops REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form

More information

INTRODUCTION TO DERIVE - by M. Yahdi

INTRODUCTION TO DERIVE - by M. Yahdi Math 111/112-Calculus I & II- Ursinus College INTRODUCTION TO DERIVE - by M. Yahdi This is a tutorial to introduce main commands of the Computer Algebra System DERIVE. You should do (outside of class)

More information

1 Introduction to MATLAB

1 Introduction to MATLAB 1 Introduction to MATLAB 1.1 General Information Quick Overview This chapter is not intended to be a comprehensive manual of MATLAB R. Our sole aim is to provide sufficient information to give you a good

More information

Linear Algebra in LabVIEW

Linear Algebra in LabVIEW https://www.halvorsen.blog Linear Algebra in LabVIEW Hans-Petter Halvorsen, 2018-04-24 Preface This document explains the basic concepts of Linear Algebra and how you may use LabVIEW for calculation of

More information

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132) BoredGames Language Reference Manual A Language for Board Games Brandon Kessler (bpk2107) and Kristen Wise (kew2132) 1 Table of Contents 1. Introduction... 4 2. Lexical Conventions... 4 2.A Comments...

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Justify all your answers and write down all important steps. Unsupported answers will be disregarded.

Justify all your answers and write down all important steps. Unsupported answers will be disregarded. Numerical Analysis FMN011 2017/05/30 The exam lasts 5 hours and has 15 questions. A minimum of 35 points out of the total 70 are required to get a passing grade. These points will be added to those you

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

Statistical Programming with R

Statistical Programming with R Statistical Programming with R Lecture 6: Programming Examples Bisher M. Iqelan biqelan@iugaza.edu.ps Department of Mathematics, Faculty of Science, The Islamic University of Gaza 2017-2018, Semester 1

More information

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3. MATLAB Introduction Accessing Matlab... Matlab Interface... The Basics... 2 Variable Definition and Statement Suppression... 2 Keyboard Shortcuts... More Common Functions... 4 Vectors and Matrices... 4

More information

Scala Style Guide Spring 2018

Scala Style Guide Spring 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Scala Style Guide Spring 2018 Contents 1 Introduction 1 2 Naming 1 3 Formatting 2 4 Class Declarations 3 5 Functional Paradigms 4 6 Comments

More information

ECE Lesson Plan - Class 1 Fall, 2001

ECE Lesson Plan - Class 1 Fall, 2001 ECE 201 - Lesson Plan - Class 1 Fall, 2001 Software Development Philosophy Matrix-based numeric computation - MATrix LABoratory High-level programming language - Programming data type specification not

More information

A Crash Course in R (and other notes)

A Crash Course in R (and other notes) A Crash Course in R (and other notes) p. 1/8 A Crash Course in R (and other notes) Professor Russell Steele (1), edited, with permission, by Dr. Pierre-Jérôme Bergeron (2) (1)Department of Mathematics

More information

Contents. I Basics 1. Copyright by SIAM. Unauthorized reproduction of this article is prohibited.

Contents. I Basics 1. Copyright by SIAM. Unauthorized reproduction of this article is prohibited. page v Preface xiii I Basics 1 1 Optimization Models 3 1.1 Introduction... 3 1.2 Optimization: An Informal Introduction... 4 1.3 Linear Equations... 7 1.4 Linear Optimization... 10 Exercises... 12 1.5

More information

Programming R. Manuel J. A. Eugster. Chapter 4: Writing your own functions. Last modification on May 15, 2012

Programming R. Manuel J. A. Eugster. Chapter 4: Writing your own functions. Last modification on May 15, 2012 Manuel J. A. Eugster Programming R Chapter 4: Writing your own functions Last modification on May 15, 2012 Draft of the R programming book I always wanted to read http://mjaeugster.github.com/progr Licensed

More information

Lecture 2: Variables, Vectors and Matrices in MATLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB Lecture 2: Variables, Vectors and Matrices in MATLAB Dr. Mohammed Hawa Electrical Engineering Department University of Jordan EE201: Computer Applications. See Textbook Chapter 1 and Chapter 2. Variables

More information

More R Concepts and Exercises

More R Concepts and Exercises More R Concepts and Exercises Kasper Daniel Hansen Margaret Taub based on slides developed by Jim Bullard University of Copenhagen August 17-21, 2009 1 / 27 Review I What does this code do: > v

More information

Package xwf. July 12, 2018

Package xwf. July 12, 2018 Package xwf July 12, 2018 Version 0.2-2 Date 2018-07-12 Title Extrema-Weighted Feature Extraction Author Willem van den Boom [aut, cre] Maintainer Willem van den Boom Extrema-weighted

More information

Math-2. Lesson 3-1. Equations of Lines

Math-2. Lesson 3-1. Equations of Lines Math-2 Lesson 3-1 Equations of Lines How can an equation make a line? y = x + 1 x -4-3 -2-1 0 1 2 3 Fill in the rest of the table rule x + 1 f(x) -4 + 1-3 -3 + 1-2 -2 + 1-1 -1 + 1 0 0 + 1 1 1 + 1 2 2 +

More information

Practice for Learning R and Learning Latex

Practice for Learning R and Learning Latex Practice for Learning R and Learning Latex Jennifer Pan August, 2011 Latex Environments A) Try to create the following equations: 1. 5+6 α = β2 2. P r( 1.96 Z 1.96) = 0.95 ( ) ( ) sy 1 r 2 3. ˆβx = r xy

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals VENTURE COMS 4115 - Language Reference Manual Zach Adler (zpa2001), Ben Carlin (bc2620), Naina Sahrawat (ns3001), James Sands (js4597) Section 1. Lexical Elements 1.1 Identifiers An identifier in VENTURE

More information

7 Control Structures, Logical Statements

7 Control Structures, Logical Statements 7 Control Structures, Logical Statements 7.1 Logical Statements 1. Logical (true or false) statements comparing scalars or matrices can be evaluated in MATLAB. Two matrices of the same size may be compared,

More information

Introduction to Matlab for Econ 511b

Introduction to Matlab for Econ 511b Introduction to Matlab for Econ 511b I. Introduction Jinhui Bai January 20, 2004 Matlab means Matrix Laboratory. From the name you can see that it is a matrix programming language. Matlab includes both

More information

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Arrays Lecture 07 - September 19, 2018 Prof. Zadia Codabux 1 Agenda Arrays For Each Loop 2D Arrays 2 Administrative None 3 Arrays 4 Array Data structure

More information

Section 5: Functions: Defining, Evaluating and Graphing

Section 5: Functions: Defining, Evaluating and Graphing Section 5: Functions: Defining, Evaluating and Graphing In this section you will learn how to define a function f (x ) in Maple. The remainder of the section covers evaluating functions, solving equations

More information

University of Alberta

University of Alberta A Brief Introduction to MATLAB University of Alberta M.G. Lipsett 2008 MATLAB is an interactive program for numerical computation and data visualization, used extensively by engineers for analysis of systems.

More information