Notes for Wednesday 14 March

Math2501ExpectedValueForRVs-slideshow

 

MAT 2572                                                      Basic Histograms for PDFs in R

 

 

 

The basics: for a RV X with possible values a, b, …, n

 

Assign the values of the probabilities as a vector

(I’ll call it my_distribution, which is really not a very good name)

Then those probabilities will be the heights of the bars

The command barplot gives the bar graph

 

> my_distribution <- c(p(a), p(b), …, p(n))

> barplot(my_distribution)

 

But this gives a bar graph with space between the bars. To make it a histogram, we make the space between the bars be 0:

 

> barplot(my_distribution, space=0)

 

barplot also has a color attribute col, which can be set like this:

 

> barplot(my_distribution, space=0, col=“blue”)

 

Experiment with the col attribute to see which colors R will recognize!

(It’s not always a good idea to put colors in graphs, because of readability and psychological issues though.)

 

To put labels on the graph (for the values of X), If the values run through a consecutive sequence of integers a through n

> names(my_distribution) <- as.character(a:n)

 

 

Saving your work as a script:

menu at upper left corner

 

 

Exporting the graph to use it in a document, for example:

Plots > export…

https://support.rstudio.com/hc/en-us/articles/200484448-Editing-and-Executing-Code