矩阵乘法误差和ggplot输出

时间:2019-04-10 07:15:23

标签: r matrix multiplication

im试图进行矩阵乘法以执行主成分分析,并使用ggplot显示结果

pca_model <- tbl(sc, "naflights") %>%
select(air_time, distance, dep_time) %>%
ml_pca()
print(pca_model)

# Explained variance:
#
#         PC1          PC2          PC3 
# 0.6975982637 0.3021978609 0.0002038754 
# 
# Rotation:
#                  PC1         PC2           PC3
# air_time -0.12514862 0.001940123 -0.9921361086
# distance -0.99200200 0.016312867  0.1251636066
# dep_time  0.01642742 0.999865054 -0.0001169274

D <- as.matrix(naflights[1:3])
E <- as.matrix(pca_model$components)

# Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
#  'data' must be of a vector type, was 'NULL'

我尝试过:

D <- as.matrix(flights[4, 15, 16])
E <- as.matrix(pca_model$components)
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL'
loadings_pca <- tidy(pca_model)
loadings_pca
# A tibble: 3 x 4
  features     PC1     PC2       PC3
  <chr>      <dbl>   <dbl>     <dbl>
1 air_time -0.125  0.00194 -0.992   
2 distance -0.992  0.0163   0.125   
3 dep_time  0.0164 1.000   -0.000117
E <- as.matrix(loadings_pca[2:4])
P <- D %*% E
Error in D %*% E : non-conformable arguments
mode(D)
[1] "numeric"
 mode(E)
[1] "numeric"

我尝试了这种方法,但是仍然出现错误:参数不一致。 有人可以帮我解决这个问题吗?

0 个答案:

没有答案