I. R Nuts and Bolts/Programming Readings
Data classes basically just means the type of data that R represents data in. It could be numerical, double, boolean or a character.
Data structures on the other hand is a way to organize that data classes in a structural way. It could be organize using a vector (a single dimension array that takes in the same data classes), an array (two-dimension array that also takes in data classes), matrix (similar to an array to be honest with) or a list (a flexible list that can include multiple lists that can take in different kind of data classes).
The data set I am going to choose from is cars
library(psych)
describe(cars)
## vars n mean sd median trimmed mad min max range skew kurtosis
## speed 1 50 15.40 5.29 15 15.47 5.93 4 25 21 -0.11 -0.67
## dist 2 50 42.98 25.77 36 40.88 23.72 2 120 118 0.76 0.12
## se
## speed 0.75
## dist 3.64
I will use the sapply() to check if every column in the data frame is numeric. In addition to that, I assign the data table to x then use the function c(cars) to check for what kind of data structure represents the cars data set.
sapply(cars, is.numeric)
## speed dist
## TRUE TRUE
x <- c(cars)
class(x)
## [1] "list"
II. Reading and Writing Functions in R
The 2 functions that I am going to talk about is the function mad which calculates the Median Absolute Deviation and sd which simply calculates the standard deviation.
mad
## function (x, center = median(x), constant = 1.4826, na.rm = FALSE,
## low = FALSE, high = FALSE)
## {
## if (na.rm)
## x <- x[!is.na(x)]
## n <- length(x)
## constant * if ((low || high) && n%%2 == 0) {
## if (low && high)
## stop("'low' and 'high' cannot be both TRUE")
## n2 <- n%/%2 + as.integer(high)
## sort(abs(x - center), partial = n2)[n2]
## }
## else median(abs(x - center))
## }
## <bytecode: 0x0000026cd94a6238>
## <environment: namespace:stats>
sd
## function (x, na.rm = FALSE)
## sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x),
## na.rm = na.rm))
## <bytecode: 0x0000026cd947a540>
## <environment: namespace:stats>
Here is my function, I will write a function that calculate the range (my version instead of using base R version)
x <- cars
my_range_function = function(column){
maxvalue = max(column, na.rm = TRUE)
minvalue = min(column, na.rm = TRUE)
return(maxvalue - minvalue)
}
my_range_function(x$speed)
## [1] 21
Concept is simple, you load the the data set inside x then write the function that take in the desired columns of the data set where it will do basic arithmetic of calculating the range between two values by taking the higher (max) values subtract the smaller (min) one.
III. Please explain Bayes Theorem in your own words, and give an example. Less than 10
sentences.
So from my understanding, we have the general law of conditional probability where the probability of an event A is happening under the condition that event B has happened. Bayes Thereon is an extension of that where it introduce the new probability of taking into account of new information be presented. The most famous example would be there is a test for a rare disease with 99% accurate on a sampled of 100 people. In that 100 people, if we just use the general law of conditional probability, there is 1% or 1 people in that group will have this rare disease. However, the conditional probability do not take into account of how rare the disease is so even if that 1 person get the positive result, there may still a chance that they do not get the disease.
The Bayes’s formula when written in LateX:
\[P(A \mid B) = \frac{P(B \mid A) * P(A)}{P(B)}\]
IV.
Let A = academic event
Let S = sport event
Let N = no event
And the condition for the lot is full = B
prob_A <- 0.35 #P(A)
prob_S <- 0.2 #P(S)
prob_N <- 0.45 #P(N)
prob_B_A <- 0.25 #P(B|A)
prob_B_S <- 0.7 #P(B|S)
prob_B_N <- 0.05 #P(B|N)
#Applying the Thereom, we have:
result = prob_B_S * prob_S / ((prob_B_S * prob_S) + (prob_B_A * prob_A) + (prob_B_N*prob_N))
result
## [1] 0.56
Based on the calculation we can see that there is a 56% probability that a sporting event is hosting on the campus.
Draw tree diagram
library(Rgraphviz)
## Loading required package: graph
## Loading required package: BiocGenerics
## Loading required package: generics
##
## Attaching package: 'generics'
## The following objects are masked from 'package:base':
##
## as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
## setequal, union
##
## Attaching package: 'BiocGenerics'
## The following objects are masked from 'package:stats':
##
## IQR, mad, sd, var, xtabs
## The following objects are masked from 'package:base':
##
## anyDuplicated, aperm, append, as.data.frame, basename, cbind,
## colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
## get, grep, grepl, is.unsorted, lapply, Map, mapply, match, mget,
## order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
## rbind, Reduce, rownames, sapply, saveRDS, table, tapply, unique,
## unsplit, which.max, which.min
## Loading required package: grid
nodes <- c("Start", "A", "S", "N",
"B_A", "notB_A",
"B_S", "notB_S",
"B_N", "notB_N")
g <- new("graphNEL", nodes = nodes, edgemode = "directed")
g <- addEdge("Start", "A", g)
g <- addEdge("Start", "S", g)
g <- addEdge("Start", "N", g)
g <- addEdge("A", "B_A", g)
g <- addEdge("A", "notB_A", g)
g <- addEdge("S", "B_S", g)
g <- addEdge("S", "notB_S", g)
g <- addEdge("N", "B_N", g)
g <- addEdge("N", "notB_N", g)
edgeLabels <- c(
"Start~A" = "0.35",
"Start~S" = "0.2",
"Start~N" = "0.45",
"A~B_A" = "0.25",
"A~notB_A" = "0.75",
"S~B_S" = "0.7",
"S~notB_S" = "0.3",
"N~B_N" = "0.05",
"N~notB_N" = "0.95"
)
nodeLabels <- c(
Start = "Full Parking",
A = "Academic",
S = "Sport",
N = "No Event",
B_A = "B | A",
notB_A = "not B | A",
B_S = "B | S",
notB_S = "not B | S",
B_N = "B | N",
notB_N = "not B | N"
)
eAttrs <- list(label = edgeLabels)
nAttrs <- list(label = nodeLabels)
attrs <- list(
node = list(shape = "ellipse", fixedsize = FALSE, fontsize = 12),
edge = list(fontsize = 10)
)
plot(g, "dot", attrs = attrs, nodeAttrs = nAttrs, edgeAttrs = eAttrs)