Barnaby
Barnaby

Reputation: 1480

error in embedded conditional expression

I have a conditional expression that when it is satisfied (==7) by the result of a function (VitPath$states) implies the summation of other embedded conditional expressions when their conditions (<0.1) are also satisfied resulting in G

G<-if(tail(VitPath$states,1)==7) 
{if(summary(ResFit)$coef[370]<0.1) summary(ResFit)$coef[64]*summary(ResFit)$coef[91]   
else 0 + 
if(summary(ResFit)$coef[371]<0.1) summary(ResFit)$coef[65]*summary(ResFit)$coef[93] 
else 0 +  
if(summary(ResFit)$coef[372]<0.1) summary(ResFit)$coef[66]*summary(ResFit)$coef[95] 
else 0 + 
if(summary(ResFit)$coef[373]<0.1) summary(ResFit)$coef[67]*summary(ResFit)$coef[97]    
else 0 +   
if(summary(ResFit)$coef[374]<0.1) summary(ResFit)$coef[68]*summary(ResFit)$coef[99]   
else 0 + 
if(summary(ResFit)$coef[375]<0.1) summary(ResFit)$coef[69]*summary(ResFit)$coef[101] 
else 0 + 
if(summary(ResFit)$coef[376]<0.1) summary(ResFit)$coef[70]*summary(ResFit)$coef[103] 
else 0 + 
if(summary(ResFit)$coef[377]<0.1) summary(ResFit)$coef[71]*summary(ResFit)$coef[105] 
else 0 +
if(summary(ResFit)$coef[378]<0.1) summary(ResFit)$coef[72]*summary(ResFit)$coef[107]   
else 0} else 0

It seems that the embedded conditional expressions are not properly expresed as it produces an error in the first embedded expression as it seem not to understand the multiplication.

Computing the asymptotic covariance matrix of estimates
Error en if (summary(ResFit)$coef[370] < 0.1) summary(ResFit)$coef[64] *  : 
valor ausente donde TRUE/FALSE es necesario

Is there a way to do this without changing the present format. When I place a parenthesis or brakets (summary(ResFit)$coef[71]*summary(ResFit)$coef[105]) it also does not work.

Many thanks

EDITED

ResFit is produced by the following expression X beeing a vector of numeric values

ResFit = HMMFit(X, nStates=9, control=list(init="KMEANS"))

Upvotes: 0

Views: 693

Answers (1)

IRTFM
IRTFM

Reputation: 263451

Try this. It's a Boolean algebra version; When the test fails, the result will be 0 but when it success it will be the product. I assume you only want the first column of the summary coef matrix since multiplying teh standard errors would not make much sense:

  G<-if(tail(VitPath$states,1)==7) {  summary(ResFit)$coef[370:378,1]<0.1 )*
                            ( summary(ResFit)$coef[64:72,1])*(summary(ResFit)$coef[91:107,1]) }

This should be much faster, as well as being clearer and easier to maintain. If you are using a modeling function different than lm, you should post further details. I was assuming the structure returned from the $coef-call would be like:

coefficients
a p x 4 matrix with columns for the estimated coefficient, its standard error, t-statistic and corresponding (two-sided) p-value. Aliased coefficients are omitted.

Upvotes: 1

Related Questions