对重复处理而不是参数执行pca

时间:2018-09-26 22:28:38

标签: r statistics pca

我有一个数据集,其形式为第1列包含治疗名称,其余列包含这些治疗的值,每种治疗有3个重复。为了说明,我使用虹膜数据集创建了模拟数据集,如下所示:

df <- read.table(text = '"Treatment" "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
"treatment_a" 5.1 3.5 1.4 0.2
"treatment_a" 4.9 3 1.4 0.2
"treatment_a" 4.7 3.2 1.3 0.2
"treatment_b" 4.6 3.1 1.5 0.2
"treatment_b" 5 3.6 1.4 0.2
"treatment_b" 5.4 3.9 1.7 0.4
"treatment_c" 4.6 3.4 1.4 0.3
"treatment_c" 5 3.4 1.5 0.2
"treatment_c" 4.4 2.9 1.4 0.2
"treatment_d" 4.9 3.1 1.5 0.1
"treatment_d" 5.4 3.7 1.5 0.2
"treatment_d" 4.8 3.4 1.6 0.2
"treatment_e" 4.8 3 1.4 0.1
"treatment_e" 4.3 3 1.1 0.1
"treatment_e" 5.8 4 1.2 0.2
"treatment_f" 5.7 4.4 1.5 0.4
"treatment_f" 5.4 3.9 1.3 0.4
"treatment_f" 5.1 3.5 1.4 0.3
"treatment_g" 5.7 3.8 1.7 0.3
"treatment_g" 5.1 3.8 1.5 0.3
"treatment_g" 5.4 3.4 1.7 0.2
"treatment_h" 5.1 3.7 1.5 0.4
"treatment_h" 4.6 3.6 1 0.2
"treatment_h" 5.1 3.3 1.7 0.5', header = TRUE)

我想使用R在此数据集上执行pca,以便在绘图上绘制重复处理而不是变量,处理名称也应在绘图上标出。 我在stackoverflow上寻找了类似的问题,但是没有找到与我的问题类似的问题。

1 个答案:

答案 0 :(得分:1)

原始回应

您是否要绘制散点图,其第一和第二主成分分别绘制在x和y轴上?然后,您要对这些点标上治疗方法吗?如果是这样,您可以尝试一下。我正在使用ggplot2软件包。

我还给锅增添了色彩美感。如果不想,可以随意放下该部分。

df <- read.table(text = '"Treatment" "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
"treatment_a" 5.1 3.5 1.4 0.2
"treatment_a" 4.9 3 1.4 0.2
"treatment_a" 4.7 3.2 1.3 0.2
"treatment_b" 4.6 3.1 1.5 0.2
"treatment_b" 5 3.6 1.4 0.2
"treatment_b" 5.4 3.9 1.7 0.4
"treatment_c" 4.6 3.4 1.4 0.3
"treatment_c" 5 3.4 1.5 0.2
"treatment_c" 4.4 2.9 1.4 0.2
"treatment_d" 4.9 3.1 1.5 0.1
"treatment_d" 5.4 3.7 1.5 0.2
"treatment_d" 4.8 3.4 1.6 0.2
"treatment_e" 4.8 3 1.4 0.1
"treatment_e" 4.3 3 1.1 0.1
"treatment_e" 5.8 4 1.2 0.2
"treatment_f" 5.7 4.4 1.5 0.4
"treatment_f" 5.4 3.9 1.3 0.4
"treatment_f" 5.1 3.5 1.4 0.3
"treatment_g" 5.7 3.8 1.7 0.3
"treatment_g" 5.1 3.8 1.5 0.3
"treatment_g" 5.4 3.4 1.7 0.2
"treatment_h" 5.1 3.7 1.5 0.4
"treatment_h" 4.6 3.6 1 0.2
"treatment_h" 5.1 3.3 1.7 0.5', header = TRUE)

# run principle components, ignore first column
pr <- prcomp(df[, 2:5])

# run predict to get the first and second principle components
pr_pred <- predict(pr)

# put this into a data frame so we can use ggplot
df2 <- data.frame(Treatment = df$Treatment,
                  pr_pred[, 1:2])

library(ggplot2)

ggplot(data = df2, aes(x = PC1, y = PC2, 
                       colour = Treatment, 
                       label = Treatment)) + 
    geom_text()

enter image description here

添加了椭圆

要添加这些类别,我们必须更改类别的数量。我们去三个。希望在您的实际数据集中可以绘制出所需的椭圆。

df_mod <- read.table(text = '"Treatment" "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
"treatment_a" 5.1 3.5 1.4 0.2
                 "treatment_a" 4.9 3 1.4 0.2
                 "treatment_a" 4.7 3.2 1.3 0.2
                 "treatment_b" 4.6 3.1 1.5 0.2
                 "treatment_b" 5 3.6 1.4 0.2
                 "treatment_b" 5.4 3.9 1.7 0.4
                 "treatment_c" 4.6 3.4 1.4 0.3
                 "treatment_c" 5 3.4 1.5 0.2
                 "treatment_c" 4.4 2.9 1.4 0.2
                 "treatment_a" 4.9 3.1 1.5 0.1
                 "treatment_a" 5.4 3.7 1.5 0.2
                 "treatment_a" 4.8 3.4 1.6 0.2
                 "treatment_b" 4.8 3 1.4 0.1
                 "treatment_b" 4.3 3 1.1 0.1
                 "treatment_b" 5.8 4 1.2 0.2
                 "treatment_c" 5.7 4.4 1.5 0.4
                 "treatment_c" 5.4 3.9 1.3 0.4
                 "treatment_c" 5.1 3.5 1.4 0.3
                 "treatment_a" 5.7 3.8 1.7 0.3
                 "treatment_a" 5.1 3.8 1.5 0.3
                 "treatment_b" 5.4 3.4 1.7 0.2
                 "treatment_b" 5.1 3.7 1.5 0.4
                 "treatment_c" 4.6 3.6 1 0.2
                 "treatment_c" 5.1 3.3 1.7 0.5', header = TRUE)


pr_mod <- prcomp(df_mod[, 2:5])
pr_pred_mod <- predict(pr_mod)

df2_mod <- data.frame(Treatment = df_mod$Treatment,
                  pr_pred_mod[, 1:2])

ggplot(data = df2_mod, aes(x = PC1, y = PC2, 
                       colour = Treatment, 
                       label = Treatment)) + 
    geom_text() + 
    stat_ellipse(show.legend = FALSE)

enter image description here