JeffreyRacine / R-Package-np

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

How to plot single conditional distribution when using npcdist #39

Closed bking124 closed 1 year ago

bking124 commented 1 year ago

I am using npcdistbw and npcdist to estimate a conditional distribution. The output of the npcdist function is stored as Fhat. When I go to run plot(Fhat), it shows estimated distributions for each predictors and for y. How can I plot only the estimated distribution for y?

Sorry if this is a naive question--I tried to look at the plotting function code, but it was quite complex and I wasn't able to parse much.

JeffreyRacine commented 1 year ago

Greetings! You can do this manually. First create an evaluation data with the sequence/values of the Y variable you want to plot and values for your X variables that are constants (say, median values of each - see the FAQ for examples of creating evaluation data files). Then use predict(Fhat,newdata=foo) where foo is the name of your evaluation data which will spit out the fitted values for each value of Y corresponding to the sequence you used. Then simply plot your Fhat predictions versus the sequence of Y values. The following is a toy illustration.

library(np)
# generate data
n <- 100
x <- rnorm(n)
y <- rnorm(n)
Fhat <- npcdist(y~x)
foo <- data.frame(y=seq(min(y),max(y),length=10),x=median(x))
y.pred <- predict(Fhat,newdata=foo)
plot(foo$y,y.pred,type="l")