Draw a trend arrow with confidence interval

Let draw an arrow with its width being dependent on the confidence interval:




drawTrend <- function(x, y, show.circle=TRUE, show.0=TRUE) {

  model <- lm(y ~ x)
  direction.se <- coef(summary(model))["x", "Std. Error"]
  direction <- model$coefficients["x"]
  
library("car")
k <- ellipse(center=c(0.5, 0), radius=c(0.5, 0.5), 
             shape=matrix(data = c(1, 0, 0, 1), nrow=2), 
             segments=1001, draw = FALSE)
par(mar=c(0, 0, 0, 0))
plot(x = c(0, 0), y=c(0, 0), type="n", bty="n", xlim=c(-0.1,1.1), 
     ylim=c(-0.6, 0.6), las=1, asp=1,
     xaxt="n", yaxt="n", xlab="", ylab = "")
if (show.circle) polygon(k[, 1], k[, 2], lwd=1)

maxy <- which.max(k[, 2])
maxx <- which.max(k[, 1])
miny <- which.min(k[, 2])
minx <- which.min(k[, 1])
if (show.circle) {
s <- seq(from=maxy, to=maxx, length.out = 11)
for (pos in 0:10) {
  s_pos <- s[11-pos]
  points(x=k[s_pos,1], y=k[s_pos,2], pch="+")
  text(x=k[s_pos,1]*1.05, y=k[s_pos,2]*1.05, labels=pos)
}
s <- seq(from=miny, to=1001, length.out = 11) 
for (pos in 1:10) {
  s_pos <- s[11-pos]
  points(x=k[s_pos,1], y=k[s_pos,2], pch="+")
  text(x=k[s_pos,1]*1.05, y=k[s_pos,2]*1.05, labels=paste0("-", pos))
}
}

# 0 est égal à 0
# 10 est égal à pi/2


polygon(x=c(0, (0.5+cos((direction-1.96*direction.se)*(pi/2/10))*0.5)*0.8, 
        0.5+cos(direction*(pi/2/10))*0.5, 
        (0.5+cos((direction+1.96*direction.se)*(pi/2/10))*0.5)*0.8, 
        0), 
        y=c(0, (sin((direction-1.96*direction.se)*(pi/2/10))*0.5)*0.8, 
        sin(direction*(pi/2/10))*0.5, 
        (sin((direction+1.96*direction.se)*(pi/2/10))*0.5)*0.8, 
        0), col="lightgrey", border=NA)

lines(x = c(0, 0.5+cos(direction*(pi/2/10))*0.5), y=c(0, sin(direction*(pi/2/10))*0.5), lty=3)

lines(x = c(0, (0.5+cos((direction+1.96*direction.se)*(pi/2/10))*0.5)*1),
      y=c(0, (sin((direction+1.96*direction.se)*(pi/2/10))*0.5)*1), lty=2)
lines(x = c(0, (0.5+cos((direction-1.96*direction.se)*(pi/2/10))*0.5)*1),
      y=c(0, (sin((direction-1.96*direction.se)*(pi/2/10))*0.5)*1), lty=2)
if (show.0) lines(x=c(0, 1), y=c(0,0), lty=3, lwd=2, col="red")
}

Different ways to use it:

layout(mat = matrix(1:4, nrow=2))

# An example with 11 xs of data and a clear trend

x <- 1990:2000
n <- floor(rnorm(n = length(x), mean=100, sd=10))
n <- n + (1:length(x))*5

drawTrend(x, n)

# An example with 11 xs of data and no trend

x <- 1990:2000
n <- floor(rnorm(n = length(x), mean=100, sd=10))

drawTrend(x, n)

# An example with 6 xs of data and no trend

x <- 2000:2005
n <- floor(rnorm(n = length(x), mean=100, sd=10))

drawTrend(x, n)

# An example with 6 xs of data and a trend

x <- 2000:2005
n <- floor(rnorm(n = length(x), mean=100, sd=10))
n <- n - (1:length(x))*5

drawTrend(x, n)

layout(1)

x <- 2000:2005
n <- floor(rnorm(n = length(x), mean=100, sd=10))
n <- n - (1:length(x))*5

drawTrend(x, n, show.circle = FALSE)

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04