rusers.sh
2009-Dec-14 02:58 UTC
[R] Confused on using expand.grid(), array(), image() and npudens(np) in my case
Hi all,
I want to use the npudens() function in the np package (multivariate
kernel density estimation), but was confused by the several functions in the
following codes,expand.grid(),array(),image() and npudensbw().
This confusion will only be generated in >=3 dimensions. I marked the four
places with confusion1-4. I think there should be some kind
of correspondence in those four places,but cannot figure them out.Thanks
very much for chewing on this.
#simulated dataset: d
x1<-c(runif(100,0,1),runif(50,0.67,1));y1<-c(runif(100,0,1),runif(50,0.67,1));d1<-data.frame(x1,y1);colnames(d1)<-c("x","y")
x2<-c(runif(100,0,1),runif(50,0.33,0.67));y2<-c(runif(100,0,1),runif(50,0.33,0.67));d2<-data.frame(x2,y2);colnames(d2)<-c("x","y")
x3<-c(runif(100,0,1),runif(50,0,0.33));y3<-c(runif(100,0,1),runif(50,0,0.33));d3<-data.frame(x3,y3);colnames(d3)<-c("x","y")
d<-rbind(d1,d2,d3)
d$tf<-c(rep(1,150),rep(2,150),rep(3,150))
plot(d1);points(d2,col="red");points(d3,col="green")
attach(d)
#Confusion1:how to specify the formula in the npudensbw() correctly? I find
the sequence of ordered(tf)+x+y is important and here i may have a wrong
specification
bw <- npudensbw(formula=~ordered(tf)+x+y, bwmethod="cv.ml")
#confusion1
year.seq <- sort(unique(d$tf)) #length is 3
x.seq <- seq(0,1,0.02) #length is 51
y.seq <- seq(0,1,0.02) #length is 51
#Confusion2:what is the correct sequence for the three variables
(year.seq,x.seq and y.seq) in expand.grid()
data.eval <- expand.grid(tf=year.seq,x=x.seq,y=y.seq) #confusion2
fhat <- fitted(npudens(bws=bw, newdata=data.eval))
#Confusion3:what is the correct sequence for the three variables in the c()
options of array()
f <- array(fhat, c(51,51,3)) #number of year.seq is 3, and number of x.seq
and y.seq are 51,confusion3
brks <- quantile(f, seq(0,1,0.05));cols <-
heat.colors(length(brks)-1);oldpar <- par(mfrow=c(1,3))
#Confusion4:what is the correct sequence for the three variables(tf,x and y)
in the image()
for (i in 1:3) image(x.seq, y.seq, f[,,i],asp=1, xlab="",
ylab="", main=i,
breaks=brks, col=cols) #confusion4
par(oldpar)
#This was also confused in 4 ,5 and more dimensions.
Any help or suggestions are greatly appreciated.
--
-----------------
Jane Chang
Queen's
[[alternative HTML version deleted]]
Maybe Matching Threads
- npudens(np) Error missing value where TRUE/FALSE needed
- xlim and ylim on npudens (np package)
- R package 'np' problems
- Versions 0.50-1 of the np and npRmpi packages released - new features and deprecated options…
- Versions 0.50-1 of the np and npRmpi packages released - new features and deprecated options…
