The reduced line of the main axes and CI for ggplot in R

In any case, do you need to add a reduced line of the main axes (and ideally CI) to ggplot? I know that I can use the = "lm" method to get OLS compliance, but there seems to be no default method for RMA. I can get RMA coefficients and CI interval from lmodel2 package, but adding them with geom_abline () does not work. Here the dummy data and code. I just want to replace the OLS and CI line with the RMA and CI line:

dat <- data.frame(a=log10(rnorm(50, 30, 10)), b=log10(rnorm(50, 20, 2))) ggplot(dat, aes(x=a, y=b) ) + geom_point(shape=1) + geom_smooth(method="lm") 

Edit1: the code below receives RMA codes (here called SMA - standardized major axis) and CI. The lmodel2 package provides more verbose output, while the smatr package only returns coefficients and CI if that helps:

 library(lmodel2) fit1 <- lmodel2(b ~ a, data=dat) library(smatr) fit2 <- line.cis(b, a, data=dat) 
+4
source share
2 answers

As Chase commented, the actual lmodel2() code and the ggplot code you use will be helpful. But here is an example that can point you in the right direction:

 dat <- data.frame(a=log10(rnorm(50, 30, 10)), b=log10(rnorm(50, 20, 2))) mod <- lmodel2(a ~ b, data=dat,"interval", "interval", 99) #EDIT: mod is a list, with components (data.frames) regression.results and # confidence.intervals containing the intercepts+slopes for different # estimation methods; just put the right values into geom_abline ggplot(dat,aes(x=b,y=a)) + geom_point() + geom_abline(intercept=mod$regression.results[4,2], slope=mod$regression.results[4,3],colour="blue") + geom_abline(intercept=mod$confidence.intervals[4,2], slope=mod$confidence.intervals[4,4],colour="red") + geom_abline(intercept=mod$confidence.intervals[4,3], slope=mod$confidence.intervals[4,5],colour="red") + xlim(c(-10,10)) + ylim(c(-10,10)) 

Full disclosure: I don't know anything about RMA regression, so I just plucked the appropriate slopes and intercepted them and inserted them into geom_abline() , using the sample code from lmodel2 as a guide. The CIs created in this toy example don't seem to make much sense, since I had to force ggplot to zoom out using xlim() and ylim() to see the CI lines (red).

But perhaps this will help you create a working example in ggplot() .

EDIT2: when adding OP code to extract the coefficients, ggplot() would be something like this:

 ggplot(dat,aes(x=b,y=a)) + geom_point() + geom_abline(intercept=fit2[1,1],slope=fit2[2,1],colour="blue") + geom_abline(intercept=fit2[1,2],slope=fit2[2,2],colour="red") + geom_abline(intercept=fit2[1,3],slope=fit2[2,3],colour="red") 
+5
source

Having compiled Joranโ€™s answer, I think itโ€™s a little easier to transfer the entire geom_abline data geom_abline :

 library(ggplot2) library(lmodel2) dat <- data.frame(a=log10(rnorm(50, 30, 10)), b=log10(rnorm(50, 20, 2))) mod <- lmodel2(a ~ b, data=dat,"interval", "interval", 99) reg <- mod$regression.results names(reg) <- c("method", "intercept", "slope", "angle", "p-value") ggplot(dat) + geom_point(aes(b, a)) + geom_abline(data = reg, aes(intercept = intercept, slope = slope, colour = method)) 
+7
source

All Articles