本文介绍了R错误:"check.data中的错误:参数应为数字"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关"kohonen"的信息,R编程语言的库.我创建了一些人工数据来尝试一些功能.我尝试使用"supersom()"仅对连续(即type = as.numeric)数据起作用,并且一切正常.但是,当我尝试运行"supersom()"时,在连续和绝对(类型= as.factor)函数上,我开始遇到一些错误(参数数据应为数字").

I am learning about the "kohonen" library for the R programming language. I created some artificial data to try some of the functions on. I tried using the "supersom()" function on only continuous (i.e type = as.numeric) data and everything works well. However, when I tried to run the "supersom()" function on both continuous and categorical (type = as.factor), I start to run into some errors ("Argument data should be numeric").

"supersom()"函数具有称为"dist.fct"的自变量.(距离功能)(允许用户指定哪种类型的距离").(例如,对于连续的"Euclidean",对于分类的"tanimoto")应该用于不同的列.我创建了一个包含4个连续变量和3个类别变量的数据集.使用以下链接: https://www.rdocumentation.org/packages/kohonen/versions/2.0.5/topics/supersom ,我尝试运行示例:

The "supersom()" function has an argument called "dist.fct" (distance function) which allows the user to specify which type of "distance" (e.g. "Euclidean" for continuous, "tanimoto" for categorical) should be used for different columns. I created a data set with 4 continuous variables and 3 categorical variables. Using the following link : https://www.rdocumentation.org/packages/kohonen/versions/2.0.5/topics/supersom , I tried to run the example:

  #load libraries
    library(kohonen)
    library(dplyr)

#create and format data

a =rnorm(1000,10,10)
b = rnorm(1000,10,5)
c = rnorm(1000,5,5)
d = rnorm(1000,5,10)
e <- sample( LETTERS[1:4], 100 , replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
f <- sample( LETTERS[1:5], 100 , replace=TRUE, prob=c(0.2, 0.2, 0.2, 0.2, 0.2) )
g <- sample( LETTERS[1:2], 100 , replace=TRUE, prob=c(0.5, 0.5) )

data = data.frame(a,b,c,d,e,f,g)
data$e = as.factor(data$e)
data$f = as.factor(data$f)
data$g = as.factor(data$g)

cols <- 1:4
data[cols] <- scale(data[cols])
data = as.matrix(data)

#som function
som <- supersom(data= data, grid =somgird(10,10, "hexagonal"),
dist.fct = c("euclidean","euclidean","euclidean","euclidean","tanimoto", "tanimoto", "tanimoto", "tanimoto), keep.data = TRUE)


#sources:
https://cran.r-project.org/web/packages/kohonen/kohonen.pdf
https://www.rdocumentation.org/packages/kohonen/versions/2.0.5/topics/supersom

但是,这会产生错误"check.data(data)中的错误:参数数据应为数字" .根据文档(参见我所附的资源),"dist.fct"存在默认值.参数-因此,我也尝试将其保留为空白,希望可以自动选择默认值:

However, this produces an error "Error in check.data(data): Argument data should be numeric". According to the documentation (see the sources I attached), there are default values for the "dist.fct" argument - therefore, I also tried leaving it blank, hoping that the default values would be automatically selected:

som <- supersom(data= data, grid =somgird(10,10, "hexagonal"), keep.data = TRUE)

但这也产生了类似的错误.

But this also produced a similar error.

有人知道我在做什么错吗?

Does anyone know what I am doing wrong?

谢谢

推荐答案

如果将因子或字符数据保留在矩阵中,则由于矩阵只能具有一种类型的数据,因此会将矩阵的所有其他值转换为字符.在矩阵中仅保留数字数据,或将每一列转换为列表.

If you keep factor or character data in a matrix it will turn all other values of matrix to character since a matrix can have data of only one type. Keep only numeric data in the matrix or convert each column to a list.

library(kohonen)

cols <- 1:4
data[cols] <- scale(data[cols])
som <- supersom(data= as.list(data), grid = somgrid(10,10, "hexagonal"),
                dist.fct = "euclidean", keep.data = TRUE)

这篇关于R错误:"check.data中的错误:参数应为数字"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:45