Returning function output on array of values in R - r

I'm a beginner R programmer struggling with a multivariate array problem.
I'm attempting to input an array of 4 parameter values, say a=1:10, b=1:10, p=1:10, q=1:10, into a function y=f(x|a, b, p, q) that calculates values of y based on my dataset, x, and every possible combination of the given 4 parameters [(a=1,b=1,p=1,q=1),(a=2,b=1,p=1,q=1),...,(a=10,b=1,p=1,q=1),...,(a=10,b=10,p=10,q=10)] = 10^4 = 10,000 possible combinations and therefore 10,000 y values.
Ideally I'd like the output to be in an array format which I can then graph in R, allowing each parameter to be plotted as a separate axis.
If anyone could point me in the right direction it would be much appreciated!
Thanks,
Robert

I agree with JD Long that the request is too vague to allow a final answer, but there is an answer to the first part:
all.comb.dfrm <- expand.grid(a=1:10, b=1:10, p=1:10, q=1:10)
all.comb.dfrm$Y <- with(all.comb.dfrm, f(a,b,p,q) )

Related

Implementing equations in R

I am new to R (also not too good at math) and I am trying to calculate this equation in R with some difficulties:
X is some integer data I have, with 550 samples.
Any help is appreciated since I am unsure how to do this. I think I have to use a for loop and the sum() function but other than that I don;t know.
R supports vectorisation, which means you very rarely need to implement for loops.
For example, you can solve your equation like so:
## I'm just making up a long numerical vector for x - obviously you can use anything
x <- 1:1000
solution <- sum(20/x)^0.5
Unless the brackets denote the integral, rather than the sum? In which case:
solution <- sum( (20/x)^0.5 )

Get the mapping from each element of input to the bin of the histogram in Julia

Matlab's [n,mapx] = histc(x, bin_edged) returns the counts of x in each bin as n and returns a map, which is the same length of x which is the bin index that each element of x was placed into.
I can do the same thing in Julia as follows:
Using StatsBase
x = rand(1000)
bin_e = 0:0.1:1
h = fit(Histogram, x, bin_e)
yx = map((z) -> findnext(z.<=h.edges[1],1),x) .- 1
Is this the "right way" to do this? It seem a bit kludgy.
Inspired by this python question you should be able to define a small function that delivers the desired mapping (modulo conventions):
binindices(edges, data) = searchsortedlast.(Ref(edges), data)
Note that the bin edges are sorted and we can use seachsortedlast to get the last bin edge smaller or equal than a datapoint. Broadcasting this over all of the data we obtain the mapping. Note that the Ref(edges) indicates that edges is a scalar under broadcasting (that means that the full array is considered in each call).
Although conceptionally identical to your solution, this approach is about 13x faster on my machine.
I filed an issue over at StatsBase.jl's github page suggesting to add this as a feature.
After looking through the code for Histogram.jl I found that they already included a function binindex. So this solution is probably the best:
x = 0:0.001:10
h1 = fit(Histogram,x,0:10,closed=left)
xmap1 = StatsBase.binindex.(Ref(h1), x)
h2 = fit(Histogram,x,0:10,closed=right)
xmap2 = StatsBase.binindex.(Ref(h2), x)
I stumbled across this question when I was trying to figure out how many occurrences of each value I had in a list of values. If each value is in its own bin (as for categorical data, or integer data with a small number of unique values), this is what one would be plotting in a histogram.
If that is what you want, then countmap() in StatBase package is just what you need.

Elements-wise matrix algebra in R arrays

I have two arrays with dimensions: arry1[2,2,n] and array2[2,2,n]. That is, n two by two matrices.
I want to produce a third array which is the n-element-wise matrix multiplication (i.e., %*%) of array1 and array2. Producing yet another array with dimensions: array3[2,2,n].
Frustratingly, I cannot figure out how to use %*% to pull this off, the following doesn't seem to work
array3 <- array1[1:2,1:2,]%*%array2[1:2,1:2,]
Moreover, the apply() family of functions don't appear to enable my operation.
Any help would be greatly appreciated.
If I understood correctly, this will work
array(sapply(1:5,function(x) a1[,,x]%*%a2[,,x]),dim = c(2,2,5))
Data
a1=array(outer(outer(1:2,1:2),1:5),dim = c(2,2,5))
a2=array(outer(outer(1:2,1:2),1:5),dim = c(2,2,5))

acf function in R return values

I would like to compute a simple acf in R. For example, consider:
v = acf(1:10, plot=F)
v now contains an element called lag which is a 3-dim array containing the last used in the estimation. How come this array is 3 dimensional and not simply 1 dimensional? I am asking because I would like to foresee exactly what will be in each dimension depending on the use-case.
You can have more than one column in your input data.
Try:
v = acf(data.frame(x=rnorm(10), y=rnorm(10)), plot=F)
then you get an array. I believe this is used for multivariate time series

using argmax or something simpler in R

I am trying to set up a Gibbs sampler in R where I update my value at each step.
I have a function in R that I want to maximise for 2 values; my previous value and a new one.
So I know the maximum outcome from the function applied to both values. But then how do I select the best input without doing it manually? (I need to do a lot of iterations). Here is an idea of the code and the variables:
g0<-function(k){sample(0:1,k,replace=T)}
this is a k dimensional vector with entries 1 or 0 uniformly. Initial starting point for my chain. If i=1 then include the i'th variable in the design matrix.
X1 design matrix
Xg<-function(g){
Xg<-cbind(X1[,1]*g[1],X1[,2]*g[2],X1[,3]*g[3],X1[,4]*g[4],X1[,5]*g[5],X1[,6]*g[6],X1[,7]*g[7])
return(Xg[,which(!apply(Xg,2,FUN = function(x){all(x == 0)}))])
}
Xg0<-Xg(g0)
reduced design matrix for g0
c<-1:100000
mp<-function(g){
mp<-sum((1/(c*(c+1)^-((q+1)/2)))*
(t(Y)%*%Y-(c/(c+1))*t(Y)%*%Xg(g)%*%solve(t(Xg(g))%*%Xg(g))%*%t(Xg(g))%*%Y)^(-27/2))
return(mp)
}
this is my function.
Therefore if I have mp(g) and mp(g*), for 2 inputs g and g*, such that the max is mp(g*) how can I return g*?
Thanks for any help and if you have any queries just ask. sorry about the messy code as well; I have not used this site before.
Like this:
inputs <- list(g, g2)
outputs <- sapply(inputs, mp)
best.input <- inputs[which.max(outputs)]

Resources