I. Nuts & Bolts

Data classes refer to the type of object each variable represents. The class determines how R processes the data. For example, numeric data, which is data that is presented as numbers can be added or subtracted, but the same functionality cannot be applied to character data, which is sequence of characters (digits, letters, punctuation, etc.). Logical data has three possible values: true, false, and NA (can also be stored as yes/no/NA). For example, if you have data looking at wages and education, one column could be: does the subject have a bachelor’s degree?

A data structure describes the method by which data is organized and stored. Common R data structures include vectors, lists, matrices, and data frames. Data frames seem to be the most common data structure for the kind of data we have been looking at so far. A data frame is a two-dimensional structure (multiple rows and columns) where each column (variable) can hold a different type (class) of data. Data frames resemble a basic Excel spreadsheet. Importantly, though, each column has to be of the same length. An example of a basic data frame is the Titanic data we looked at last week. A vector is a one-dimensional array of data; essentially a single-column data frame. In the Titanic data, the column “Age” would be a vector. A matrix is a mix of a data frame and vector; it is two-dimensional, like the data frame, but all the data has to be of the same class, like a vector. Last week, we made a matrix when we had to provide a cross-tabulation of Survived and Sex in the Titanic data.

library(AER)
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: survival
data("CPSSW8")
CPSdata<-CPSSW8
str(CPSdata)
## 'data.frame':    61395 obs. of  5 variables:
##  $ earnings : num  20.67 24.28 10.15 8.89 6.41 ...
##  $ gender   : Factor w/ 2 levels "male","female": 1 1 1 2 2 2 1 1 1 2 ...
##  $ age      : int  31 50 36 33 56 52 30 41 37 44 ...
##  $ region   : Factor w/ 4 levels "Northeast","Midwest",..: 3 3 3 3 3 3 4 3 3 3 ...
##  $ education: int  14 12 12 10 10 12 16 12 13 13 ...
class(CPSdata)
## [1] "data.frame"
typeof(CPSdata)
## [1] "list"

The CPSSW8 dataset has a data structure of dataframe. There are 5 variables. Earnings data is numeric, and education and age data are integers. Region and gender data are both character. Gender has two levels (male/female) and region has 4 levels (northeast, midwest,south, and west).

It makes sense that earnings would be numeric. Numeric data indicates that the data is a number and contains decimals. The data is showing average hourly earnings based on people’s gender, age, region, and education (see p. 29 of this doc (https://spout.ussg.indiana.edu/CRAN/web/packages/AER/AER.pdf) which explains the data). Because the values represent an average, it is logical that they would not be whole numbers. It further makes sense that age and education are integers, since these variables show age and amount of education measured in whole years.

  1. Reading and Writing Functions

One function I looked at in R was mean(), which calculates the average. My basic understanding of this function is that it adds the values together and then divides them by the total number of values. This function would require numeric or integer data to operate.

mean(CPSdata$age)
## [1] 41.2314
sum(CPSdata$age)/nrow(CPSdata)
## [1] 41.2314

Another function I looked at was sd(), which calculates the standard deviation. To calculate the standard deviation, R would have to first calculate the mean of the data, find the difference between each observation and that mean, square those differences, sum them together, divide that sum by n-1 and take the square root. I get the same value using those individual steps and the sd() function. This function would also require numeric or integer inputs.

my_vector<-c(1,2,3,4)
mean(my_vector)
## [1] 2.5
diffsquares<-(1-2.5)^2+(2-2.5)^2+(3-2.5)^2+(4-2.5)^2
diffsquares_n1<-diffsquares/3
sqrt(diffsquares_n1)
## [1] 1.290994
sd(my_vector)
## [1] 1.290994

I chose to convert the fuel price in the USAirlines data from the AER package from USD to GBP, using the average exchange rate in 2003: 0.6117.

library(AER)
library(ggplot2)
data("USAirlines")
firm1<-subset(USAirlines,firm=="1")
numeric_years<-as.numeric(as.character(firm1$year))
print(firm1$price)
##  [1] 106650 110307 110574 121974 196606 265609 263451 316411 384110 569251
## [11] 871636 997239 938002 859572 823411
prices_GBP<-firm1$price*0.6117
print(prices_GBP)
##  [1]  65237.81  67474.79  67638.12  74611.50 120263.89 162473.03 161152.98
##  [8] 193548.61 234960.09 348210.84 533179.74 610011.10 573775.82 525800.19
## [15] 503680.51
plot(x=numeric_years,y=firm1$price,xlab="Year",ylab="Price",col="blue",type = "l")
lines(x=numeric_years,y=prices_GBP,type="l")
legend("topleft",legend = c("Price USD", "Price GBP"), col=c("blue","black"),lty = 1)

  1. Bayes’ Theorem

Bayes’ Rule is an application of the Law of Conditional Probability, which refers to the probability of an event occurring given that another event already occurred. The  Rule allows one to adjust the probability of the secondary event based on newly available information, according to historical probability and new likelihoods. A basic application is in game theory: for example, if there are two politicians deciding whether to run a negative political ad, there is an initial probability for the likelihood that both candidates go negative. After the first round of ads is released, the Bayes’ Rule allows us to update the probability that both candidates will go negative based on how they both acted in the first round.

#p_A: Probability of event A, p(A) #p_BA: Probability of event B given event A, p(B|A) #p_B: Probability of event B, P(b)

\[\displaystyle \frac{P(A \mid B)* P(A)}{P(B)}\] IV. Problem 3.43

First I solved using Bayes’ Rule:

P_A1<-0.2
P_BA1<-0.7
P_A2<-0.35
P_BA2<-0.25
P_A3<-0.45
P_BA3<-0.05
P_A1B<-(P_BA1*P_A1)/((P_BA1*P_A1)+(P_BA2*P_A2)+(P_BA3*P_A3))
P_A1B
## [1] 0.56

Then, I created the tree using the data.tree package. I first created each node and then assigned the nodes their respective probabilities.

This link was really helpful: https://cran.r-project.org/web/packages/data.tree/vignettes/data.tree.html#tree-creation

library(data.tree)
trees<-Node$new("Tree")
tree<-Node$new("Tree")
Academic<-tree$AddChild("Academic")
Academic_Full<-Academic$AddChild("Academic Full")
Academic_Available<-Academic$AddChild("Academic Available")
Sporting<-tree$AddChild("Sporting")
Sporting_Full<-Sporting$AddChild("Sporting Full")
Sporting_Available<-Sporting$AddChild("Sporting Available")
None<-tree$AddChild("None")
None_Full<-None$AddChild("None Full")
None_Available<-None$AddChild("None Available")
tree$Academic$p<-0.35
tree$Sporting$p<-0.2
tree$None$p<-0.45
tree$Academic$`Academic Full`$p<-0.25
tree$Academic$`Academic Available`$p<-0.75
tree$Sporting$`Sporting Full`$p<-0.7
tree$Sporting$`Sporting Available`$p<-0.3
tree$None$`None Full`$p<-0.05
tree$None$`None Available`$p<-0.95
plot(tree)

Then I wanted to add the probabilities onto the tree image.

SetEdgeStyle(tree,
label = function(node) {if (is.null(node$p)) "" else paste0(node$p * 100, "%")})
plot(tree)

Then I added the overall probabilities for each conditional node.

tree$Academic$`Academic Full`$overall <- 
  tree$Academic$p * tree$Academic$`Academic Full`$p

tree$Academic$`Academic Available`$overall <- 
  tree$Academic$p * tree$Academic$`Academic Available`$p

tree$Sporting$`Sporting Full`$overall <- 
  tree$Sporting$p * tree$Sporting$`Sporting Full`$p

tree$Sporting$`Sporting Available`$overall <- 
  tree$Sporting$p * tree$Sporting$`Sporting Available`$p

tree$None$`None Full`$overall <- 
  tree$None$p * tree$None$`None Full`$p

tree$None$`None Available`$overall <- 
  tree$None$p * tree$None$`None Available`$p
print(tree,"p","overall")
##                     levelName    p overall
## 1  Tree                         NA      NA
## 2   ¦--Academic               0.35      NA
## 3   ¦   ¦--Academic Full      0.25  0.0875
## 4   ¦   °--Academic Available 0.75  0.2625
## 5   ¦--Sporting               0.20      NA
## 6   ¦   ¦--Sporting Full      0.70  0.1400
## 7   ¦   °--Sporting Available 0.30  0.0600
## 8   °--None                   0.45      NA
## 9       ¦--None Full          0.05  0.0225
## 10      °--None Available     0.95  0.4275

Then I wanted to add the overall probabilities onto the image.

SetNodeStyle(tree,label = function(node) {if (node$isLeaf) {paste0(node$name,"\n",sprintf("%.2f%%", node$overall*100))} else {node$name}})
plot(tree)

Then I used these probabilities to calculate the likelihood that there was a sporting event.

Full_Overall<-Sporting_Full$overall+Academic_Full$overall+None_Full$overall
Sporting_Full$overall/Full_Overall
## [1] 0.56