R中的矩阵乘法:需要数值/复数矩阵/向量参数。

6

我正在使用mlbench包中的BreastCancer数据集,并试图执行以下矩阵乘法作为逻辑回归的一部分。

我将特征放在前10列中,并创建了一个名为theta的参数向量:

X <- BreastCancer[, 1:10]
theta <- data.frame(rep(1, 10))

然后我进行了以下矩阵乘法:

constant <- as.matrix(X) %*% as.vector(theta[, 1])

但是,我遇到了以下错误:

Error in as.matrix(X) %*% as.vector(theta[, 1]) : 
  requires numeric/complex matrix/vector arguments

我需要使用as.numeric(X)将矩阵强制转换为双精度吗?因为X中的值看起来像字符串,因为它们有双引号。

1个回答

13

矩阵乘法运算符/函数,如"%*%"crossprodtcrossprod,期望传入的矩阵是“numeric”、“complex”或“logical”模式。但是,您的矩阵是“character”模式。

library(mlbench)
data(BreastCancer)
X <- as.matrix(BreastCancer[, 1:10])
mode(X)
#[1] "character"

你可能会感到惊讶,因为数据集似乎包含数字数据:

head(BreastCancer[, 1:10])
#       Id Cl.thickness Cell.size Cell.shape Marg.adhesion Epith.c.size
#1 1000025            5         1          1             1            2
#2 1002945            5         4          4             5            7
#3 1015425            3         1          1             1            2
#4 1016277            6         8          8             1            3
#5 1017023            4         1          1             3            2
#6 1017122            8        10         10             8            7
#  Bare.nuclei Bl.cromatin Normal.nucleoli Mitoses
#1           1           3               1       1
#2          10           3               2       1
#3           2           3               1       1
#4           4           3               7       1
#5           1           3               1       1
#6          10           9               7       1

但是你被打印风格所误导了。这些列实际上是字符或因子

lapply(BreastCancer[, 1:10], class)
#$Id
#[1] "character"
#
#$Cl.thickness
#[1] "ordered" "factor" 
#
#$Cell.size
#[1] "ordered" "factor" 
#
#$Cell.shape
#[1] "ordered" "factor" 
#
#$Marg.adhesion
#[1] "ordered" "factor" 
#
#$Epith.c.size
#[1] "ordered" "factor" 
#
#$Bare.nuclei
#[1] "factor"
#
#$Bl.cromatin
#[1] "factor"
#
#$Normal.nucleoli
#[1] "factor"
#
#$Mitoses
#[1] "factor"
当您使用as.matrix时,这些列都被强制转换为“character”类型(详见R: Why am I not getting type or class "factor" after converting columns to factor?)。因此,在进行矩阵乘法时,我们需要将这些列正确地强制转换为“numeric”类型。
dat <- BreastCancer[, 1:10]

## character to numeric
dat[[1]] <- as.numeric(dat[[1]])

## factor to numeric
dat[2:10] <- lapply( dat[2:10], function (x) as.numeric(levels(x))[x] )

## get the matrix
X <- data.matrix(dat)
mode(X)
#[1] "numeric"

现在你可以进行矩阵与向量的乘法计算。

## some possible matrix-vector multiplications
beta <- runif(10)
yhat <- X %*% beta

## add prediction back to data frame
dat$prediction <- yhat

不过,我怀疑这不是在构建具有因子的模型时获取预测值的正确方法。因为模型矩阵不是上述的X,而是虚拟矩阵。我强烈建议您使用predict函数。


这行代码也对我起效了:as.matrix(sapply(dat, as.numeric))

看来你很幸运,数据集中因子水平与数值相同。一般来说,将因子转换为数字应该使用我所使用的方法。比较一下:

f <- gl(4, 2, labels = c(12.3, 0.5, 2.9, -11.1))
#[1] 12.3  12.3  0.5   0.5   2.9   2.9   -11.1 -11.1
#Levels: 12.3 0.5 2.9 -11.1

as.numeric(f)
#[1] 1 1 2 2 3 3 4 4

as.numeric(levels(f))[f]
#[1] 12.3  12.3  0.5   0.5   2.9   2.9   -11.1 -11.1

这个内容在文档页面?factor中介绍。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接