<Urgent> the partial dependence plot show nothing

Hi i am using the code below to find some partial dependence plots
However, all of these output shows empty plot like this


May I ask if someone can help me on that please?

link for the predictors in the training set: 39.7 KB file on MEGA
link for the target variable in the training set: 9.7 KB file on MEGA

library(ANN2)
set.seed(1)
NN28 <- neuralnetwork(
X=x_train, y=y_train,
hidden.layers=c(11,15),regression=TRUE,
standardize=TRUE,loss.type="squared",
activ.functions="relu",n.epochs=200,
learn.rates=1e-04,val.prop=0.1/0.9,
random.seed=1
)
library(pdp)
NN <- NN28
partial(NN, train = x_train, pred.var = "TMAX",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "TMIN",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "TOBS",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "PRCP",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "SNOW",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "moon_radian",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "Full",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "New",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "Waning",type="regression", plot = TRUE, rug = TRUE)
partial(NN, train = x_train, pred.var = "Waxing",type="regression", plot = TRUE, rug = TRUE)

I think the problem is that the predict method for ANN objects returns a list rather than a vector. If you write the prediction function yourself and have it take the mean of the first element of the list (which is the vector of predictions), that should work. You should verify to make sure this gives the intended answer, though.

x_train <- rio::import("~/Downloads/x_train.xlsx")
y_train <- rio::import("~/Downloads/y_train.xlsx")

library(ANN2)
set.seed(1)
NN28 <- neuralnetwork(
  X=x_train, y=y_train$Property,
  hidden.layers=c(11,15),regression=TRUE,
  standardize=TRUE,loss.type="squared",
  activ.functions="relu",n.epochs=200,
  learn.rates=1e-04,val.prop=0.1/0.9,
  random.seed=1
)
#> Artificial Neural Network: 
#>   Layer - 10 nodes - input 
#>   Layer - 11 nodes - relu 
#>   Layer - 15 nodes - relu 
#>   Layer - 1 nodes - linear 

library(pdp)
NN <- NN28


partial(NN, train = x_train, pred.var = "TMAX",type="regression", plot = TRUE, rug = TRUE, 
        pred.fun = function(object, newdata){mean(ANN2:::predict.ANN(object, newdata=newdata)[[1]])})

Created on 2024-02-13 with reprex v2.0.2

1 Like

one more further question on that
what if i want to calculate the out of sample R squared with the below test set?
link for the predictors in the test set: 22.5 KB file on MEGA
link for the target variable in the test set: 7.3 KB file on MEGA

You can just predict and then get the squared correlation between the observed and fitted values:

x_test <- rio::import("~/Downloads/x_test.xlsx")
y_test <- rio::import("~/Downloads/y_test.xlsx")
yhat <- predict(NN, newdata=x_test)

## out of sample R-squared
cor(yhat[[1]], y_test[[1]])^2
#>          [,1]
#> y_1 0.1151513