Hallo guys, i'm having some trouble to plot data. i got 2 methods and both give me different results. I don't get it.
In a previous post someone told me the function "stat_function" in ggplot2 it's like the "curve"-function but i don't get the same result.
1st. Methode (with curve):
# draw.data:
draw.data <- function(xy,xlab="log10",ylab="",pch=16,col=1){
plot(xy,xlab=xlab,ylab=ylab,pch=pch,col=col)
}
# f.probit
f.probit <- function(x,beta1=0,beta2=1,minv=0,maxv=1){
return(pnorm(beta1+beta2*x)*(maxv-minv)+minv)
}
# draw.probit
draw.probit <-function(beta1=0,beta2=1,minv=0,maxv=1,col=1,
lwd=2,lty=1,add=T,from=0,to=1){
if (add){
curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),add=T,col=col,lwd=lwd,lty=lty)
}else{
curve(f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv),from=from,to=to,col=col,lwd=lwd,lty=lty)
}
}
2nd. Methode (with ggplot)
# draw.data:
draw.data <- function(xy, add = F, mod = "Data", FUN = NULL){
# Bibliothek für ggplot-Funktion
# Dependencies: > library("ggplot2") must be imported!
x.lab <- "concentration [M]"
y.lab <- "normalised luminescence [%]"
my_labels <- parse(text = paste("1E", seq(-10, -4, 1), sep = ""))
# Find max, min and difference
# y.max <- max(my.data$y)
# y.min <- min(my.data$y)
y.max <- 1
y.min <- 0
diff <- y.max - y.min
# Find percentage and apply to new column
data <- data.frame(xy)
my.data <- data.frame(x=data$x,y=apply(data, 1, function(z) ((z['y'] - y.min)/diff)*100),model = mod)
if(!add){
quartz() # windows() unter MS Windows
ggplot(my.data, aes(x, y, group = model, color = model)) +
geom_point() +
#geom_line() +
#stat_function(fun = FUN, geom = "line", aes(group = model, colour = model)) +
# Draw 2 lines at 50% and 90% through the y-axis
geom_hline(yintercept = c(50, 90), linetype = "dotted") + # draw dotted horizontal lines at 50 and 90
scale_x_continuous(x.lab, breaks = seq(-10, -4, 1), labels = my_labels) +
labs(title = "Graph", x = x.lab, y = y.lab)
} else{
#geom_line(aes(x, y, group = model, color = model), data = my.data) +
stat_function(fun = FUN, geom = "line", aes(x, y, group = model, colour = model))
}
}
# f.probit remains the same!
# draw.probit
draw.probit <- function(xy, beta1 = 0, beta2 = 1,minv = 0, maxv = 1,
mod = "Probit", add = T){
# Aufruf der Funktion f.probit zur Verbesserung der y-Werte
#f <- f.probit(xy[,1],beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)
selected_FUN <- function(x){
f.probit(x,beta1=beta1,beta2=beta2,minv=minv,maxv=maxv)
}
draw.data(xy, add, mod, selected_FUN)
}
And hier are the data:
> xy
x y
[1,] -10 1.14259527
[2,] -9 1.15024188
[3,] -8 1.10517450
[4,] -7 1.00961311
[5,] -6 0.71238360
[6,] -5 0.20355333
[7,] -4 0.04061895
[8,] -10 1.11022461
[9,] -9 1.11083317
[10,] -8 1.07867942
[11,] -7 0.98422000
[12,] -6 0.73539660
[13,] -5 0.36134577
[14,] -4 0.18124645
[15,] -10 2.13212408
[16,] -9 1.14529425
[17,] -8 1.25102307
[18,] -7 1.16045169
[19,] -6 0.50321380
[20,] -5 0.15422609
[21,] -4 0.10198811
[22,] -10 1.16539392
[23,] -9 1.15855333
[24,] -8 1.11766975
[25,] -7 0.97204379
[26,] -6 0.53504417
[27,] -5 0.17431435
[28,] -4 0.29470416
[29,] -10 1.03683145
[30,] -9 1.07524250
[31,] -8 1.07761291
[32,] -7 0.96401682
[33,] -6 0.78346457
[34,] -5 0.32783725
[35,] -4 0.08103084
[36,] -10 0.81372339
[37,] -9 0.85402909
[38,] -8 0.86584396
[39,] -7 0.80705470
[40,] -6 0.53086151
[41,] -5 0.15711034
[42,] -4 0.11496499
Now when i start draw.data(xy) in both cases i get respectively these curves:
Which is exactly, what i expected. But when i start 'draw.probit' i get:
1st. Methode (as expected):
> draw.probit(beta1 -4.827511, beta2 = -0.8401166, minv = 0.05, maxv = 1, add = T)
2nd. Methode (Error)
mapping: x = x, y = y, group = model, colour = model
geom_line:
stat_function: fun = function (x)
{
f.probit(x, beta1 = beta1, beta2 = beta2, minv = minv, maxv = maxv)
}, n = 101, args = list()
position_identity: (width = NULL, height = NULL)
>
Now the question :-)
What can i do to get the same curve like in the 1st. method.... Please can someone help? I'm getting tired trying everything.
Thanks guys!
I think the answer you're looking for might be found somewhere here. This is a question from a year or two ago and shows really nice examples of how to fit a logit and probit model to a ggplot2 curve. I believe what you're looking for is something along the lines of
stat_smooth(method="glm",family="binomial",link="probit")
but you may have to play around with that a bit to get it to work. When I tried with a subset of your data set, I got an error
Error in eval(expr, envir, enclos) : y values must be 0 <= y <= 1
which has something to do with how the regression model is set up. You might find some of these links helpful for dealing with that.