我有一个数据集,其中一个变量中缺少值,我试图对其执行PCA,然而,我总是得到相同的错误消息。
# packages
library(tidyverse)
library(missMDA)
# dataset
df <- structure(list(
A = c(39.64, 39.23, 38.89, 38.63, 38.44, 38.32, 38.27, 38.3, 38.4, 38.56, 38.79, 39.06, 39.36,
39.68, 40.01, 40.34, 40.68, 41.05, 41.46, 41.93, 42.48, 43.14, 43.92, 44.84, 45.9, 47.1, 48.4,
49.78, 51.2, 52.62, 54.01, 55.31, 56.52, 57.6, 58.54, 59.33, 59.98, 60.46, 60.78, 60.94, 60.92,
60.71, 60.3, 59.69, 58.87, 57.86, 56.67, 55.33, 53.87, 52.33, 50.75),
B = c(3.29, 3.29, 3.3, 3.32, 3.33, 3.35, 3.36, 3.38, 3.4, 3.42, 3.44, 3.46, 3.48, 3.5, 3.52, 3.53,
3.54, 3.55, 3.55, 3.54, 3.53, 3.51, 3.48, 3.44, 3.4, 3.35, 3.3, 3.24, 3.18, 3.12, 3.07, 3.02, 2.98,
2.96, 2.94, 2.94, 2.95, 2.98, 3.02, 3.08, 3.14, 3.22, 3.3, 3.4, 3.49, 3.59, 3.7, 3.8, 3.91, 4.02,
4.12),
C = c(NA, NA, NA, NA, NA, NA, NA, NA, 12.23, 4.3, 3.44, 3.49, 5.59, 0.76, 4.2, 4.61, 4.6, 8.03,
4.26, 3.31, 4.16, 0.37, -2.17, -1.93, -3.7, -0.48, -4.63, -2.89, -4.29, -2.35, -2.27, -1.66, -2.82,
-3.2, -2.55, -0.9, -2.42, -0.84, -1.46, -5.39, -2.57, -2.12, -0.59, 1.75, 3.7, 0.99, -3.28, -3.2,
-0.6, -0.61, -1.94)), class = "data.frame", row.names = c(NA, -51L))
# estimate number of dimensions
pcaDim <- estim_ncpPCA(X = df, method.cv = "Kfold")
# PCA
pca <- imputePCA(X = df, ncp = pcaDim)
Error in imputePCA(X = df, ncp = pcaDim) :
'list' object cannot be coerced to type 'double'
# I've tried to fix this error with the unlist function, but that has not helped.
df <- mutate(df, across(everything(), ~as.numeric(unlist(.))))
1条答案
按热度按时间hgc7kmma1#
imputePCA()
中的“ncp”选项需要一个“与用于预测丢失条目的组件数量相对应的整数”;我猜你应该把pcaDim$ncp
传递给这个参数,即。创建于2023-06-06带有reprex v2.0.2