[英]Function for calculate quantile and qnorm for QQ plot in R
My data: 我的资料:
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", header = TRUE) mydata <-read.csv(“ myData.csv”,标头= TRUE)
mydataframe <- data.frame(mydata) mydataframe <-data.frame(mydata)
I did the following function to be applied to each column variable of my data frame, which contains 4 columns: 我将以下功能应用于数据框的每个列变量,该变量包含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")
}
When I run 当我跑步
qqfunc(mydataframe, Test1)
appears the Warning Message: 出现警告消息:
Removed 1 rows containing missing values (geom_abline).
删除了1个包含缺失值的行(geom_abline)。
As result, the QQ Plot doesn't appear in pdf output file. 结果,QQ图未出现在pdf输出文件中。 I am not sure if the problem is in the parsing or in the function ggplot().
我不确定问题出在解析还是在ggplot()函数中。
PS: PS:
1. Curiously, if I run these following commands outside the function, it works: 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.If I run these commands: 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)
Error message: 错误信息:
Error in (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, : object 'Test1' not found
如果(is.matrix(i))as.matrix(x)[[i]]则(函数(x,i,精确)中存在错误,否则.subset2(x,:找不到对象'Test1'
FULL CODE: 完整代码:
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)
Works with me. 和我一起工作。 You should have followed my suggestion.
你应该遵循我的建议。
And the suggestion of @Tung to post a sample dataset. 并建议@Tung发布样本数据集。 Since you have not, here is the complete working code.
由于您还没有,所以这里是完整的工作代码。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.