Loop does not print function output

后端 未结 3 721
情话喂你
情话喂你 2021-01-28 13:39

I\'m trying to make a loop which gives me back the bootstrapped confidence intervals for a regression analysis with one Intercept and three coefficients. Programming the bootstr

相关标签:
3条回答
  • 2021-01-28 13:55

    Automatic printing is turned off inside a loop, just as it is inside a function. You need to explicitly print something if you want to see the output.

    for (i in 2:inputnumberobjects)
    {
       cat(paste("BOOT CONFIDENCE INTERVALS FOR COEFFICIENT ", inputnamesobjects[i], ":\n\n", sep=""))
       print(boot.ci(bootResults, type = "bca", index=i)) ### Result for Coefficients
    }
    

    Hope that helps.

    0 讨论(0)
  • 2021-01-28 14:05

    I'm using the example data from the function help for boot.ci since you haven't included any. Some functions need to be forced to print when they're inside other functions, often using the function print. Copy the format of this:

    library(boot)
    ratio <- function(d, w) sum(d$x * w)/sum(d$u * w)
    city.boot <- boot(city, ratio, R = 999, stype = "w", sim = "ordinary")
    
    for (i in letters[1:5]) {
       cat("This is number:\n", i, "\n")
       print(boot.ci(city.boot, conf = c(0.90, 0.95),type = c("bca")))
    }
    

    Notice that you don't need to paste inside of cat. But in general it is good to avoid cat as print is a slightly gentler function. Using cat can lead to annoying messages that are hard later.

    In future, please supply reproducible examples! (And let us know which packages you're using!)

    0 讨论(0)
  • 2021-01-28 14:06

    If you just want to see the output, wrap your function in a print command.

    for (i in 2:inputnumberobjects)
      {
       cat(paste("BOOT CONFIDENCE INTERVALS FOR COEFFICIENT ", inputnamesobjects[i], ":\n\n", sep=""))
       print(boot.ci(bootResults, type = "bca", index=i)) ### Result for Coefficients
      }
    
    0 讨论(0)
提交回复
热议问题