rnorouzian
rnorouzian

Reputation: 7517

Code runs ok in R but fails in R-Fiddle, why?

I'm trying to run a piece of R code HERE on R-Fiddle with no success. The code runs very smoothly in R but doesn't run at all HERE on R-Fiddle?

Any advise is appreciated.

alt.hyp = function(N, d){

options(warn = -1) ; d = sort(d)
df = N - 1  ;  d.SE = 1/sqrt(N)  ;  ncp.min = min(d)*sqrt(N)  ;  ncp.max = max(d)*sqrt(N)
min.d = d.SE*qt(1e-5, df, ncp.min)  ;  max.d = d.SE*qt(0.99999, df, ncp.max)  

for(i in 1:length(d)){      
   H = curve(dt(d[i]*sqrt(N), df, x*sqrt(N)), min.d, max.d, n = 1e3, xlab = "Effect Size", 
       ylab = NA, ty = "n", add = i!= 1, bty = "n", yaxt = "n", font.lab = 2)

   polygon(H, col = adjustcolor(i, .7), border = NA)
   text(d[i], max(H$y), bquote(bolditalic(H[.(i-1)])), pos = 3, xpd = NA)
   axis(1, at = d[i], col = i, col.axis = i, font = 2)
   segments(d[i], 0, d[i], max(H$y), lty = 3)
   }
 }
# Example of use:
alt.hyp(N = 30, d = seq(0, 2, .5))

Upvotes: 2

Views: 52

Answers (1)

Severin Pappadeux
Severin Pappadeux

Reputation: 20080

Looks like older version of R is used on the R fiddle.

Anyway, if I redo your script in old style, it works, see here. The only changes are replacement of assignment from = to <- and single statement per line.

Code

alt.hyp <- function(N, d) {
    options(warn = -1)
    d <- sort(d)
    df <- N - 1
    d.SE <- 1/sqrt(N)
    ncp.min <- min(d)*sqrt(N)
    ncp.max <- max(d)*sqrt(N)
    min.d <- d.SE*qt(1e-5, df, ncp.min)
    max.d <- d.SE*qt(0.99999, df, ncp.max)

    for(i in 1:length(d)){      
        H <- curve(dt(d[i]*sqrt(N), df, x*sqrt(N)), min.d, max.d, n = 1e3, xlab = "Effect Size", ylab = NA, ty = "n", add = i!= 1, bty = "n", yaxt = "n", font.lab = 2)
       polygon(H, col = adjustcolor(i, .7), border = NA)
       text(d[i], max(H$y), bquote(bolditalic(H[.(i-1)])), pos = 3, xpd = NA)
       axis(1, at = d[i], col = i, col.axis = i, font = 2)
       segments(d[i], 0, d[i], max(H$y), lty = 3)
    }    

    N
}

q <- alt.hyp(N = 30, d = seq(0, 2, .5))
print(q)

And the output in the R Fiddle

enter image description here

Upvotes: 1

Related Questions