How to use Deriv with a matrix structure in r - r

I am computing a partial derivative using Deriv for the function below but I keep getting errors probably because Deriv can not understand the symbols I am using within my defined function.
m1 and m2 have 3 elements each or you would think of it as a matrix of 3 columns and 2 rows where each row represents each m_i
I have defined the function f in r as:
f <- function(m,q){
structure(vapply(seq(3), function(j)
12 * log(sum(vapply(seq(2), function(i) exp(m[i,j]) * q[i], double(1))) + q[3]),
double(1)), dim=c(1,3))
}
The i's and j's are only useful because I am planning to broaden the function. Could there be a way I would define this function so that I am able to compute the f1 as defined above?
f1 = Deriv(f, "m[i, ]")
Or could there be any other package that could handle such a problem?

Related

How to split a "formula" in R

I'm trying to make a small R package with my limited knowledge in R programming. I am trying to use the following argument:
formula=~a+b*X
where X is vector, 'a' and 'b' are constants in a function call.
What I'm wondering is once I input the formula, I want to extract (a,b) and X separately and use them for other data manipulations inside the function call. Is there a way to do it in R?
I would really appreciate any guidance.
Note: Edited my question for clarity
I'm looking for something similar to model.matrix() output. The above mentioned formula can be more generalized to accommodate 'n' number of variables, say,
~2+3*X +4*Y+...+2*Z
In the output, I need the coefficients (2 3 4 ...2) as a vector and [1 X Y ... Z] as a covariate matrix.
The question is not completely clear so we will assume that the question is, given a formula using standard formula syntax, how do we parse out the variables names (or in the second answer the variable names and constants) giving as output a character vector containing them.
1) all.vars Try this:
fo <- a + b * X # input
all.vars(fo)
giving:
[1] "a" "b" "X"
2) strapplyc Also we could do it with string manipulation. In this case it also parses out the constants.
library(gsubfn)
fo <- ~ 25 + 35 * X # input
strapplyc(gsub(" ", "", format(fo)), "-?[0-9.]+|[a-zA-Z0-9._]+", simplify = unlist)
giving:
[1] "25" "35" "X"
Note: If all you are trying to do is to evaluate the RHS of the formula as an R expression then it is just:
X <- 1:3
fo <- ~ 1 + 2 * X
eval(fo[[2]])
giving:
[1] 3 5 7
Update: Fixed and added second solution and Note.
A call is a list of symbols and/or other calls and its elements can be accessed through normal indexing operations, e.g.
f <- ~a+bX
f[[1]]
#`~`
f[[2]]
#a + bX
f[[2]][[1]]
#`+`
f[[2]][[2]]
#a
However notice that in your formula bX is one symbol, you probably meant b * X instead.
f <- ~a + b * X
Then a and b typically would be stored in an unevaluated list.
vars <- call('list', f[[2]][[2]], f[[2]][[3]][[2]])
vars
#list(a, b)
and vars would be passed to eval at some point.

Need help vectorizing a double for-loop creating a matrix of norms of vector differences in R

I'm trying to figure out how to vectorize the following code block in R:
X is an N x M matrix
centers is a K x M matrix
phi <- matrix(0, nrow(X), nrow(centers))
for(i in 1:nrow(phi)) {
for(j in 1:ncol(phi)) {
phi[i, j] <- norm(as.matrix(X[i, ]) - as.matrix(centers[j, ]), type = 'F')
}
}
I'm constructing an N x K matrix, phi, which at each position, [i, j], contains the norm of the difference between the vectors at row i of X and row j of centers:
phi[i, j] = || X[i, ] - centers[j, ] ||
My approach so far has been to attempt to use R's outer() function. I'm new to the outer() function, so I've searched for several examples, however, the examples I've come across involve using outer() to apply some function to a pair of vectors of scalar values. As I'm dealing with the differences between pairs of rows from two matrices, outer() behaves different than expected. I'm not sure how to get it to recognize the matrices I'm passing it (X and centers) as vectors of vectors, where each row represents a vector to be involved in the computation of phi.
In addition, when I define a function to compute the norm of the difference between two M-length vectors, that function returns a scalar. It is my understanding that in order to vectorize a function using R's Vectorize(), that function must return a result of the same length as its arguments. I'm not sure how to define a function which, when used in conjunction with outer(), recognizes each row of a matrix as a single element (in spite of it being an M-length vector).
Below are a couple of my attempts to use outer() with toy examples of the matrices X and centers.
X <- matrix(c(7,8,9,1,2,3,4,5,6), 3, 3)
centers <- matrix(c(1,2,3,4,5,6), 2, 3)
fun <- function(y, x) norm(as.matrix(y) - as.matrix(x), type = 'F')
outer(X, centers, fun)
This was my first attempt. I was trying to use outer() in a manner analogous to the way it is used when it is passed a pair of vectors. I was (naively) hoping it would take one row from each matrix at a time, pass them as the two arguments to fun, and position the result appropriately in the product matrix. Instead, I get the following error message.
Error in outer(X, centers, fun) :
dims [product 54] do not match the length of object [1]
I also tried vectorizing my function using R's Vectorize() before calling outer().
Vecfun <- Vectorize(fun)
outer(X, centers, Vecfun)
In this case, I no longer get an error message, but the result is an erroneous matrix of matrices. I'm also new to the Vectorize() function, so I'm not too sure why it produces the result that it does as I don't have a real grasp on what it does; using it was sort of a shot in the dark.
I'll appreciate any help in vectorizing my original problem; I'm completely open to suggestions that do not involve outer().
Clarifications regarding outer() and Vectorize() also welcome.

parameter passing mechanism in R

The following function is used to multiply a sequence 1:x by y
f1<-function(x,y){return (lapply(1:x, function(a,b) b*a, b=y))}
Looks like a is used to represent the element in the sequence 1:x, but I do not know how to understand this parameter passing mechanism. In other OO languages, like Java or C++, there have call by reference or call by value.
Short answer: R is call by value. Long answer: it can do both.
Call By Value, Lazy Evaluation, and Scoping
You'll want to read through: the R language definition for more details.
R mostly uses call by value but this is complicated by its lazy evaluation:
So you can have a function:
f <- function(x, y) {
x * 3
}
If you pass in two big matrixes to x and y, only x will be copied into the callee environment of f, because y is never used.
But you can also access variables in parent environments of f:
y <- 5
f <- function(x) {
x * y
}
f(3) # 15
Or even:
y <- 5
f <- function() {
x <- 3
g <- function() {
x * y
}
}
f() # returns function g()
f()() # returns 15
Call By Reference
There are two ways for doing call by reference in R that I know of.
One is by using Reference Classes, one of the three object oriented paradigms of R (see also: Advanced R programming: Object Oriented Field Guide)
The other is to use the bigmemory and bigmatrix packages (see The bigmemory project). This allows you to create matrices in memory (underlying data is stored in C), returning a pointer to the R session. This allows you to do fun things like accessing the same matrix from multiple R sessions.
To multiply a vector x by a constant y just do
x * y
The (some prefix)apply functions works very similar to each other, you want to map a function to every element of your vector, list, matrix and so on:
x = 1:10
x.squared = sapply(x, function(elem)elem * elem)
print(x.squared)
[1] 1 4 9 16 25 36 49 64 81 100
It gets better with matrices and data frames because you can now apply a function over all rows or columns, and collect the output. Like this:
m = matrix(1:9, ncol = 3)
# The 1 below means apply over rows, 2 would mean apply over cols
row.sums = apply(m, 1, function(some.row) sum(some.row))
print(row.sums)
[1] 12 15 18
If you're looking for a simple way to multiply a sequence by a constant, definitely use #Fernando's answer or something similar. I'm assuming you're just trying to determine how parameters are being passed in this code.
lapply calls its second argument (in your case function(a, b) b*a) with each of the values of its first argument 1, 2, ..., x. Those values will be passed as the first parameter to the second argument (so, in your case, they will be argument a).
Any additional parameters to lapply after the first two, in your case b=y, are passed to the function by name. So if you called your inner function fxn, then your invocation of lapply is making calls like fxn(1, b=4), fxn(2, b=4), .... The parameters are passed by value.
You should read the help of lapply to understand how it works. Read this excellent answer to get and a good explanation of different xxpply family functions.
From the help of laapply:
lapply(X, FUN, ...)
Here FUN is applied to each elementof X and ... refer to:
... optional arguments to FUN.
Since FUN has an optional argument b, We replace the ... by , b=y.
You can see it as a syntax sugar and to emphasize the fact that argument b is optional comparing to argument a. If the 2 arguments are symmetric maybe it is better to use mapply.

How to build out more complex vectorized operations?

I have a sublist of principal component rotation vectors computed by prcomp, where each list item is an Nx2 array (i.e., two column vectors), for each class.
Using those vectors, I'd like to project some data similarly structured into a list of classes, each class item containing arrays with dimension NxMxT, where T is the number of trials.
My problem is, I can write simple vectorized functions with apply and its variants, but I'm having trouble generalizing this to apply that over each list.
Example data:
somedata <- list(array(rnorm(100),dim=c(5,4,5)),array(rnorm(100),dim=c(5,4,5)))
somevectors <- list(array(rnorm(10),dim=c(5,2)),array(rnorm(10),dim=c(5,2)))
Here is a simple example of the operation over each list element:
o.proj.1 <- apply(somedata[[1]],3,function(x){
t(somevectors[[1]]) %*% x
}) # returns an array where each projected trial is a column
I tried fitting this inside a call to lapply(), but didn't find much success:
lapply(somedata, y = somevectors, function(x,y){
apply(x,3,function(z){
t(y) %*% z
})
})
Error in t(y) %*% z : requires numeric/complex matrix/vector arguments
Basically my algorithm is to put the appropriate apply type (here lapply) around the more local function and remove the index that will be vectorized (here [[]]). What am I missing?
Of the *apply family of functions, mapply is the one to use when you want to loop simultaneously over two or more objects. Try:
o.proj <- mapply(function(x,y){
apply(x,3,function(z){
t(y) %*% z
})
}, somedata, somevectors, SIMPLIFY = FALSE)
I suppose you will want to use SIMPLIFY = FALSE to return a list, otherwise mapply will attempt to simplify your output into an array, a little like sapply does.
Also know that you can use Map as a shortcut for mapply(..., SIMPLIFY = FALSE).

Write a function that takes two arguments, x and n, and returns h(x, n). using FOR loop

I am trying to write a function in R that takes two arguments, x and n, and returns h(x, n); x=1
Does anyone know how to do this using a for loop?
The function I am working with is:
x^0 + x^1 + x^2...x^n
I have been working for a while on this and am not sure if I am doing this correctly.
Can anyone give me some guidance on how to do this problem.
Here is what I have..
n = seq(1,6, by = 1)
x = 1
h = function (x,n){
for (i in 0:n){
for( i in 1:n){
sum = sum +x^i
{
}}
h <- function( x, n ) sum( x^c(0:n) )
h( 1, 6 )
Loops are best avoided in R. First, you can use vectors in many situations; then, learn to use apply and friends (sapply, lapply etc.).
Do yourself a favor and use <- instead of = in assignments. It pays off in the long run.
Like in other programming languages, no need to declare the variables outside of the function (and anyways, since n is an argument to your function, your first assignment has no effect on the function)
Don't use seq() where a simple k:n will do.

Resources