×

### Let's log you in.

or

Don't have a StudySoup account? Create one here!

×

or

## AN INTRODUCTN TO R

by: Giovani Ullrich PhD

51

0

7

# AN INTRODUCTN TO R STAT 579

Giovani Ullrich PhD
ISU
GPA 3.5

Staff

These notes were just uploaded, and will be ready to view shortly.

Either way, we'll remind you when they're ready :)

Get a free preview of these Notes, just enter your email below.

×
Unlock Preview

COURSE
PROF.
Staff
TYPE
Class Notes
PAGES
7
WORDS
KARMA
25 ?

## Popular in Statistics

This 7 page Class Notes was uploaded by Giovani Ullrich PhD on Saturday September 26, 2015. The Class Notes belongs to STAT 579 at Iowa State University taught by Staff in Fall. Since its upload, it has received 51 views. For similar materials see /class/214410/stat-579-iowa-state-university in Statistics at Iowa State University.

×

## Reviews for AN INTRODUCTN TO R

×

×

### What is Karma?

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

Date Created: 09/26/15
Fall 2005 STATISTICS 579 R Tutorial More on Writing Functions 1 Iterative Methods Three kinds of looping constructs in R the for loop the while loop and the repeat loop were discussed previously In order to understand the need for looping in a programming language some background knowledge in numerical computing is useful What follows is a short introduction to iterative methods A variety of problems exist where the solution to a problem can be improved by applying the same calculation repeatedly each time using the previous solution as the starting value Such a mathematical process is said to form a convergent sequence if the solution converges to a value such that the error becomes uniformly smaller in each iteration The problem of nding the solution to the equation x 0 where f is a nonlinear function of a single variable x is used in this class as an example of application of this method Example Find the root of x x3 7 3x 1 0 contained in the interval 01 by an iterative method There are several approaches to obtaining iterative algorithms for this problem A very brief look at a few methods follows below Bisection Algorithm The class of methods where only the value of the function at different points are calculated is known as search methods The best known iterative search methods are called bracketing methods since they attempt to reduce the width of the interval where the root is located while keeping the successive iterates bracketed within that interval The simplest bracketing method is the bisection algorithm where the interval of uncertainty is halved at each iteration and from the two resulting intervals the one that straddles the root is chosen as the next interval Consider the function f to be continuous in the interval x0 x1 where x0 lt x1 and that it is known that a root of x 0 exists in this interval Bisection Algorithm Select starting values x0 lt x1 such that xo gtk x1 lt 0 ex and 5f Repeat a set 2 0 x12 b if fw2 aw lt 0 set x1 x2 c else set x0 x2 until x1 7 x0 lt em or x2 lt 5f Return x2 To use the bisection algorithm it is easily veri ed by plotting the function that the x x3 7 3x 1 O has a single root in the interval 01 gt ffunctionxxquot3 3x 1 gt xseq01 10 gt plotxfxtypequotlquot gt ablineh0lty3 fx 0 0 The results of the bisection iteration are Error 1 05 1 1 0375 01527036447 2 025 1 0375 0265625 00972963553 3 0375 0265625 0375 007226563 00277036447 4 03125 0265625 007226563 009301758 00347963553 5 034375 009301758 007226563 0009368896 00035463553 6 0359375 0009368896 007226563 003171158 00120786447 7 03515625 0009368896 003171158 001123571 00042661447 8 034765625 0009368896 001123571 00009493232 00003598947 9 0345703125 0009368896 00009493232 000420583 00015932303 10 03466796875 000420583 00009493232 0001627262 00006166678 11 03471679688 0001627262 00009493232 0000338721 000012838655 12 03474121094 0000338721 00009493232 00003053632 0000115754075 13 03472900391 0000338721 00003053632 1666335e 05 63162375e 06 14 03473510742 1666335e 05 00003053632 00001443538 5471891875e05 15 03473205566 1666335e 05 00001443538 638462e 05 2420134062e05 16 03473052979 1666335e 05 638462e 05 2359167e 05 8942551562e 06 It is obvious that the convergence is very slow the error reducing extremely slowly and even changing sign However the bisection eventually converges and is not affected by features of the function such as its gradient Fixed Point Iteration By re expressing fw 0 in the form x gw one can obtain an iterative formula 54 gw for nding roots of nonlinear equations Any such function 9 called the mapping function must satisfy conditions so that the sequence 531 we converges to a unique solution in a speci ed interval 11 To use the xed point method for fw 33 7 3x 1 0 rewrite it in the form x 33 13 giving gw 33 13 It can be veri ed that this choice of 9 satis es the necessary conditions for the iteration to produce a convergent sequence To obtain a starting value we use the plot of fw in the interval 01 The plot clearly shows that a root exists is the interval between 3 and 4 In practice several intervals may be searched for the existence of roots by trial and error and is called a grid search The results of the iteration using mm 4 as the starting value are i cc Error 1 03546667 0007370267 2 03482043 00009079228 3 03474062 00001097562 4 03473096 1320312e 05 5 03472980 1553274e 06 6 03472965 1480699e 07 7 03472964 2141932e 08 NewtonRaphson Iteration This iteration is based on the following Consider a starting value mm for obtaining an itertive formula to solve fc 0 If the next iterate 0 is very close to mm then the rst derivative off at mm can be approximated as f w0 m HEW This gives an approximation for the next iterate mm as mu x mm 7 90 1 which leads to the Newton Raphson iterative formula W W 7 f WW Newton Raphson iteration produces a convergent sequence as long as the starting value mm is chosen to be close to a root of fc 0 A good property of this method is that it has quadratic convergence ie the error is reduced quadratically at each iteration the fastest rate of convergence for an iteration for root nding Since the the rst derivative of fc 3 7 3x 1 the example used previously is f w 3w2 7 3 the Newton Raphson iterative formula to solve 3 7 3x 1 0 is given by 3 w2 1 7 7 7 7 3 1 3462 7 3 2 Applying this iteration with the starting value of mm 4 to nd the root gives the following sequence i 5 Error 1 03460317 0001264654 2 03472957 6740933e 07 3 03472964 446663e08 4 03472964 4466614e08 When iteration schemes such as above are implemented in computer programs a condi tion for termination of the iteration called the stopping rule must be speci ed In the above two examples the iteration was terminated when the absolute difference between two successive iterates became smaller than 1e 8 It has been suggested that more than a single rule be used for determining whether convergence is achieved For example in a root nding problem one could also check if the function value has become close enough to zero to conclude that convergence has been reached and thus terminate the iteration 2 Programming Iterative Methods in R If the xed point iteration is to be used to solve an equation fc 0 it must be rst expressed in the form cc gc where gw must satisfy certain conditions so that the iteration 5 gw produces a convergent sequence It will be assumed that this part has been done and gw is available The following function to be copied from the le simpleR in the StatLabsStat579R folder on your desktop is an implementation of the xed point iteration also called simple iteration Source this function as usual into R simplef unctionx0 nlim eps iterno 0 repeat iterno iterno 1 ifiterno gt nlim catquot Iteration Limit Exceeded Current quotiterno fill T x1gx0 catquotIter No iterno Current Iterate ifabsx1x0 lt epslIabsx1gx1lt1e10 break x0x1 returnx1 Note that the arguments are respectively g the mapping function x0 the starting value nlim a limiting value for the number of iterations for the iteration to be terminated if convergence is not reached before that number and eps the tolerance value to determine if convergence has been reached To nd the root of fw 33 7 3x 1 0 contained in the interval 01 using the mapping function gw 33 13 rst de ne g and call the above function i Assume nR the function simple has been sourced into R gt g functionxxquot313 gt simple4100000001 Iter No39 1 Current Iterate 03546667 Iter No 2 Current Iterate 03482043 Iter No 3 Current Iterate 03474062 Iter No 4 Current Iterate 03473096 Iter No 5 Current Iterate 03472980 Iter No 6 Current Iterate 03472965 Iter No 7 Current Iterate 03472964 1 03472964 Note carefully that the R function simple calls another R function g functions s imp l e problem The modified version of simple is Both these are user written However if the function g were to be renamed as say g1 will not execute because it will look for the function named g0 To correct this the function simple is modi ed by including g as an argument sourced into R quot x1 fill T gt simple functiong x0 nlim eps iterno 0 repeat iterno iterno 1 ifiterno gt nlim catquot Iteration Limit Exceeded Current quotiterno fill T x1NA break else x1gx0 catquotIter No quot iterno quot Current Iterate quot x1 fill T ifabsx1x0 lt epslIabsx1gx1lt1e10 break x0x1 returnx1 gt simpleg4100000001 Iter No 1 Current Iterate 03546667 Iter No 2 Current Iterate 03482043 Iter No 3 Current Iterate 03474062 Iter No 4 Current Iterate 03473096 Iter No 5 Current Iterate 03472980 Iter No 6 Current Iterate 03472965 Iter No 7 Current Iterate 03472964 1 03472964 In the following implementation of the Newton Raphson algorithm to solve f 07 two R functions fun and derfO that represent the function f and its derivative f are passed into the function newtonO as arguments The function newtonO is in the le newtonR available in the StatLabsStat579R folder on your desktop Copy this le and source it into R Before calling newtonO7 two R functions fun and derf need to be written newtonfunctionfun derf x0 eps iter0 repeat iter iter1 x1 x0 funx0derfx0 ifabsx0 x1 lt epslIabsfunx1lt1e10 break x0 x1 catquot Iter No quot iter quot Current Iterate quot x1fillT returnx1 gt fun functionxxquot3 3 1 gt derf functionx 3xquot2 3 gt newtonfunderf 4 000001 Iter No 1 Current Iterate 03460317 Iter No 2 Current Iterate 03472957 1 03472964 Below7 the entire sequence of steps needed to solve an equation using newtonO is repeated to nd the roots of the equation we z 02 First sketch the function gt fun2 functionx xexpx02 gt x seq2220 gt fun2x 1 1497811220 1091232802 785765477 557672048 388583912 264318178 7 173950426 109088663 063305522 031694768 010525393 003027777 13 011093553 015266956 016735168 016374898 014827479 012556636 19 009892812 007067057 gt xseq14100 gt plotxfun2xtypequotlquot gt ablineh0lty2 It is obvious that there is one root near 00 and another near 20 Write a function derf2 representing f and call newtonO with appropriate starting values gt derf2functionx 1xexpx gt newtonfun2derf200 000001 Iter No 1 Current Iterate 02 Iter No 2 Current Iterate 02553507 Iter No 3 Current Iterate 02591540 Iter No 4 Current Iterate 02591711 1 02591711 gt newtonfun2derf220000001 Iter No 1 Current Iterate 2522189 Iter No 2 Current Iterate 2542569 Iter No 3 Current Iterate 2542641 1 2 542641 Finally7 for completeness an R function named bisectO for implement the bisection method is available in the le bisectR in the StatLabsStat579R folder on your desktop Recall that the bisection method is a bracketing method thus two values 30 31 that bracket the requiredroot uniquely must be available as starting values bisectfunctionfun x0 x1 eps iterno 0 repeat iterno iterno 1 x2 x0x12 if funx2funx0lt0 x1x2 else x0x2 ifabsx1x0 lt epsl absfunx2lt1e 10 break catquot Iter No quot iterno quot Current Iterate quot x2 fill T returnx2 gt ffunctionxxquot3 3x 1 gt bisectf0100001 Iter No 1 Current Iterate 05 Iter No 2 Current Iterate 025 Iter No 3 Current Iterate 0375 Iter No 4 Current Iterate 03125 Iter No 5 Current Iterate 034375 Iter No 6 Current Iterate 0359375 Iter No 7 Current Iterate 03515625 Iter No 8 Current Iterate 03476563 Iter No 9 Current Iterate 03457031 Iter No 10 Current Iterate 03466797 Iter No 11 Current Iterate 0347168 Iter No 12 Current Iterate 03474121 Iter No 13 Current Iterate 03472900 Iter No 14 Current Iterate 03473511 Iter No 15 Current Iterate 03473206 Iter No 16 Current Iterate 03473053 1 03472977

×

×

### BOOM! Enjoy Your Free Notes!

×

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

Steve Martinelli UC Los Angeles

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

Jennifer McGill UCSF Med School

#### "Selling my MCAT study guides and notes has been a great source of side revenue while I'm in school. Some months I'm making over \$500! Plus, it makes me happy knowing that I'm helping future med students with their MCAT."

Steve Martinelli UC Los Angeles

Forbes

#### "Their 'Elite Notetakers' are making over \$1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!
×

### 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