繁体   English   中英

R中QQ图的分位数和qnorm计算功能

[英]Function for calculate quantile and qnorm for QQ plot in R

我的资料:

Subject Test1 Test2 Test3 Test4  
    1   8   7   1   6  
    2   9   5   2   5  
    3   6   2   3   8  
    4   5   3   1   9  
    5   8   4   5   8  
    6   7   5   6   7  
    7   10  2   7   2  
    8   12  6   8   1

mydata <-read.csv(“ myData.csv”,标头= TRUE)
mydataframe <-data.frame(mydata)

我将以下功能应用于数据框的每个列变量,该变量包含4列:

qqfunc <- function(df,df_var) {    
          y <- quantile(df$df_var, c(0.25, 0.75))     
          x <- qnorm( c(0.25, 0.75))       
          slope <- diff(y) / diff(x)      
          int <- y[1] - slope * x[1]      
          ggplot() + aes(sample=df$df_var) + stat_qq(distribution=qnorm) +   
          geom_abline(intercept=int, slope=slope) + ylab("QQ")    
}  

当我跑步

qqfunc(mydataframe, Test1)

出现警告消息:

删除了1个包含缺失值的行(geom_abline)。

结果,QQ图未出现在pdf输出文件中。 我不确定问题出在解析还是在ggplot()函数中。

PS:
1.奇怪的是,如果我在函数外运行以下这些命令,它将起作用:

y <- quantile(mydataframe$Test1, c(0.25, 0.75)) # Find the 1st and 3rd quartiles  
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis
slope <- diff(y) / diff(x) # Compute the line slope
int <- y[1] - slope * x[1] # Compute the line intercept # Generate normal q-q plot   
ggplot() + aes(sample=mydataframe$Test1) + stat_qq(distribution=qnorm) + 
  geom_abline(intercept=int, slope=slope) + ylab("QQ Test1")  

2.如果我运行以下命令:

qqfunc <- function(df, df_var) {   
  y <- quantile(df[[df_var]], c(0.25, 0.75))   
  x <- qnorm( c(0.25, 0.75))  
  slope <- diff(y) / diff(x)  
  int <- y[1] - slope * x[1]  
  ggplot() + aes(sample=df[[df_var]]) + stat_qq(distribution=qnorm) + 
    geom_abline(intercept=int, slope=slope) + ylab("QQ")   
}
qqfunc(mydataframe, Test1)  

错误信息:

如果(is.matrix(i))as.matrix(x)[[i]]则(函数(x,i,精确)中存在错误,否则.subset2(x,:找不到对象'Test1'

完整代码:

library(Hmisc)  
library(ggplot2)  
library(boot)  
library(polycor)  
library(ggm)  
library(gdata)  
library(readxl)  
library(csvread)  
library (plyr)  
library(psych)  
library(mice)  
library(VIM)  
library(ez)   
library(reshape)   
library(multcomp)  
library(nlme)  
library(pastecs)  
library(WRS2)  
library(dplyr)  

mydata <- read.csv("mydata.csv", header = TRUE) # CSV  
mydataframe <- data.frame(mydata)  

y <- quantile(mydataframe$Test1, c(0.25, 0.75)) # Find the 1st and 3rd quartiles   
x <- qnorm( c(0.25, 0.75)) # Find the matching normal values on the x-axis   
slope <- diff(y) / diff(x) # Compute the line slope   
int <- y[1] - slope * x[1] # Compute the line intercept # Generate normal q-q plot   
ggplot() + aes(sample=mydataframe$Test1) + stat_qq(distribution=qnorm) + geom_abline(intercept=int, slope=slope) + ylab("QQ Test 1") 

qqfunc <- function(df, df_var) {     
         y <- quantile(df[[df_var]], c(0.25, 0.75))   
         x <- qnorm( c(0.25, 0.75))   
         slope <- diff(y) / diff(x)   
         int <- y[1] - slope * x[1]   
         ggplot() + aes(sample=df[[df_var]]) + stat_qq(distribution=qnorm) + 
           geom_abline(intercept=int, slope=slope) + ylab("QQ")   
}
qqfunc(mydataframe, Test1) 

和我一起工作。 你应该遵循我的建议。
并建议@Tung发布样本数据集。 由于您还没有,所以这里是完整的工作代码。

library(ggplot2)

qqfunc <- function(df, df_var) {    
          y <- quantile(df[[df_var]], c(0.25, 0.75))     
          x <- qnorm( c(0.25, 0.75))       
          slope <- diff(y) / diff(x)      
          int <- y[1] - slope * x[1]      
          ggplot() + aes(sample=df[[df_var]]) + stat_qq(distribution=qnorm) +   
              geom_abline(intercept=int, slope=slope) + ylab("QQ")    
}  

set.seed(3551)    # Make the results reproducible
n <- 100
mydataframe <- data.frame(X = rnorm(n))

column_variable <- "X"

qqfunc(mydataframe, column_variable)

qqplot

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM