Plot Causes "Error: Incorrect Number of Dimensions"

Plot causes Error: Incorrect Number of Dimensions

getCodes() produces a list and as such you have to treat it like one.

Calling getCodes(som) produces a list containing 7 items named a-g as such you should be selecting items from the list either using $ or [[]]

e.g

plot(som, type = "property", property = getCodes(som)[[1]], main=names(getCodes(som))[1], palette.name=terrain.colors)

or

plot(som, type = "property", property = getCodes(som)$a, main="a", palette.name=terrain.colors)

or

plot(som, type = "property", property = getCodes(som)[["a"]], main="a", palette.name=terrain.colors)

if you must set the variable prior to calling the plot you can do so like:

var <- 1    
plot(som, type = "property", property = getCodes(som)[[var]], main=names(getCodes(som))[var], palette.name=terrain.colors)

Regarding kmeans()

kmeans() needs a matrix or an object that can be coerced into a matrix, you have factors (categorical data) which cannot be coerced into numeric, either drop the factors, or find another method.

drop the factors:

#cluster (error)  
set.seed(33)
#for reproducability

fit_kmeans <- kmeans(as.matrix(data[1:4]), 3)
#3 clusters are used, as indicated by the wss development.

cl_assignmentk <- fit_kmeans$cluster[data$unit.classif]
par(mfrow=c(1,1))
plot(som, type="mapping", bg = rgb(colour4), shape = "straight", border = "grey",col=contrast)
add.cluster.boundaries(som, fit_kmeans$cluster, lwd = 3, lty = 2, col=contrast[4])

edit:
Alternatively you can specify the code directly from getCodes() by using idx like so:

plot(som, type = "property", property = getCodes(som, idx = 1), main="a"), palette.name=terrain.colors)

gmm Error: Incorrect number of dimensions

I found my flaw, wich is due to the definition of the function g.
As the reference manual for gmm in R states, g can be a formula (if you use linear models) or an explicit function with two parameters. The declaration of g as a function must be the estimated vector of parameter theta as the first argument and the matrix of data x as the second argument. In fact,

g <- function(data,theta){
...
}

has to be replaced by

g <- function(theta,data){
...
}

Error: Incorrect number of dimensions in R

Elle, try this if you want to exclude the records with NA.

library(psych) 
data <- sat.act[complete.cases(sat.act), ]
prcomp(data)

Standard deviations:
[1] 146.8300134 68.2543504 9.5430803 3.6666452 1.1715551 0.4647055

Rotation:
PC1 PC2 PC3 PC4 PC5
gender 0.0003401435 -0.0012020541 0.0010970565 0.005788342 -0.0591625722
education -0.0004299901 -0.0002918314 -0.0850528466 0.024064095 -0.9943380906
age 0.0027020833 0.0014188342 -0.9929359565 -0.084983862 0.0824906340
ACT -0.0208448308 0.0016963799 -0.0827031922 0.995852246 0.0314096125
SATV -0.6956211490 -0.7182796666 -0.0017374908 -0.013494765 0.0003648618
SATQ -0.7181010432 0.6957498829 0.0003989952 -0.016166443 -0.0003874180
PC6
gender -0.9982301944
education 0.0589781669
age -0.0064738244
ACT 0.0038129483
SATV 0.0005261265
SATQ -0.0011528452

Or if you want to force NA to 0

data <- sat.act
data[is.na(data)] <- 0
prcomp(data)

Standard deviations:
[1] 159.4488983 85.1587086 9.5463091 3.7961644 1.1814762 0.4653497

Rotation:
PC1 PC2 PC3 PC4 PC5
gender 0.0003915730 -7.364935e-04 0.0008193646 0.002717142 -0.0591610356
education -0.0004932616 -9.314099e-05 -0.0837084272 0.019199014 -0.9945610838
age 0.0012746540 4.606768e-03 -0.9933615141 -0.080624581 0.0817016560
ACT -0.0172578373 -1.064616e-02 -0.0788111515 0.996345023 0.0259420620
SATV -0.5500283967 -8.349310e-01 -0.0030404778 -0.018696325 0.0002655931
SATQ -0.8349664116 5.502319e-01 0.0021652104 -0.008410428 -0.0000266278
PC6
gender -0.9982440693
education 0.0589261882
age -0.0058797665
ACT 0.0011109106
SATV 0.0003311219
SATQ -0.0007530176

Shiny error in incorrect number of dimensions (Error in [: incorrect number of dimensions)

Finally I have figured out the solution. It occured that the API returned every few minutes an empty response. Printing the content of the reactive variables wasn't a solution, because the print was after the error occured.

R error: Incorrect number of dimensions

This is the output of the top of the str() call on the object you are sending to plot():

> xsub <- subset(x, delta <2)
> str(xsub)
Classes ‘model.selection’ and 'data.frame': 1 obs. of 11 variables:
$ (Intercept) : num 17.6
$ COLOUR_RF : Factor w/ 1 level "+": NA
$ FAMILY : Factor w/ 1 level "+": NA
$ FRUIT_TYPE : Factor w/ 1 level "+": 1
$ COLOUR_RF:FAMILY : Factor w/ 1 level "+": NA
$ COLOUR_RF:FRUIT_TYPE: Factor w/ 1 level "+": NA
$ df : int 3
$ logLik : num -44.3
$ AICc : num 94.8
$ delta : num 0
$ weight : num 1

I suspect you have simply made a restrictive call that doesn't provide as much information as needed for the plotting function to do any useful work. Using subset(x, delta <5) succeeds.

Error in sil.obj[, 1:3] : incorrect number of dimensions for silhouette method?

The problem is that the default value for k.max in fviz_nbclust is 10 which is one more group than you have data (9 rows). Also the first variable is character not numeric. Try this

fviz_nbclust(dt_scaled, FUN = hcut, method = "silhouette", k.max=5)

or any value of k.max less than the number of observations in your data.



Related Topics



Leave a reply



Submit