Question 1. Captain Jack is convinced that he can predict how much gold he will find on an island with the following equation: (a * b) - c * 324 + log(a), where a is the area of the island in square meters, b is the number of trees on the island, and c is how drunk he is on a scale of 1 to 10. Create a function called Jacks.Equation that takes a, b, and c as arguments and returns Captain Jack’s predictions. Test your function for an island with an area of 1,000 square meeters that contains 30 trees when Jack is at a 7 / 10 on a drunkenness scale.
Jacks.Equation <- function(a, b, c) {
output <- (a * b) - c * 325 + log(a)
return(output)
}
Jacks.Equation(a = 1000, b = 30, c = 7)
## [1] 27731.91
# He will find 27731.91 units of gold.
Question 2. Write a function called standardize.me that takes a vector x as an argument, and returns a vector that standardizes the values of x (standardization means subtracting the mean and dividing by the standard deviation).
standardize.me <- function(x) {
output = (x - mean(x))/sd(x)
return(output)
}
data <- c(6, 3, 8, 6, 3, 2, 3, 2, 100)
standardize.me(data)
## [1] -0.2740789 -0.3677514 -0.2116305 -0.2740789 -0.3677514 -0.3989756
## [7] -0.3677514 -0.3989756 2.6609937
Question 3. Write a function called remove.outliers that takes a vector as an argument, determines which values of the vector are outliers, and returns a vector with the outliers removed. Define an outlier as any value that is less than 2 standard deviations below the mean, or more than 2 standard deviations above the mean.
When you are finished writing the function, run these commands to make sure your function works
remove.outliers <- function(x) {
is.outlier <- x < (mean(x) - (2 * sd(x))) | x > (mean(x) + (2 * sd(x)))
new.vec <- x[is.outlier == FALSE]
output <- new.vec
return(output)
}
data <- c(rep(1, 100), -529484903)
mean(data)
## [1] -5242424
mean(remove.outliers(data))
## [1] 1
Question 4. Write a function called how.many that takes two arguments (data and value). The function should return a value indicating how many times the element value occured in the vector data
Try running your function with these arguments:
how.many <- function(data, value) {
log.vec <- data == value
n.true <- sum(log.vec)
return(n.true)
}
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = 1)
## [1] 4
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = -100)
## [1] 0
Question 5. Write a function called madlib that takes three strings as arguments, and returns the following sentence with the string arguments inserted into the following text:
“If you talk to an ADJECTIVE pirate like NAME you may find that he/she spends more time talking about PLURALNOUN than the pirate arts.”
Your three arguments to the function should be:
adjective, a string indicating an adjective name, a string of a person’s name plural.noun, a string indicatinga plural noun For example, if you run your function with the arguments “ADJECTIVE”, “NOUN” and “PLURAL NOUN”, your function should return the following values.
madlib <- function(a,b,c) {
output <- paste("If you talk to an", a, "pirate like", b, "you may find that he/she spends more time talking about", c, "than the pirate arts")
return(output)
}
madlib("ADJECTIVE", "NOUN", "PLURAL NOUN")
## [1] "If you talk to an ADJECTIVE pirate like NOUN you may find that he/she spends more time talking about PLURAL NOUN than the pirate arts"
Question 6. Write a function called ttest.apa that takes a numeric vector as an argument, conducts a one-sample t.test on that vector, and returns a string summarising the test in APA style. Your function should have 3 arguments
x, a vector of data null, the population mean under the null hypothesis p.critical, the critical value for determining significance Once you have programmed your function, test it on these data:
#ttest.apa <- function(x, null, p.critical) {
# t.test(x)
# if(p.critical < 0.05) {print ("A one sample t-test was significant (t(8) # = 13.92, p = 0). We reject the null hypothesis that the true population #mean is 0")}
Question 7. Create a function called three.var.scatter that takes three vectors of numeric data, x, y and z, and creates a scatterplot where the vector z is used in constructing the plot. Here are the arguments
x, y, z: Three vectors of numerical data. x and y are the data for the x and y axes, while z can be used for the size and color of the plotting points. point.size.log: A logical value indicating whether or not to make the size of the points depend on the vector z (hint, use cex) point.col.log: A logical value indicating whether or not to make the color of the points depend on the vector z (hint, use gray()) mean.lines.log: A logical value indicating whether or not to add lines showing the means of x and y regression.log: A logical value indicating whether or not to add a regression line Here are some examples of three.var.scatter in action. Try them on your function!