1/33
Looks like no tags are added yet.
What does Is() do?
Shows what has been defined in the environment
What is the output of the following code —> sum(c(TRUE,TRUE,FALSE,TRUE))
3 because a logic value is 1 for TRUE and 0 for FALSE
What is the output of the code c(1,10,100) * 1.5?
1.5 15.0 150.0
Given x ← c(a = 10, b = 20, c = 30)
x1 ← x[c(2,3)]
Are x[2] and x1[2] the same? And are x[‘b’] and x1[‘b’] the same? Briefly explain why or why not
x[2] and x1[2] are not the same because x[2] refers to b 20 and x1[2] refers to c 30, since the second position of x1 is the third position of x
x[‘b’] and x1[‘b’] are the same because they refer to names over position and since both x and x1 have ‘b’, they both output b = 20
What is the difference between identical() and all.equal()
Identical() asks if these two objects are the exact same, down to how they look
If you had 0.5-0.3 and 0.3-0.1, it would produce FALSE because they are not the same despite both equaling 0.2
all.equal() asks if these values are equal, so in the previous example, it would be TRUE because both statements equate to 0.2
TRUE or FALSE
Run the code xx <- c(a=1, b=2)
Object xx does not have an attribute "names", because it does not use function attr() to add this attribute
FALSE because the names a and b auto make a names attribute for xx even if you didn’t use attr()
What does “rep” do?
rep Replicates the elements in a vector, it works like this
rep( value(s), x amount of times)
So rep(1,5) = 1 1 1 1 1 and
rep(c(1,2,3),4) = 1 2 3 1 2 3 1 2 3 1 2 3
What happens if you rep(c(1,2,3), c(2,1,4)
1 1 2 3 3 3 3
The number in each vector comes out as the amount on the other vector, so since it’s 2 in the first vector, it’s going to be 2 1s
What does “seq” do
seq makes a sequence of equally spaced numbers on a linear scale
so seq(1,15,2) works like (start, end, slope). This comes out as
1 3 5 7 9 11 13 15
How to use if and else together
Start with the if
if (x > 0.5) {
Then use a curly bracket to group the expression
cat(“head\n)
y ← 1
Then use else
} Else {
cat(“tail\n”)
y ← 0
}
Logical Conditions
FALSE || TRUE = TRUE
NA || TRUE = TRUE
FALSE | TRUE = TRUE
FALSE && TRUE = FALSE
NA && TRUE = NA
T || F || F = TRUE
How to bind a name to a function and use it
Do name ← function(x)
Then put in your function > sqrt(mean(x²))
What does body() do for a function
Shows what the body formula for the function is
What does formals() do for a function
Shows what the parameters are for the function (x,p)
What does return() do for a function
It returns a value from a function,
What happens if you don’t specify the input for a return function
It uses the default value
Decomposition
Breaking down a complex problem or system into smaller more manageable parts
Abstraction
Focusing on the important info only, ignoring irrelevant detail
Algorithms
Developing a step-by-step solution to the problem, or the rules to follow to solve the problem
What does prop.trim do
Removes all outliers and trims the same proportions on both sides
What does stopifnot() do
It’s a function that will stop any input if stopifnot() is NOT true
What does test_that do?
Groups a few checks and gives them a name. Works when something breaks which will spew out a sentence
What does expect_equal do?
Compares with a numerical tolerance, not tripping over the last decimal place
What does expect_error and expect_warning do
Checks a bad input and refuses it
What does Map do?
Map calls a function on each element of a sequence in order to transform: their individual components (sqrt,round), or the corresponding elements of many sequences
Useful in replacing loops
What does Reduce do?
Reduce applies a binary operation to combine consecutive elements in a sequence, e.g., to generate the aggregates
What does Filter do?
Creates a subset of a sequence that is comprised of elements that enjoy a given property
What does Find do?
Locates the first element that fufills some logical condition
What does lobstr::ast() do?
Displays the underlying tree structure
Like this
## █─f
## ├─a
## └─"b"How to add x,y and a title to a ggplot
Using the ggplot() function, you can add points with geom_point(), setthe x and y aesthetics with aes(x,y) and use the labs(title, x, y) to add labels to the plot
Acceptance-rejection sampling method transform the samples simulated from the working distribution and then accept them
TRUE
What is true of acceptance-rejection sampling
It needs the target density only up to a constant
The proposal must have heavier tails than the target
The accepted values are exact draws from the target
Using acceptance-rejection with utility distribution, how many samples do you need
c * n samples, where c = some constant and n is the amount of samples