JeffreyRacine / R-Package-np

R package np (Nonparametric Kernel Smoothing Methods for Mixed Data Types)
https://socialsciences.mcmaster.ca/people/racinej
47 stars 18 forks source link

How to use npudens correctly ? #36

Closed wiz21b closed 2 years ago

wiz21b commented 2 years ago

Hello,

I'm trying to compute densities with the npudens function of the np package. Here's the code:

data("Italy")
attach(Italy)
nrow(Italy)
year.seq <- sort(unique(year))
gdp.seq <- seq(1,36,length=500)
data.eval <- expand.grid(year=year.seq,gdp=gdp.seq)

bw <- npudensbw(formula=~year+gdp)
fhat <- fitted(npudens(bws=bw, newdata=data.eval))
length(fhat) == nrow(data.eval) # TRUE

bw <- npudensbw(Italy)
fhat <- fitted(npudens(bws=bw, newdata=data.eval))
length(fhat) == nrow(data.eval) # FALSE ???

Why are the two lengths different ? I expect that both calls to npudensbw are equivalent. I've checked the documentation, the vignettes and even the source code... Note that the code seems fine if the number of rows in data.eval is smaller than in Italy...

JeffreyRacine commented 2 years ago

Hi,   Try   bw <- npudensbw(formula=~year+gdp) fhat <- npudens(bws=bw) fhat.pred <- predict(fhat,newdata=data.eval)   I have not run this but first fit the model then use predict(…,newdata=) which ought to work…   Jeff  

From: wiz21b @.> Reply-To: JeffreyRacine/R-Package-np @.> Date: Wednesday, June 22, 2022 at 15:43 To: JeffreyRacine/R-Package-np @.> Cc: Subscribed @.> Subject: [JeffreyRacine/R-Package-np] How to use npudens correctly ? (Issue #36)

 

Hello, I'm trying to compute densities with the npudens function of the np package. Here's the code: data("Italy") attach(Italy) nrow(Italy) year.seq <- sort(unique(year)) gdp.seq <- seq(1,36,length=500) data.eval <- expand.grid(year=year.seq,gdp=gdp.seq)   bw <- npudensbw(formula=~year+gdp) fhat <- fitted(npudens(bws=bw, newdata=data.eval)) length(fhat) == nrow(data.eval) # TRUE   bw <- npudensbw(Italy) fhat <- fitted(npudens(bws=bw, newdata=data.eval)) length(fhat) == nrow(data.eval) # FALSE ??? Why are the two lengths different ? I expect that both calls to npudensbw are equivalent. I've checked the documentation, the vignettes and even the source code... Note that the code seems fine if the number of rows in data.eval is smaller than in Italy... — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>