How to realize Variance Analysis and Visualization in R language
Today, I would like to share with you how R language to achieve analysis of variance and visualization of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
Analysis of variance (Analysis of Variance, abbreviated as ANOVA), also known as "analysis of variance", is invented by R.A.Fisher and is used to test the significance of the difference in the mean of two or more samples. Due to the influence of various factors, the data obtained in the study fluctuated. The causes of fluctuations can be divided into two categories, one is uncontrollable random factors, and the other is the controllable factors imposed in the study to affect the results.
Classification of analysis of variance:
Analysis of variance is commonly used for single-factor analysis of variance and two-way analysis of variance, while multi-factor analysis of variance is less complex. Here, the first two kinds of analysis of variance are mainly introduced.
Single factor analysis of variance
One-way ANOVA compares the mean values of dependent variables in two or more groups defined by classification factors. Different from t-test: t-test is suitable for the comparison of the mean of two columns of data. One-way ANOVA is suitable for the mean comparison of two or more columns of data. But for the comparison of the mean of two columns of data, one-way analysis of variance = double-tailed t-test of equal variance hypothesis.
Take the built-in data PlantGrowth set in R as an example. Weight data of plants planted in different plots ('ctrl',' trt1', and 'trt2'); the problems solved are as follows
1. Is there any difference in yield among different plots?
Which is the bigger difference between 2.trt1 and trt2?
# # normality test my_data 0.05accepts the original hypothesis, indicating that the data are normally distributed at multiple levels. # # Variance homogeneity test bartlett.test (weight~group,data=my_data) # the results show that pair0.2371 > 0.05.It accepts the original hypothesis, indicating that the data are equal variance at different levels. # # one-way ANOVA fitF) # group 2 3.766 1.8832 4.846 0.0159 * # Residuals 27 10.492 0.3886 #-# Signif. Codes: 0'* * '0.001'. The results showed that there were significant differences in yield among different plots. # if you use the nonparametric test Kruskal-Wallis testkruskal.test (weight ~ group, data = my_data) # # multiple comparison (multiple T test), which part of the difference is more obvious? the TukeyHSD () function in the R language provides a pairwise test for the mean difference of each group. TukeyHSD (fit) # if you use KW to verify multiple comparisons, pairwise.wilcox.test (PlantGrowth$weight, PlantGrowth$group, p.adjust.method = "BH") is a better method + drawing display:
Ggpubr package is used for analysis of variance, and the results can be visualized.
Library ("ggpubr") compare_means (weight ~ group, data = PlantGrowth,method= "anova") compare_means (weight ~ group, data = PlantGrowth,method= "kruskal.test") my_comparisons