forked from PLS120BookTeam/PLS120Book2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.0_Rcomputation.Rmd
More file actions
executable file
·133 lines (77 loc) · 7.17 KB
/
Copy path02.0_Rcomputation.Rmd
File metadata and controls
executable file
·133 lines (77 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Chapter 2: Statistics with R {#ch.statsR}
## Learning Objectives for Chapter
1. Install R and RStudio.
1. Find information about objects and functions in R.
1. Create and use R scripts and projects in RStudio.
1. Read data into R from csv files and line input.
1. Perform simple assignments and calculations.
1. Use vectorized operations.
1. Create, study and use data frames.
1. Create simple plots and export them.
1. Customize global and project options.
1. Identify, list and describe function of each pane.
1. Describe the efects of the parameters of the Uniform distribution
1. Calculate the expected value of a random variable with Uniform distribution.
1. Explain what the pdf of the Uniform distribution shows (y axis, X axis)
1. Draw a sketch of the sum of 2 uniform distributions.
1. Give an example of a random variable that might be modelled using the Uniform distribution.
## R and RStudio {#sect.RnRStudio}
W. N. Venables, D. M. Smith and the R Core Team (2016) state: "R is an integrated suite of software facilities for data manipulation, calculation and graphical display." They specifically leave out the word "statistics" to emphasize that R is actually an "environment" or fully planned coherent system for programming. RStudio furthers this idea and provides an IDE or integrated development system for programming in R and other languages. In fact, this book and all the figures and interactive examples and simulations were completely programmed, compiled and deployed using R in the RStudio IDE. The only way to get a good idea of what R and Rstudio can do is to use them with a curious and exploratory approach.
">" on the left of each line in the Terminal is called the cursor and it indicates that R is ready to receive a command. When the cursos changes to "+" it means that you have submitted a partial command and R indicates it is waiting for you to finish the command.
Use material from lab 1.
"<-" and "=" mean the same thing. They assign the value on the right into the object on the left: object.name <- operation. The "=" sign is also used to give values to the arguments of functions.
## Functions
Functions take arguments, perform operations on them and return results. For example, in mean(x = c(8, 4, 6)), mean() is the function and the argument called x receives the value c(8, 4, 6), which is a vector made up of the numbers 8, 4, and 6 in that order. The function returns a result that is the number 6.
Users interact with R mostly using functions. R and R packages provide thousands of functions, and each user can create new custom fuctions. Suppose we want to make a plot of a polynomial of order 3 for many different values of the parameters. We define a function that calculates the values of the polynomial and plots them all in one step.
``` {r}
plotPoly <- function(b0, b1, b2, b3, xmin, xmax){ # name function and arguments
x <- seq(from = xmin, to = xmax, length.out = 100) # create x values
y <- b0 + b1 * x + b2 * x^2 + b3 * x^3 # calculate y values
plot(y ~ x, type = "l") # plot the results
}
# Now that the function is defined we can make plots for any values of the arguments pretty quickly
plotPoly(b0 = 1, b1 = 0.5, b2 = 0.03, b3 = -0.01, xmin = -5, xmax = 10)
# Adjust the range of x to get a better view
plotPoly(b0 = 1, b1 = 0.5, b2 = 0.03, b3 = -0.01, xmin = -10, xmax = 10)
```
## Help
RStudio has a pane specifically designed to find information about functions, objects, data and many other things in R. The help pane has an entry area for keywords at the top right. There is a basic toolbar that can be used to navigate pages as in a browser.
There is a large community of R users all over the world. This community provides lots of help and support through multiple websites. Whenever you are stuck and cannot find the information you need in a form that you understand using R help, you should use a seach engine, typically Google, and pose your question directly as a web search. This works remarkably well if you use the correct keyworkds. Always end your query with "in R" so the search is much more focused.
## Data and data frames
One of the most common way to have data in R is using objects called data frames. These are table-like structures with rows and columns like a spreadsheet. Each column can have only one type of data, but different columns can have different data types. Data and objects in general in R can be of various types, called "modes." Whenever we enter data into R and do not specify the type, R will assign it the data type that does not lose any information. For example, if we enter A, 1, 3, 4, t, i, 7.5, into a column of a data frame, R will make a column of type character, because that is the only mode that can contain letters and numbers, but the numbers are stored as characters. The mode of objects can be changed, with restrictions.
Point to R resources and cheatsheets.
Give simple examples.
R will be a tool that we need to use, but it is not the goal of the course.
All code will be provided, with exception of very simple fill-in-the-blank exercises.
Should we use Rcmdr?
Explain structure of R Studio, topology of scripts, markdown files, directory, etc.
## Examples {#ch.statsR.ex01}
Provide and example of a simulation to determine the distribution of the sum of two independent uniform distributions. The definition of independence is found in Section \@ref(ch.Prob.indep) . Show analytic and simulation solutions. Show that analytic solution is general. Simulation may depend on the specific values selected to simulate.
## Exercises and Solutions
Use simulation and analytic/intuition to determine what happens when two uniform distributions are summed. Explore uniform distributions with different coefficients. Explain that the coefficients of the uniform distribution are the two ends of the "box."
Probability density function of the Uniform distribution U(a, b).
We write
$$x \sim U(a, b) \quad \text{where} \quad \quad a< b \quad$$ (\#eq:unif.pdf)
to state that the continuous random variable $x$ has a uniform distribution between a and b. This means that the probability of obtaining a value of x in an interval $[x_1, x_2]$ within [a,b] is constant and equal to $(x_2-x_1)/(b-a)$ \@ref(fig:unif.dist.fig)
Note that the uniform distribution U(0, 1) is the distribution of the random numbers generated by the function RAND() in Excel. For more details on this distribution, see [Uniform Distribution] (https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)#Minimum-variance_unbiased_estimator).
$$\begin{equation}
f(x) =
\begin{cases}
\frac{1}{b-a} & \quad \text{if } a \leq x \leq b \\
0 & \quad \text{otherwise }
\end{cases}
(\#eq:unif.pdf)
\end{equation}$$
(ref:fig.unif.dist) Probability density function for the uniform distribution that has a support between any two values of the x-axis is the area under the line between those values.
```{r unif.dist.fig, fig.cap='(ref:fig.unif.dist)'}
# code for a unif dist with an interval and area selected
plotUnif <- function(a,b) {
x <- seq(from = a - (b - a), to = b + (b - a), length.out = 200)
plot(dunif(x, a, b) ~ x, type = "l")
}
plotUnif(1, 4.5)
```
## Homework Problems
## Laboratory Exercises
### Plant Sciences
### Animal Sciences