Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Wrapping the body of the loop in <code>try</code> will allow it to continue after an error occurs. Also, you can assign an entire row of <code>results</code> at once like this:</p> <pre><code>for(i in 1:8)try({fit&lt;-lm(as.vector(funds[,i])~TBY,data=factors,na.action=na.omit) results[i,]&lt;-coef(summary(fit))[1,] }) ## Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : ## 0 (non-NA) cases results ## estimates residual t p ## fund1 0.1977773 0.1949221 1.0146478 0.4953715 ## fund2 0.7192174 0.2862573 2.5124861 0.2411462 ## fund3 2.9271787 NaN NaN NaN ## fund4 0.8757588 2.1261925 0.4118906 0.7512633 ## fund5 -0.3371507 0.5472105 -0.6161262 0.6005921 ## fund6 0.2844758 0.3068079 0.9272114 0.4222080 ## fund7 NA NA NA NA ## fund8 -0.2380825 0.2613918 -0.9108262 0.4295420 </code></pre> <p>Incidentally, you avoid the loop entirely with <code>sapply</code> and <code>tryCatch</code>:</p> <pre><code>sapply(funds,function(x) tryCatch(coef(summary(lm(x ~ TBY,data=factors,na.action=na.omit)))[1,], error= function(x)rep(NA,4))) ## fund1 fund2 fund3 fund4 fund5 fund6 fund7 ## Estimate 0.1977773 0.7192174 2.927179 0.8757588 -0.3371507 0.2844758 NA ## Std. Error 0.1949221 0.2862573 NaN 2.1261925 0.5472105 0.3068079 NA ## t value 1.0146478 2.5124861 NaN 0.4118906 -0.6161262 0.9272114 NA ## Pr(&gt;|t|) 0.4953715 0.2411462 NaN 0.7512633 0.6005921 0.4222080 NA ## fund8 ## Estimate -0.2380825 ## Std. Error 0.2613918 ## t value -0.9108262 ## Pr(&gt;|t|) 0.4295420 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload