Dimitris.Kapetanakis
2011-Jul-07 18:30 UTC
[R] apply family functions (tapply, sapply, mapply etc)
Dear all,
I am trying to use apply or similar functions in order to improve the
efficiency of my code but I guess I am misunderstanding the function of
these commands. What I want to do is possible to see with the following code
that I use a for loop command. In words what I want to do are two things 1)
to multiply each row of a matrix with its transpose (the same row in a
column) for each row the matrix.
N<-250
x.1<-as.matrix(rnorm(N,0,1))
x.2<-as.matrix(rnorm(N,1,1))
x.3<-as.matrix(rnorm(N,1,1))
x.4<-as.matrix(rnorm(N,0,1))
X<-cbind(x.1, x.2,x.3,x.4)
q<-ncol(X)-1
h0<-0.3
Xu<-cbind(x.1,x.2,x.3)
beta.in<-as.matrix(c(1,1,1))
XX<-matrix(0,N*q,q)
Qn<-matrix(0,N*q,q)
DDS<-function(Betas, h, Xs){
v<-(Xs[,1:q]%*%Betas+Xs[,q+1])/h
((105/64)*(matrix(1,N,1)-10*v+28*v^3-18*v^5)*(v>-1)*(v<1))}
for (j in 1:N){
nq<-(j-1)*q+1
nq1<-j*q
Qn[nq:nq1,]<-Xu[j,]%*%t(Xu[j,])*(DDS(beta.in, h=h0, Xs=X)[j,])
XX<-Xu[j,]%*%t(Xu[j,])
}
for (j in 1:N){
nq<-(j-1)*q+1
nq1<-j*q
Q<-0+Qn[nq:nq1,]
}
Another issue that I try to solve is that I want to calculate the result of
let say a function "S" for every different row of the matrix, lets
call it
B, where the rows of B I want to be the argument Betas (that is a vector) of
function "S"
Sn <-function(Betas, h, Xs){
v<-(Xs[,1:q]%*%Betas+Xs[,q+1])/h
(2*Y-1)*(0.5+(105/64)*(v-(5/3)*v^3+(7/5)*v^5-(3/7)*v^7))*(v>-1)*(v<1)+matrix(1,N,1)*(v>=1)}
S <-function(Betas, h, Xs){colMeans(Sn(Betas, h, Xs))}
Any help would be highly appreciated
Thanks
Dimitris
--
View this message in context:
http://r.789695.n4.nabble.com/apply-family-functions-tapply-sapply-mapply-etc-tp3652276p3652276.html
Sent from the R help mailing list archive at Nabble.com.
On 07.07.2011 20:30, Dimitris.Kapetanakis wrote:> Dear all, > > I am trying to use apply or similar functions in order to improve the > efficiency of my code but I guess I am misunderstanding the function of > these commands. What I want to do is possible to see with the following code > that I use a for loop command. In words what I want to do are two things 1) > to multiply each row of a matrix with its transpose (the same row in a > column) for each row the matrix.This should be diag(crossprod(t(X)))> > N<-250 > x.1<-as.matrix(rnorm(N,0,1)) > x.2<-as.matrix(rnorm(N,1,1)) > x.3<-as.matrix(rnorm(N,1,1)) > x.4<-as.matrix(rnorm(N,0,1)) > X<-cbind(x.1, x.2,x.3,x.4) > q<-ncol(X)-1 > h0<-0.3 > Xu<-cbind(x.1,x.2,x.3) > beta.in<-as.matrix(c(1,1,1)) > > XX<-matrix(0,N*q,q) > Qn<-matrix(0,N*q,q) > DDS<-function(Betas, h, Xs){ > v<-(Xs[,1:q]%*%Betas+Xs[,q+1])/h > ((105/64)*(matrix(1,N,1)-10*v+28*v^3-18*v^5)*(v>-1)*(v<1))} > > for (j in 1:N){ > nq<-(j-1)*q+1 > nq1<-j*q > Qn[nq:nq1,]<-Xu[j,]%*%t(Xu[j,])*(DDS(beta.in, h=h0, Xs=X)[j,]) > XX<-Xu[j,]%*%t(Xu[j,]) > } > > for (j in 1:N){ > nq<-(j-1)*q+1 > nq1<-j*q > Q<-0+Qn[nq:nq1,] > } > > > Another issue that I try to solve is that I want to calculate the result of > let say a function "S" for every different row of the matrix, lets call it > B, where the rows of B I want to be the argument Betas (that is a vector) of > function "S" > > > Sn<-function(Betas, h, Xs){ > v<-(Xs[,1:q]%*%Betas+Xs[,q+1])/h > > (2*Y-1)*(0.5+(105/64)*(v-(5/3)*v^3+(7/5)*v^5-(3/7)*v^7))*(v>-1)*(v<1)+matrix(1,N,1)*(v>=1)} > > S<-function(Betas, h, Xs){colMeans(Sn(Betas, h, Xs))}apply(B, 1, S, h=h, Xs=Xs) Uwe Ligges> Any help would be highly appreciated > > Thanks > > Dimitris > > > -- > View this message in context: http://r.789695.n4.nabble.com/apply-family-functions-tapply-sapply-mapply-etc-tp3652276p3652276.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.