Very strange function() behaviour.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Very strange function() behaviour.

iliketurtles
This post was updated on .
##EDIT: SOLVED. It turns out function() really dislikes length(.)=1 entries that are numeric vectors.
#I had to add:
if(is(t)=="numeric")t<-matrix(t)
#to make it work.. even though when I took off the function() notation and ran the code out on t<-2, it worked fine!



----------
#Hi, R newb here. I've coded a function that inputs N dimensional array(s) [or class=numeric if it's dim=1] of #coefficients and tstats, where dim(coef_matrix)=dim(tstat_matrix), it will then output a same dimension matrix of #coefficients pasted to tstats in brackets pasted to significance stars.

#The problem is it works maybe 30% of the time when I'm trying to do something like:
fun.starpaste(t=2,co=2)

#Yet, when I actually go INSIDE The function and set
t<-2
co<-2
#Then actually copy and paste the CONTENTS of the function into the R console, it works 100% of the time.

What?!?!?!?!?!!



#----------------------------------------------------------------------------------------------

             fun.starpaste <- function(t,co=NULL,tails=2,sig=c(0.9,0.95,0.99),tround=2)   #handles the pasting of #coef(tstat)significance_stars
 
             {

if(length(dim(t))>1)t3<-t else fake1<-1
t<-matrix(t)

if(is.null(co)==TRUE)fake<-1 else co<-matrix(co)

if(exists(as.character(substitute(fake1)))==FALSE)fake<-1 else t3<-t
                         
tstat<-as.numeric(t);tstat[is.na(tstat)]<-0

sig.l<-length(sig)
stars<-rawToChar(charToRaw("*********************************************"),multiple=T)


MISC1<-0
  for(x in 1:length(sig)){
     if(tails==1)cutoff<-qt(sig[x],Inf) else if(tails==2)cutoff<-qt(sig[x]+0.5*(1-sig[x]),Inf) else fake<-1
     MISC1<-cbind(MISC1,cutoff)
  }
cutoff<-MISC1[-1]


  for(i in 1:length(tstat)){
     if(abs(as.numeric(tstat[i]))<=cutoff[1])tstat[i]<-c("") else fake<-1
  }
  for(i in 1:length(tstat)){
  for(x in 1:(sig.l-1)){
     if(sum(apply(matrix(1:(sig.l+1),nrow=1),c(1,2),function(N){ if(tstat[i]==cbind(c(""),c("*"),c("**"),c("***"),c("****"),c("*****"),c("******"))[N])1 else 0  } ))==0 && abs(as.numeric(tstat[i]))>cutoff[x] && abs(as.numeric(tstat[i]))<cutoff[x+1])             tstat[i]<-paste(stars[1:x],collapse="",sep="") else fake<-1
         if(sum(apply(matrix(1:(sig.l+1),nrow=1),c(1,2),function(N){ if(tstat[i]==cbind(c(""),c("*"),c("**"),c("***"),c("****"),c("*****"),c("******"))[N])1 else 0  } ))==0 && abs(as.numeric(tstat[i]))>=cutoff[sig.l])        tstat[i]<-paste(stars[1:sig.l],collapse="",sep="") else fake<-1
  }}
options(warn=-1)
a<-as.numeric(tstat);b<-a[!is.na(a)]
if(sum(b)>0){a<-cbind(1:length(tstat),a);a<-a[!is.na(a[,2]),1];tstat[a]<-c("ERROR")} else fake<-1  

data2<-array(tstat,dim=dim(t))
dimnames(data2)<-dimnames(t)


t2<-format(round(t,tround),nsmall=tround,trim=T)
 if(is.null(co)==FALSE){
co<-format(round(co,coefround),nsmall=coefround,trim=T)
t<-paste(co,c("("),t2,c(")"),data2,sep="")
t<-array(t,dim=c(length(t),1))
dimnames(t)<-dimnames(data2)
 } else {t<-data2}
if(length(dim(t3))>1 && length(dim(t3)[dim(t3)==1])==0)t<-array(t,dim=dim(t3)) else fake<-1
return(t)

                     }
----

Isaac
Research Assistant
Quantitative Finance Faculty, UTS
Reply | Threaded
Open this post in threaded view
|

Re: Very strange function() behaviour.

iliketurtles
This post was updated on .
EDIT.
----

Isaac
Research Assistant
Quantitative Finance Faculty, UTS