我的数据:
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)
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)
错误讯息:
(function(x,i,exact)if(is.matrix(i))as.matrix(x)[[i]] else .subset2(x,: 对象&#39;测试1&#39;找不到
完整代码:
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)
答案 0 :(得分:1)
与我合作。你应该听从我的建议 并建议@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)