mmeierer / REndo

REndo - A R package to control for endogeneity by using internal instrumental variable models
15 stars 4 forks source link

copulaEndo class #12

Closed Rgui closed 8 years ago

Rgui commented 9 years ago

I want to create a "copulaEndo" class for the copulaEndo function. I already tried something, as you can see in the commented lines in the copulaEndo.R file.

The problem is that copulaEndo function calls 3 other functions, each of them returning a different type of result, as follows:

when function copulaCont1 is called, the returned slots should be:

when function copulaMethod2 is called, the returned slots should be:

when copulaDiscrete is called, the object returned should be of "lm" class, since copulaDiscrete returns the result of a "lm" call.

Would something like that be possible to implement? Or what solution would you suggest?

Rgui commented 9 years ago

to run copulaEndo function, you can find examples in the copulaEndo.R file, before the beginning of the body of the function.

pschil commented 8 years ago

I would suggest not to handle the classes in the copulaEndo function itself but rather return the results of the functions copulaCont1, copulaMethod2, and copulaDiscrete. Therefore, prepare the classes in these 3 functions.

copulaEndo <- function(y,X,P,param=NULL,type=NULL,method=NULL, intercept = NULL){
  if ("continuous" %in% c(type)){
         if ("1" %in% method )
        {
             print("Attention! The endogeneous regressor should be continuous and NOT normally distributed")
             ret= copulaCont1(y,X,P,param, intercept)
         } else 
             if ("2" %in% method) 
 ret = copulaMethod2(y,X,P, intercept) 
  } else
   if ("discrete" %in% c(type)) {
     ret=copulaDiscrete(y,X,P,intercept)     
}

return(ret)
}

The question remaining is what kind of class to use. It really depends on the planned further usage of this copulaEndo class object. Is it only kind of cosmetic for output or is is going to be used in other functions. Assuming it is mainly used for output I'd go for S3 classes here.

For example in copulaCont1:

res <- list(coefEndoVar=b[,k], coefExoVar=b[,1:(k-1)], rho=b[,k1-1],sigma=b[,k1], value=b$fevals, convcode=b$convcode)
class(res) <- c("copulaEndo") #make it a copulaEndo object
return(res)

I'm unsure if what you are using in copulaDiscrete really is an lm object. You are returning an object

res <- list(a.est = a.est, b.est = b.est, ps.est = ps.est, se.a=sd.a, se.b = sd.b, se.ps = sd.ps)

which is missing many attributes of a lm object.

However in copulaMethod2 a lm object seems to be returned:

f.lm <- lm(y ~.-1, data=dataCopula)
} else {f.lm <- lm(y ~., data=dataCopula)}
f.lm$call <- match.call()
return(f.lm)

Are you sure it is not copulaMethod2 which should return a lm object?

I hope I was able to make my explanations in a reasonably clear way, if not don't hesistate to tell.

Rgui commented 8 years ago

Thank you. I will do as you suggested.

Yes, it is CopulaMethod2 that returns an "lm" object. copulaDiscrete returns just a list of elements.