【问题标题】:What does "Error: Must use a vector in `[`, not an object of class matrix." mean when running a PCA?“错误:必须在`[`中使用向量,而不是类矩阵的对象”是什么意思。在运行 PCA 时是什么意思?
【发布时间】:2020-02-20 11:18:38
【问题描述】:

我对 R 很陌生,我正在尝试使用代码为不完整的数据集运行 PCA:

res.comp <- imputePCA(questionaire_results_PCA, ncp = nb$ncp)

但是 R 告诉我:

错误:必须使用[ 中的向量,而不是类矩阵的对象。 运行rlang::last_error() 以查看发生错误的位置。

所以我跑了:

rlang::last_error()

R 说:

 1. missMDA::imputePCA(questionaire_results_PCA, ncp = nb$ncp)
 4. tibble:::`[.tbl_df`(X, !is.na(X))
 5. tibble:::check_names_df(i, x)
Run `rlang::last_trace()` to see the full context

所以我跑了:

rlang::last_trace()

R 说:

Must use a vector in `[`, not an object of class matrix.
Backtrace:
    █
 1. └─missMDA::imputePCA(questionaire_results_PCA, ncp = nb$ncp)
 2.   ├─base::mean((res.impute$fittedX[!is.na(X)] - X[!is.na(X)])^2)
 3.   ├─X[!is.na(X)]
 4.   └─tibble:::`[.tbl_df`(X, !is.na(X))
 5.     └─tibble:::check_names_df(i, x)

有谁知道这意味着什么以及如何让它发挥作用?

我跑过: dput(head(questionaire_results_PCA))

我得到了:

structure(list(Active = c(6, 6, 5, 7, 5, 6), `Aggressive to people` = c(NA, 
4, NA, 2, NA, 1), Anxious = c(NA, 4, NA, 3, NA, 2), Calm = c(NA, 
5, NA, 5, NA, 6), Cooperative = c(7, 6, 7, 6, 6, 6), Curious = c(7, 
2, 7, 7, 7, 6), Depressed = c(1, 3, 1, 1, 1, 1), Eccentric = c(1, 
3, 1, 4, 1, 4), Excitable = c(5, 2, 5, 5, 4, 4), `Fearful of people` = c(1, 
2, 1, 2, 1, 1), `friendly of people` = c(5, 6, 7, 7, 7, 7), Insecure = c(2, 
5, 2, 3, 2, 2), Playful = c(4, 6, 2, 5, 6, 6), `Self assured` = c(7, 
6, 7, 5, 6, 6), Smart = c(6, 2, 7, 5, 7, 3), Solitary = c(4, 
4, 3, 4, 3, 2), Tense = c(1, 2, 1, 3, 1, 2), Timid = c(2, 2, 
2, 2, 2, 2), Trusting = c(6, 6, 6, 6, 6, 6), Vigilant = c(7, 
6, 5, 3, 5, 3), Vocal = c(2, 7, 1, 6, 1, 7)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

然后我运行代码: dput(nb$ncp)

并得到: 3L

【问题讨论】:

  • 嗨!你可以做 dput(head(questionaire_results_PCA)) 和 dput(nb$ncp),将输出粘贴为你的问题的一部分吗?否则我们将不知道是什么导致了错误
  • 嗨,我已经完成了你的要求并添加到我上面的问题中!我仍然不知道那是什么意思。非常感谢您到目前为止的帮助!
  • 嘿,有几件事要尝试。首先,将您的对象转换为矩阵。 res.comp
  • 你保留 = which(apply(as.matrix(x),2,sd)!=0) ; imputePCA(as.matrix(questionaire_results_PCA)[,keep], npc=2)
  • 第一个成功了,非常感谢!!

标签: r


【解决方案1】:

如果有人遇到同样的问题,这里是答案。使用OP提供的数据:

 class(questionaire_results_PCA)
[1] "tbl_df"     "tbl"        "data.frame"

imputePCA 的输入需要 data.frame,但它不适用于 tribble。所以我们需要将其转换回矩阵或data.frame:

library(missMDA)
res.comp <- imputePCA(data.frame(questionaire_results_PCA), ncp = 2)

Error in eigen(crossprod(t(X), t(X)), symmetric = TRUE) : 
  infinite or missing values in 'x'

我收到此错误是因为它是数据的子集,并且某些列没有偏差,我们首先解决此问题。

sel = which(apply(questionaire_results_PCA,2,sd)!=0)

# returns you a data.frame
res1 <- imputePCA(as.data.frame(questionaire_results_PCA[,sel]), ncp = 2)
# returns you a matrix
res2 <- imputePCA(as.matrix(questionaire_results_PCA[,sel]), ncp = 2)

【讨论】:

  • 不客气@Natalie,我只是把答案写下来以防有人遇到同样的问题:)
猜你喜欢
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2017-06-11
  • 2018-03-05
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多