Programming$in$R - About people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM689/Programming.pdfESSM689$Quan,tave$Methods$in$ Ecology,$Evolu,on$and$Biogeography$ Ecosystem$Science$and$Management|$

Post on 26-Jun-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Programming  in  R  

A.  Michelle  Lawing  Ecosystem  Science  and  Management  Texas  A&M  University  College  Sta,on,  TX  77843  alawing@tamu.edu  

 

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Schedule  for  today  •  My  presenta,on    

–  Review  –  New  stuff  

•  Model  Selec,on  and  Bootstrapping  presenta,on  

•  Model  Selec,on  and  Bootstrapping  demonstra,on  

•  BREAK  

•  Model  Selec,on  and  Bootstrapping  tutorial  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Review:  Probability  and  Distribu,on  

•  R  makes  it  easy  to  draw  probability  distribu,ons  to  demonstrate  sta,s,cal  concepts  

•  Func,ons  for  each  distribu,on  follow  a  par,cular  format  

Name   Descrip,on  

dname()   Density  or  probability  func,on  

pname()   Cumula,ve  density  func,on  

qname()   Quan,le  func,on  

rname()   Random  deviates  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Review:  Probability  and  Distribu,on  

Normal   Uniform   Student  t  dnorm(0)   dunif(0)   dt(0,20)  pnorm(0)   punif(0)   pt(0,20)  qnorm(0)   qunif(0)   qt(0,20)  rnorm(1)   runif(1)   rt(1,20)  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

−4 −2 0 2 4

0.0

0.4

0.8

Comparison of Distributions

x value

Den

sity

− P

roba

bilit

y Distributionsdf=1df=3df=8df=30normunif

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

−4 −2 0 2 4

0.0

0.1

0.2

0.3

0.4

Comparison of t Distributions

x value

Den

sity

− P

roba

bilit

y Distributionsdf=1df=3df=8df=30norm

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Normal Distribution

x values

Den

sity

− P

roba

bilit

y

P( −0.674 < x values < 0.674 ) = 0.5

−4 −2 0 2 4

0.0

0.1

0.2

0.3

0.4

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Student T Distribution Two−Tailed

x values

Den

sity

− P

roba

bilit

y

P( −1.44 > x values < 1.44 ) = 0.15

−4 −2 0 2 4

0.0

0.1

0.2

0.3

0.4

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Programming  in  R  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Ge`ng  Help  

•  There  are  various  control  func,ons  that  are  used  to  establish  the  control  flow  

 help(Control)  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Execu,ng  a  file  

•  A  set  of  commands  can  be  saved  in  a  file  and  executed  from  the  command  line  in  R  

 source("hep://michellelawing.info/file.R")                #source  a  file,  url,  or  other  connec,on    help(source)  

 

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Execu,ng  a  file  

source("hep://michellelawing.info/file.R",  echo  =  T)    source("hep://michellelawing.info/file.R",  print.eval  =  T)    source("hep://michellelawing.info/file.R",  print.eval  =  F)    source("hep://michellelawing.info/file.R",  verbose  =  T)    TIP:  Open  R  from  right  clicking  on  your  script  file      

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Where  is  the  file?  

source(“notThere.R”)      #some,mes  R  can’t  find  the  file                    #R  searches  the  current  director  

 getwd()              #returns  the  working  directory    dir()                #lists  the  files  in  the  wd    setwd()              #change  wd  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

if  statement  

x  <-­‐  1                #assign  x  a  number    if(  x  <  5)  {              #if  x  is  less  than  5  

 x  <-­‐  x  +  1            #then  increment  by  1  }    

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

else  statement  

x  <-­‐  5              #assign  x  a  number    if(  x  <  5)  {            #if  x  is  less  than  5  

 x  <-­‐  x  +  1          #then  increment  by  1  }  else  {            #in  all  other  cases  

 x  <-­‐  x  –  1          #decrement  by  1  }    

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

chain  ifs  together  

x  <-­‐  5              #assign  x  a  number    if(  x  <  5)  {            #if  x  is  less  than  5  

 x  <-­‐  x  +  1          #then  increment  by  1  }  if  else  (x  ==  5){      #or  if  x  equals  5  

 x  <-­‐  x  +  1          #then  increment  by  1  }  else  {            #in  all  other  cases  

 x  <-­‐  x  –  1          #decrement  by  1  }  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

for  statements  for(i    in    1:10)    {          #loop  the  temp  var  i  10  ,mes  

 print(i)              #print  i  each  loop  }    for(i    in    seq(0,    1,    by    =    0.1))    {  #loop  the  i  from  0  to  1  by  0.1  

 print(i)              #print  i  each  loop  }    for(i    in    10:5)    {          #loop  i  from  10  to  5  

 print(i)              #print  i  each  loop  }    

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Random  Walk  

e  <-­‐  200            #assign  the  number  of  ,mes  steps  y  <-­‐  0              #assign  the  star,ng  value    for(i    in    2:e)    {          #loop  i  from  1  to  e            y[i]    <-­‐    rnorm(1,    mean    =    y[i  –  1])              

               #draw  a  random  number  }          plot(1:e,    y,    pch    =    16,    type    =    "l”)    TIP:  You  don’t  have  to  ini,alize  arrays  in  R.            

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

while  statement  

x    <-­‐    1                #assign  a  star,ng  value  cntr    <-­‐    0              #set  the  counter  to  0    while(x    <    3)    {          #repeat  un,l  condi,al  is  met          x    <-­‐    rnorm(1)          #draw  random  number          cntr    <-­‐    cntr    +    1        #increment  the  counter  }    cntr                #print  the  number  of  draws  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Other  useful  statements  

•  repeat  –  Same  as  while,  except  evaluates  at  least  once  –  Need  to  define  stopping  point  

•  break  –  break  stops  the  execu,on  of  the  current  loop  

•  next  –  next  skips  the  remaining  statements  within  a  loop  and  starts  the  next  looping  cycle  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

func,ons  

foo  <-­‐    func,on  (    a,    b)  {    #assign  a  func,on  name    x    <-­‐  runif(    10,    a,    b)    #draw  10  values  from  a  unif  dist    mean(x)          #calculate  the  mean  

}      foo(  -­‐1,    1)    #execute  func,on  with  min  and  max  input  

       #the  last  expression  in  the  func,on  is  returned  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

func,ons  –  return    

foo  <-­‐    func,on  (    a,    b)  {        x    <-­‐  runif(    10,    a,    b)        return(list(mu    =    mean(x),    std    =    sd(x)))        

}                      #use  return  statement      foo(  -­‐1,    1)                  #returns  list  

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

func,ons  –  return  mul,ple  

foo  <-­‐    func,on  (    a,    b)  {        x    <-­‐  runif(    10,    a,    b)        return(list(a    =    a,    b    =    b,    mu    =    mean(x),    std    =    sd(x)))  

}                    #use  return  statement      foo(  -­‐1,    1)                  #returns  list  and  input    

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

func,ons  –  return  plot  

foo  <-­‐    func,on  (    a,    b)  {        x    <-­‐  runif(    10,    a,    b)    hist(x)      return(list(a    =    a,    b    =    b,    mu    =    mean(x),    std    =    sd(x)))  

}                    #use  return  statement      foo(  -­‐1,    1)                  #returns  list  and  input    

ESSM  689  Quan,ta,ve  Methods  in  Ecology,  Evolu,on  and  Biogeography  

Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Exercise  -­‐  Programming  

Create  a  func,on  that  plots  a  random  walk  by  using  rnorm.  The  func,on  should  read  in  the  ini,al  trait  value  and  the  number  of  ,me  steps.    

top related