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)
mydataframe <- data.frame(mydata)
I did the following function to be applied to each column variable of my data frame, which contains 4 columns:
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).
As result, the QQ Plot doesn't appear in pdf output file. I am not sure if the problem is in the parsing or in the function ggplot().
PS:
1. Curiously, if I run these following commands outside the function, it works:
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:
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
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. 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.