I’m trying to help my girlfriend with a task for a class with R.
I don’t have idea very things about the code, I know C++, but R I don’t know.
I was reading the documentation but if can you help me, that would be great!
The following code is from this website.
Part I of Code:
# rm(list=ls(all=TRUE))
# It's discourteous to wipe out your responder's entire workspace
install.packages("abind")
library(abind)
simHPP<-function(lambda,T,N){
# lambda: scalar, intensity of the Poisson process
# T: scalar, time horizon
# N: scalar, number of trajectories
EN <- rpois(N,lambda*T)
y <- matrix(T,nrow=2*max(EN)+2,ncol=N)*matrix(1,nrow=2*max(EN)+2,ncol=N)
yy <- abind(y,matrix(1,nrow=2*max(EN)+2,ncol=N)*EN,along=3)
i=1
while(i<=N){
if(EN[i]>0){
yy[1:(2*EN[i]+1),i,1] <- c(0,rep(sort(T*runif(EN[i])),each=2))
} else {
yy[1,i,1]=0
}
yy[1:(2*EN[i]+2),i,2] <- c(0,floor((1:(2*EN[i]))/2),EN[i])
i=i+1
}
return(yy)
}
The function abind, how is it working? The operator of assignment (=) in R, looks like a (<-) ?
And, for example, what is this doing?
yy[1:(2*EN[i]+1),i,1] <- c(0,rep(sort(T*runif(EN[i])),each=2))
There is a code review website, and that might be appropriate since you are asking (in part) for interpretation of code for which you have not given context.
(t’s confusing, although not an error, to use capital T for a vector name since the default assignment to that name is logical TRUE.)
abindtakes arrays as arguments and returns augmented arrays. If you have two 2 x 2 x 2 arrays and youabindthen you get a 2 x 2 x 4 array