# 7-03-2020 in R, type source("Rlang.q") to execute Appendix C examples of R Rdesc = function() { cat("\n\n ***** R Tutorial (Appendix C) *****\n\n") cat("\t 1. R is a functional language, with arguments inside parentheses ()\n") cat("\t 2. Download R from https://cran.r-project.org/ \n") cat("\t 3. Type help.start() to bring up an interactive search window\n") cat("\t 4. We will use R in place of probability tables and for plotting\n") cat("\t 5. We will focus on basic R, not RStudio and other advanced options/libraries\n") cat("\t 6. Most R functions have optional arguments with default values\n") cat("\t 7. We end with a description of how to write your own R function\n\n\n") cat("\t type the letter 'c' to continue\n") cat("\t type the letter 'Q' to quit\n") cat("\t invoke the function 'q()' to exit R\n\n"); browser() cat("\n\n\n") } Rdesc() Rbasics = function() { cat("Basic Arithmetic, built-in Functions, and Graphics (the R prompt is >)\n\n") cat("> x=1+3; y=sin(x); z=exp(x)\t\t separate multiple inline commands with ;\n\n") x=1+3; print(paste("x=",x)) y=sin(x); print(paste("y=",y)) z=exp(x); print(paste("z=",z)) cat("\n\t make a vector by concatenation > xyz = c(x,y,z)\n\n") xyz = c(x,y,z); print(paste("xyz=c(",paste(xyz,collapse=","),")")) cat("\n\n\t The browser() R function takes you into interactive mode:\n\n") cat("\t\t Enter any R command\n") cat("\t\t or the letter 'n' to execute the next command\n") cat("\t\t or the letter 'c' to continue\n\n") browser() # basic plotting cat("\n\n Some basic plotting examples\n\n") cat("\t 100 random points on unit square\n") cat("\t\t > xr=runif(100); yr=runif(100)\n") xr=runif(100); yr=runif(100) cat("\t\t > plot(xr,yr)\t\t basic plot of scatter diagram\n\n") plot(xr,yr); lim=c(0,1) browser() cat("\n\t lim=c(0,1)\tmore optional plotting arguments...better?\n\n") cat("\t> plot(xr,yr,pch=16,col=2,xlim=lim,ylim=lim,main='Example')\n\n") plot(xr,yr,pch=16,col=2,xlim=lim,ylim=lim,main="Example"); browser() # line plots cat("\n\n line vs scatter plot examples\n\n") cat("\t x = seq(0,1,0.01)\t vector of 101 equally spaced points (0:100)/100\n\n") x = seq(0,1,0.01) cat("\t compute values of sin function at x y = sin(x)\n") y = sin(x) cat("\n\t y is also a vector with same length as x\n\n") cat("\t plot(x,y,type='l')\t gives a line plot rather than points (default)\n\n") plot(x,y,type='l',ylim=c(-1,1)); browser() cat("\t\t add another sine curve to plot\n\n") lines(x,sin(8*pi*x),col=2,lty=2) abline(h=0,col=4,lwd=.5); browser() cat("\n\n various unix-like command listing objects in file .RData\n\n") cat("\t > ls()\t you should type this is yourself\n\n"); browser(); ls() browser() } Rbasics() Rprobs = function() { cat("Probability Related Functions\n\n") cat("\t For any built-in prob mass or density function,\n") cat("\t\t there are 4 functions:\n\n") cat("\t\t\t The Uniform is unif\n") cat("\t\t\t The standard Normal is norm\n") cat("\t\t\t The Binomial is binom, etc.\n\n\n"); browser() cat("\n\t Examples:\n\n") cat("\t\t > fx = dnorm(x)\t\t the pdf at x (x can be a vector)\n") cat("\t\t > Fx = pnorm(x)\t\t the cdf at x; Prob( X le x )\n") cat("\t\t > q = qnorm(p)\t\t quantile: P( X le q ) = p\n") cat("\t\t > x = rnorm(100)\t\t generate 100 N(0,1) samples\n\n") cat("\t\t > set.seed(123)\t\t set random number seed,\n") cat("\t\t\t\t\t\t so can repeat the same sequence\n\n") cat("\n\t Pick some values of x p q and try it out.\n\n\n") browser() } Rprobs() Rloops = function() { cat("\n **** Looping and Control ****\n\n\n") cat("\t > n=100; nrep=1000; ans=rep(0,nrep)\t create a new vector\n\n") n=100; nrep=1000; ans=rep(0,nrep); browser() cat("\n\n\t > for(k in 1:nrep) {ans[k]=mean(rnorm(n)}\n") cat("\t\t saves (a new) xbar in kth place\n\n") for(k in 1:nrep) {ans[k]=mean(rnorm(n))}; browser() cat("\n\n\t > hist(ans,40,col=2,main='dist of xbar')\n") cat("\t\t plots a histogram of the 1000 sample means\n\n") hist(ans,40,col=2,main='dist of xbar'); browser() cat("\n\n\t useful logical construct:\n\n") cat("\t\t > if(runif(1)>0.5) {side='Heads'} else {side='Tails'}\n\n") browser() } Rloops() Rfunc = function(){ cat("\n\n **** Writing Your Own R Function to Visualize mean and std dev ****\n\n") cat("\t Note: Variables created in functions are not saved on exit (good!)\n") hw1 = function(n=100,nrep=1000,seed=123) { set.seed(seed) ans=matrix(0,nrow=nrep,ncol=2) # save simulation results # simulation loop for( k in 1:nrep ) { z=rnorm(n); ans[k,1]=mean(z); ans[k,2]=sd(z) } # save mean and standard deviation in kth row of ans hist( ans[,1], 40, col=2 ); abline(v=0) # 1st column of ans browser() # examine graph; type c to continue; Q to quit hist( ans[,2], 40, col=2 ); abline(v=1) # 2nd column of ans return(list(ans=ans,n=n)) } # the function hw1 is now complete cat("\n\n **** print(hw1) types out the function hw1 entered ****\n\n") print(hw1); cat("\n"); browser() cat("\n\n run function hw1 and save output to list 'results'\n\n") cat("\t note that hw1 plots and returns a list as well\n\n") results = hw1() cat("\n\t Did it run the first time??? If not, debug in browser\n\n") browser() } Rfunc() Rpdf = function() { cat("\n\n Making It Easy to Do Your Homework:\n\n") cat("\t Type the function hw1 Into the File hw1.txt\n\n"); browser() cat("\n\n\t\t If you include the command results=hw1()\n") cat("\t\t\t at the end of the file, it will execute below\n\n"); browser() cat("\t COPY AND RUN THESE:\n\n") cat("\t\t > source('hw1.txt') \t # read the commands into R and debug if necessary\n\n") cat("\t\t > dev.copy2pdf('fig1.pdf') \t # copy figure to a pdf file to submit\n\n") cat("\t\t > print(results$ans); print(results$n) \t # access items in the list\n\n") cat("\t\t > q()\t\t # quit R (save to .RData option given)") browser() } Rpdf()