0

I was trying to create a function to calculate leverages for some data, and I came to this:

leverages = function(x,n){
   n =  length(x)
  leverage = array(0,length(x))
   for (i in 1:length(x)){
    leverage[i] = round(1/n + ((mean(x)-x[i])^2)/(n-1)*var(x), d = 4);
    cat(paste("h_ii number", c(i,i), "equals", round(print(leverage[i]),d = 4)))
  }
}

Here's the output, for some leverages(X,length(X)):

[1] 11.3596
h_ii number 1 equals 11.3596[1] 1.9143
h_ii number 2 equals 1.9143[1] 0.0577

How can I make this output better? Thank you!

  • 1
    what's wrong with it? What's your desired output? Check the help pages for `?cat`, `?sprintf`, they'll help you with 95% of outputting cases. In your case I think you need a `\n` in `cat`. See also https://stackoverflow.com/q/12775085/3576984 – MichaelChirico Apr 16 '20 at 14:04
  • Thank you MichaelChirico. I wanted an output in the style of "h_ii number 1 equals 11.3596", for all cases. Do you think a \n suffices? – Duarte Silva Apr 16 '20 at 16:41
  • How do you want to make your output better? please append a sample of your expected output . – user27665 Apr 16 '20 at 16:45
  • you can remove the print() [cat will do the printing for you] and add \n and you should be good – MichaelChirico Apr 16 '20 at 16:50
  • @user27665 I wanted to be in the style of `h_ii number 1 equals 11.3596` without the `[1] 1.9143` next to it. – Duarte Silva Apr 17 '20 at 01:37
  • @MichaelChirico thanks for the info. Still, it doesn't seem to be working. Maybe because of the data class? – Duarte Silva Apr 17 '20 at 01:38

0 Answers0