1
votes

I am using the R programming language. I am trying to follow this tutorial over here: http://www.semspirit.com/artificial-intelligence/machine-learning/regression/support-vector-regression/support-vector-regression-in-r/

For the famous Iris dataset, I am trying to plot the 3D decision surface for the random forest algorithm (using tsne dimensions):

  library(Rtsne)
    library(dplyr)
    library(ggplot2)
    library(plotly)
    library(caret)
    library(randomForest)
    
  #data
a = iris
a <- unique(a)

#create two species just to make things easier
s <- c("a","b")
species<- sample(s , 149, replace=TRUE, prob=c(0.3, 0.7))
a$species = species
a$species = as.factor(a$species)

#split data into train/test, and then random forest 

index = createDataPartition(a$species, p=0.7, list = FALSE)
train = a[index,]
test = a[-index,]

rf = randomForest(species ~ ., data=train, ntree=50, mtry=2)

#have the model predict the test set
pred = predict(rf, test, type = "prob")
labels = as.factor(ifelse(pred[,2]>0.5, "a", "b"))
confusionMatrix(labels, test$species)


#tsne algorithm
tsne_obj_3 <- Rtsne(test[,-5], perplexity=1, dims=3)
df_m2 <- as.data.frame(tsne_obj_3$Y)

df_m2$labels = test$species

From here, I am trying to plot the 3d decision surface (http://www.semspirit.com/artificial-intelligence/machine-learning/regression/support-vector-regression/support-vector-regression-in-r/) :

axis_1 = df_m2$V1
axis_2 = df_m2$V2
axis_3 = df_m2$V3

plot_ly(x=as.vector(axis_1),y=as.vector(axis_2),z=axis_3, type="scatter3d", mode="markers", name = "Obs", marker = list(size = 3)) %>%
add_trace(x=as.vector(axis_1),y=as.vector(axis_2),z=df_m2$labels, type = "mesh3d", name = "Preds")

But I am getting the following error:

2: In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels

3: 'mesh3d' objects don't have these attributes: 'mode', 'marker'
Valid attributes include:
'type', 'visible', 'legendgroup', 'name', 'uid', 'ids', 'customdata', 'meta', 'hoverlabel', 'stream', 'uirevision', 'x', 'y', 'z', 'i', 'j', 'k', 'text', 'hovertext', 'hovertemplate', 'delaunayaxis', 'alphahull', 'intensity', 'intensitymode', 'color', 'vertexcolor', 'facecolor', 'cauto', 'cmin', 'cmax', 'cmid', 'colorscale', 'autocolorscale', 'reversescale', 'showscale', 'colorbar', 'coloraxis', 'opacity', 'flatshading', 'contour', 'lightposition', 'lighting', 'hoverinfo', 'showlegend', 'xcalendar', 'ycalendar', 'zcalendar', 'scene', 'idssrc', 'customdatasrc', 'metasrc', 'xsrc', 'ysrc', 'zsrc', 'isrc', 'jsrc', 'ksrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'intensitysrc', 'vertexcolorsrc', 'facecolorsrc', 'hoverinfosrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
 

A 3D plot is produced, but the 3D plane is completely gone.

Can someone please tell me what I am doing wrong?

I am trying to make so that when you move your mouse over each point, for that point it will display the value of a$Sepal.Length, a$Sepal.Width, a$Petal.Length, a$Petal.Width, a$Species

Thanks

1

1 Answers

2
votes

When you called add_trace(), z is not assigned correctly. The labels won't plot; you need to plot the probabilities you identified, z=df_m2$pred.

There are multiple ways to fix the issues with the mesh plot, but the easiest would be to use add_mesh instead of add_trace.

plot_ly(x=as.vector(axis_1),
        y=as.vector(axis_2), 
        z=axis_3, 
        type="scatter3d",
        mode="markers", 
        name = "Obs", 
        marker = list(size = 3)) %>% 
   add_mesh(x=as.vector(axis_1), 
            y=as.vector(axis_2), 
            z=df_m2$pred, 
            type = "mesh3d", 
            name = "Preds")