I. Nuts & Bolts
Data types lets you store information of a variable in different types. The class of the data lets R process it in a particular way. Each data class has its own methods of manipulating that type of data. For example, numeric variables are represented by numbers,and can be manipulated by mathematic operations (add,subtract,multiply,etc).Another example would a logical variable, that is represented by either TRUE, FALSE, or NA values. It can helps you validate expressions, calculations, and more.
Data structures, on the other hand, are ways to store, arrange, and manipulate data in an efficient way. Some data structures requires the data stored within to be of the same type (numeric, char, etc.), like vectors, matrices, arrays, and other data structutres can store multiple types of data, like lists, and data frames. Each data structre has its own built in functions to help manipulate the data. For example, data frame is one of the most common data structures. It has rows and column (2 dimensions), that has to be of the same length. It has its own functions, for example dim(), that gives you the dimensions(rows,columns) of the data frame, which helps you access and understand data inside the data frame easier.
data("FrozenJuice")
#?FrozenJuice
str(FrozenJuice)
## Time-Series [1:612, 1:3] from 1950 to 2001: 43.6 52.1 46.6 46.6 46.6 46.6 46.6 46.6 46.6 39.8 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:3] "price" "ppi" "fdd"
class(FrozenJuice)
## [1] "mts" "ts"
typeof(FrozenJuice)
## [1] "double"
I chose the frozen juice data set. This data set gives the monthly data on the price of frozen orange juice concentrate and temperature in the orange-growing region of Florida, tracked from 1950 to 2001. The data set has 3 numeric variables: price (Average producer price for frozen orange juice), ppi (Producer price index for finished goods. Used to deflate the overall producer price index for finished goods to eliminate the effects of overall price inflation), and fdd (Number of freezing degree days at the Orlando, Florida, airport. Calculated as the sum of the number of degrees Fahrenheit that the minimum temperature falls below freezing in a given day over all days in the month)
Applying the class() and typeof() commands I found that the frozen juice that the data set is of class time series, specifically a multivariate time series, meaning that multiple variables are tracked over time. The data stored inside the data frame is of type double.
It makes sense because the data frame tracks 3 numeric variables over a period of time. It shows the affect of freezing days, on the price of frozen orange juice, taking in account the price index for finished goods.
The first function I chose is mean() which should take numeric variables input , sum all of the observations, and divide it by the number of observations. This function should give you the expected value(average) of the row of data.I chose to take the mean of the ppi (producer price index) variable.
mean(FrozenJuice[,"ppi"])
## [1] 71.37387
The second function I chose is IQR. This function will find you middle 50% of the observations for a specific variable. This function takes numeric variables, and makes a couple of mathematical operations to find the interquartile range. It takes the median of the observations, then finds Q1 and Q3 by taking the medians of the lower and upper halves of the data, and then subtract Q3 minus Q1 to find the IQR. I chose to take the mean of the price (average producer price) variable.
IQR(FrozenJuice[,"price"])
## [1] 65.125
For writing my own function, I decided to write a simple function that shows the real average price of the frozen orange juice, given by dividing the price variable by the ppi variable. Then, I will convert the answer from dollars (original) to Israeli Shekels. The exchange rate of 1 dollar is 3.03 Israeli Shekels.
real_juice_price_in_ILS = function(){
# Taking the real price
real_prices <- (FrozenJuice[,"price"]) / FrozenJuice[, "ppi"]
# Averaging the data in the set
average_real_price <- sum(real_prices) / length(real_prices)
# Converting to ILS
return (average_real_price * 3.03)
}
real_juice_price_in_ILS()
## [1] 3.31353
An example can be while knocking on a door, there is an initial probability of having a dog in the house. If you hear barking inside the house,the probability of having a door in the house is updated by the new evidence.
\[ P(A \mid B) = \frac{P(B \mid A) \times P(A)}{P(B)} \]
First, lets name the probabilities and solve using Bayes’ formula.
#Academic event
P_Aevent <- 0.35
#Sports event
P_Sevent <- 0.2
#No event
P_Nevent <- 0.45
P_AeventB <- (0.35*0.25)/0.35
#P_AeventB = 0.25
P_SeventB <- (0.2*0.7)/0.2
#P_SeventB = 0.7
P_NeventB <- (0.45*0.05)/0.45
#P_NeventB=0.05
P_BSevent <- (P_SeventB*P_Sevent)/((P_SeventB*P_Sevent) + (P_NeventB*P_Nevent) + (P_AeventB*P_Aevent))
P_BSevent
## [1] 0.56
Given the fact that the garage is full, there is a chance of 56% that there is a sporting event.
I downloaded the data.tree library, and manually added the tree nodes and their corresponding probability.
library(data.tree)
garage <- Node$new("Parking Garage")
#Academic event branch and its probabilities
academic <- garage$AddChild("Academic Event")
academic$p <- 0.35
academic_Full <- academic$AddChild("Full garage")
academic_Full$p <- 0.25
academic_Available <- academic$AddChild("Spaces available")
academic_Available$p <- 1-academic_Full$p
#Sports event branch and its probabilities
sports <- garage$AddChild("Sports Event")
sports$p <- 0.2
sports_Full <- sports$AddChild("Full garage")
sports_Full$p <- 0.7
sports_Available <- sports$AddChild("Spaces available")
sports_Available$p <- 1 - sports_Full$p
#No event branch and its probabilities
no_event <- garage$AddChild("No Event")
no_event$p <- 0.45
no_event_full <- no_event$AddChild("Full garage")
no_event_full$p <- 0.05
no_event_Available <- no_event$AddChild("Spaces available")
no_event_Available$p = 1- no_event_full$p
#Adding the probability to each edge
SetEdgeStyle(
garage,
label = function(node) {
if (is.null(node$p)) {
""
} else {
paste0(node$p * 100, "%")
}
}
)
plot(garage)
full_sports_event <- (sports_Full$p*sports$p)/((sports_Full$p*sports$p) + (no_event_full$p*no_event$p) + (academic_Full$p*academic$p))
full_sports_event
## [1] 0.56