R:如何修复Bonferroni更正的错误编码

时间:2017-11-26 19:51:56

标签: r correlation summary pearson-correlation

我坚持如何在RStudio中进行Bonferroni校正编码和Pearson Correlation Matrix的原始P值。我是一名学生,也是R的新手。我也迷失了如何获得数据的平均值,SD和n的表格。当我计算Pearson Correlation Matrix时,我得到的是r值而不是原始概率值。我不知道如何在RStudio中编写代码。然后我尝试计算Bonferroni更正并收到一条错误消息,说明列表对象无法强制输入double。如何修复我的代码,这样就消失了?我还尝试为数据创建一个平均值,SD和n的表格,然后我开始坚持如何继续。

我的数据如下:

Tree Height DBA Leaf Diameter
45.3    14.9    0.76
75.2    26.6    1.06
70.1    22.9    1.19
95      31.8    1.59
107.8   35.5    0.43
93      26.2    1.49
91.5    29      1.19
78.5    29.2    1.1
85.2    30.3    1.24
50      16.8    0.67
47.1    12.8    0.98
73.2    28.4    1.2

我安装了dplyr,tidyr,multcomp,multcompview的软件包 我从excel CSV(逗号分隔)文件中读取数据并创建数据> dataHW8_1 12obs。 3个变量

summary(dataHW8_1)

然后我创建了数据的Scatterplots     plot(dataHW8_1 $ Tree_Height,dataHW8_1 $ DBA,main =“Scatterplot Tree Height Vs Trunk Diameter at Breast Height(DBA)”,xlab =“Tree Height(cm)”,ylab =“DBA(cm)”)     plot(dataHW8_1 $ Tree_Height,dataHW8_1 $ Leaf_Diameter,main =“Scatterplot Tree Height Vs Leaf Diameter”,xlab =“Tree Height(cm)”,ylab =“Leaf Diameter(cm)”)     plot(dataHW8_1 $ DBA,dataHW8_1 $ Leaf_Diameter,main =“Scatterplot Trunk Diameter at Breast Height(DBA)Vs Leaf Diameter”,xlab =“DBA(cm)”,ylab =“Leaf Diameter(cm)”)

然后我注意到数据不是线性的,所以我使用log()函数对其进行了转换     dataHW8_1log = log(dataHW8_1)

然后我使用转换后的数据重新创建了我的Scatterplots

plot(dataHW8_1log$Tree_Height,dataHW8_1log$DBA,main="Scatterplot of 
Transformed (log)Tree Height Vs Trunk Diameter at Breast Height 
(DBA)",xlab="Tree Height (cm)",ylab="DBA (cm)")
plot(dataHW8_1log$Tree_Height,dataHW8_1log$Leaf_Diameter,main="Scatterplot 
of Transformed (log)Tree Height Vs Leaf Diameter",xlab="Tree Height 
(cm)",ylab="Leaf Diameter (cm)")
plot(dataHW8_1log$DBA,dataHW8_1log$Leaf_Diameter,main="Scatterplot of 
Transformed (log) Trunk Diameter at Breast Height (DBA) Vs Leaf 
Diameter",xlab="DBA (cm)",ylab="Leaf Diameter (cm)")

然后我创建了Scatterplots的矩阵图

pairs(dataHW8_1log)
然后,我使用Pearson方法计算相关系数 这不会给出P值的无差错矩阵------你怎么做的?

cor(dataHW8_1log,method="pearson")

我被困在如何获取数据的原始概率矩阵(未修正的P值)

然后我计算了Bonferroni校正-----你是怎么做到的?

Data$Bonferroni = 
p.adjust(dataHW8_1log, 
method = "bonferroni")

这样做给了我以下错误:

 Error in p.adjust(dataHW8_1log, method = "bonferroni") : 
 (list) object cannot be coerced to type 'double'

我尝试使用lapply修复,但这并没有解决我的问题

然后我尝试制作一个平均值SD,n的表格,但我只能创建以下代码并且卡在那里去哪里------你怎么做的?

(,data = dataHW8_1log,
      FUN = function(x) c(Mean = mean(x, na.rm = T),
                          n = length(x),
                          sd = sd(x, na.rm = T))

我在网上尝试了以下示例,但没有一个能帮助我正确编码Bonferroni更正。如果有人能帮助我解释我做错了什么以及如何制作矩阵/表格我会非常感激。

1 个答案:

答案 0 :(得分:0)

以下是使用50行×10列样本数据帧的示例。

# 50 rows x 10 columns sample dataframe
df <- as.data.frame(matrix(runif(500), ncol = 10));

我们可以显示成对散点图。

# Pairwise scatterplot
pairs(df);

enter image description here

我们现在可以使用cor.test获取单个比较的p值。我们使用便利函数cor.test.p为所有成对比较执行此操作。为了在信用到期时给予信用,函数cor.test.p取自this SO post,并以dataframe为参数,同时返回未修正的p值矩阵。

# cor.test on dataframes
# From: https://stackoverflow.com/questions/13112238/a-matrix-version-of-cor-test
cor.test.p <- function(x) {
    FUN <- function(x, y) cor.test(x, y)[["p.value"]];
    z <- outer(
      colnames(x),
      colnames(x),
      Vectorize(function(i,j) FUN(x[,i], x[,j])));
    dimnames(z) <- list(colnames(x), colnames(x));
    return(z);
}

# Uncorrected p-values from pairwise correlation tests
pval <- cor.test.p(df);

我们现在通过对每行(或列,因为矩阵是对称的)应用Bonferroni校正来纠正多个假设检验,并且我们已经完成了。请注意,p.adjust将p值的向量作为参数。

# Multiple hypothesis-testing corrected p-values
# Note: pval is a symmetric matrix, so it doesn't matter if we correct 
# by column or by row
padj <- apply(pval, 2, p.adjust, method = "bonferroni");
padj;
#V1 V2        V3 V4        V5        V6 V7 V8        V9 V10
#V1   0  1 1.0000000  1 1.0000000 1.0000000  1  1 1.0000000   1
#V2   1  0 1.0000000  1 1.0000000 1.0000000  1  1 1.0000000   1
#V3   1  1 0.0000000  1 0.9569498 1.0000000  1  1 1.0000000   1
#V4   1  1 1.0000000  0 1.0000000 1.0000000  1  1 1.0000000   1
#V5   1  1 0.9569498  1 0.0000000 1.0000000  1  1 1.0000000   1
#V6   1  1 1.0000000  1 1.0000000 0.0000000  1  1 0.5461443   1
#V7   1  1 1.0000000  1 1.0000000 1.0000000  0  1 1.0000000   1
#V8   1  1 1.0000000  1 1.0000000 1.0000000  1  0 1.0000000   1
#V9   1  1 1.0000000  1 1.0000000 0.5461443  1  1 0.0000000   1
#V10  1  1 1.0000000  1 1.0000000 1.0000000  1  1 1.0000000   0