[R] URGENT Help required

arnaud_amsellem@ssga.com arnaud_amsellem at ssga.com
Tue Nov 26 12:29:05 CET 2002


I've the following problem:
The below function runs a loop with regression analysis and stores F-Stat
in a matrix. When I call the matrix elements (models[i,j]) in the function
I get proper results, but when I try to call  the same elements outside the
function the matrix appears to be empty e.g when I call compt inside the
function I get: 5 but when I call it outside I get: 1. This is the same
problem with models which appears to be empty outside the function.
Specifically I would like being able to  run the same function for various
data frames and store the results in the same matrix (models) one after the
other.

# Set general variables & matrix
myDataFrame <- read.table("path",header=T)
models <- matrix(nrow = 20, ncol =8)
X <- matrix(nrow = 1, ncol = 6)
compt <- 1
deb <- 0
fin <- 0

# Runsanalysis
myFunction <- function(IndDF)
{
     indDF <- as.data.frame(IndDF)
     for (i in 2:(length(IndDF)-1))
     {
          for (j in ((i+1):(length(IndDF))))
          {
               for (k in 0:5)
               {
                    deb <- dim(IndDF)[1]-k*21-100
                    fin <- dim(IndDF)[1]-k*21
                    X[1,k+1] <- lm((IndDF[,i])[deb:fin]~(IndDF
[,j])[deb:fin]))$fstat
               }
               if (0.39*X[1,1]+0.25*X[1,2]+0.16*X[1,3]+0.10*X[1,4]
+0.06*X[1,5]+0.04*X[1,6] > 5)
               {
                    models[compt,1] <- "ind"
                    models[compt,2] <-
substr(as.character(names(IndDF)[i]),2,7)
                    models[compt,3] <-
substr(as.character(names(IndDF)[j]),2,7)
                    models[compt,6] <- 0.39*X[1,1]+0.25*X[1,2]+0.16*X[1,3]
+0.10*X[1,4]+0.06*X[1,5]+0.04*X[1,6]
                    models[compt,7] <- as.character((IndDF
[,1])[dim(IndDF)[1]-100])
                    models[compt,8] <- as.character((IndDF
[,1])[dim(IndDF)[1]])
                    compt <- compt+1
               }
          }
     }
}

Any help appreciated

Thanks
                           
                           
                           



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list