Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return plot from R function

Tags:

Why won't my R function return or print a plot? The code is below. All of the code seems to work fine, except for the plot. No matter what I do, I can't get R to create the plot when the function is called. Looking around online, I can't find any reason why this wouldn't work.

powerc.fun <- function(n,sigma,r){  a <- 0.05 d <- seq(-20,20,2)  power <- rep(NA,length(d)) p.lab <- rep(NA,length(d))  for (j in 1:length(d)){    mu1 <- 110   mu2 <- mu1-d[j]      reject <- rep(NA,r)    for (i in 1:r){      sample1 <- rnorm(n,mu1,sigma)     sample2 <- rnorm(n,mu2,sigma)      sample.t <- t.test(sample1,sample2)     p.val <- sample.t[3]      reject[i] <- p.val<a       power[j] <- sum(reject)/length(reject)     p.lab[j] <- paste('d=',d[j],sep='')    }}  d.power <- cbind(d,power)  return(d.power)  p.plot <- plot(d.power[,1], d.power[,2], type="l", xlab=bquote(H[a]), ylab="Power", main="Power Calculations for Two Sample T Test")  print(p.plot) return(p.plot)  } 

Any ideas?

like image 389
Ryan Simmons Avatar asked Oct 27 '13 18:10

Ryan Simmons


People also ask

Can you plot a function in R?

The plot() function in R isn't a single defined function but a placeholder for a family of related functions. The exact function being called will depend upon the parameters used. At its simplest, plot() function simply plots two vectors against each other. This gives a simple plot for y = x^2.

Can a function return a list in R?

1 Answer. In R programming, functions do not return multiple values, however, you can create a list that contains multiple objects that you want a function to return.

Can a function return a function in R?

Answer: R returns the last output of a function automatically. We therefore do not need to use the return explicitly. However, using the return command is often considered as good practice, since it makes the R code easier to read and understand.


2 Answers

This works. As suggested, you can save more than one object by using a list:

powerc.fun <- function(n,sigma,r){  a <- 0.05 d <- seq(-20,20,2)  power <- rep(NA,length(d)) p.lab <- rep(NA,length(d))  for (j in 1:length(d)){    mu1 <- 110   mu2 <- mu1-d[j]      reject <- rep(NA,r)    for (i in 1:r){      sample1 <- rnorm(n,mu1,sigma)     sample2 <- rnorm(n,mu2,sigma)      sample.t <- t.test(sample1,sample2)     p.val <- sample.t[3]      reject[i] <- p.val<a       power[j] <- sum(reject)/length(reject)     p.lab[j] <- paste('d=',d[j],sep='')    }}  d.power <- cbind(d,power) p.plot <- plot(d.power[,1], d.power[,2], type="l", xlab=bquote(H[a]), ylab="Power", main="Power Calculations for Two Sample T Test")  return(list(p.plot, d.power))  }  # prints the plot and saves d.power values output <- powerc.fun(100,0.1,10)  # d.power values output[[2]] 

But probably you prefer just to save d.power and then call it for plotting the graph:

powerc.fun <- function(n,sigma,r){  a <- 0.05 d <- seq(-20,20,2)  power <- rep(NA,length(d)) p.lab <- rep(NA,length(d))  for (j in 1:length(d)){    mu1 <- 110   mu2 <- mu1-d[j]      reject <- rep(NA,r)    for (i in 1:r){      sample1 <- rnorm(n,mu1,sigma)     sample2 <- rnorm(n,mu2,sigma)      sample.t <- t.test(sample1,sample2)     p.val <- sample.t[3]      reject[i] <- p.val<a       power[j] <- sum(reject)/length(reject)     p.lab[j] <- paste('d=',d[j],sep='')    }}  d.power <- cbind(d,power)  return(d.power)  }  # saves d.power output <- powerc.fun(100,0.1,10)  # plot p.plot <- plot(output[,1], output[,2], type="l", xlab=bquote(H[a]), ylab="Power", main="Power Calculations for Two Sample T Test") 
like image 125
Stefano Lombardi Avatar answered Oct 15 '22 00:10

Stefano Lombardi


A call to "return()" ends the function call, so anything after it is ignored

Try getting rid of

return (d.power) 

or moving it to after you define and print the plot.

On a related note, you can't return two objects from one function. Pick one or put them in a list and return the list.

like image 27
janattack Avatar answered Oct 15 '22 01:10

janattack