【发布时间】: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