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?
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.
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.
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.
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With