For each of the problems below (except in cases where you are asked to discuss your interpretaion) write R code blocks that will compute appropriate solutions. A good rule of thumb for judging whether your solution is appropriately “computable” is to ask yourself “If I added or changed observations to this data set, would my code still compute the right solution?”
When completed, submit your R Markdown document (the file with the extension .Rmd) and the knit HTML via Sakai.
Each of the problems below is worth 2.5 pts.
Write a function, vec.dot(x,y), that calculates the dot product of two vectors, x and y, of arbitrary number of elements.
Write a function, vec.length(x), that calculates the Euclidean length of a vector.
Write a function vec.cosine(x,y), that calculates the cosine of the angle between two vectors using your vec.dot() and vec.length() functions.
Write a function vec.angle(x, y, deg=FALSE) that by defaults calculates the angle, in radians, between two vectors. When the argument deg=TRUE your function should return the angle in degrees.
Write a function, vec.distance(x,y) that calculates the distance between two vectors (i.e. distance between their end points).
Write a function vec.mean(x) that calculates the mean of x, using vector algebraic operations.
Write a function vec.cov(x,y) that calculates the covariance of x and y, using vector algebraic operations. Remember to mean center! Compare the results with built-in cov() function to make sure your function works properly.
Write a function vec.var(x) that calculates the variance of x, using vector algebraic operations. Remember to mean center! Compare the results with built-in var() function to make sure your function works properly.
Write a function vec.cor(x,y) that calculates the correlation of tow vectors, x and y, using vector algebraic operations. Remember to mean center! Compare the results with built-in cor() function to make sure your function works properly.