mmeierer / REndo

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

problems summary function class ivreg #10

Closed Rgui closed 9 years ago

Rgui commented 9 years ago

When I am applying hmlewbel and then trying to run a summary on it, it gives me the error below:

h1 <- hmlewbel(y ~ X1 + X2 + P, IIV = "yp") summary(h1) Error in UseMethod("coeftest") : no applicable method for 'coeftest' applied to an object of class "ivreg"

h1 is of class "ivreg" so summary(h1) should be working. Can you point me to where is the error?

pschil commented 9 years ago

using the code on master@mmeierer/REndo I could not reproduce this error, it works fine for me:

h1 <- hmlewbel(y ~ X1 + X2 + P, IIV = "yp")
summary(h1)
Call:
hmlewbel(formula = y ~ X1 + X2 + P, IIV = "yp")

Residuals:
     Min       1Q   Median       3Q      Max 
-7.38447 -1.14233  0.03337  1.12850  6.30829 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.29445    0.69968   4.709 2.63e-06 ***
X1           1.40614    0.35607   3.949 8.06e-05 ***
X2           2.95752    0.07569  39.073  < 2e-16 ***
P           -1.00827    0.01751 -57.576  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.726 on 2496 degrees of freedom
Multiple R-Squared: 0.9095, Adjusted R-squared: 0.9094 
Wald test:  1606 on 3 and 2496 DF,  p-value: < 2.2e-16

What code and data do you use exactly? Which packages do you load? Do you have all dependencies of AER installed, especially lmtest? Do you build the whole REndo package and then run the code or simply source the files?

It is correct, that summary(h1) will call summary.ivreg (access directly with AER:::summary.ivreg as it is in the AER package) which then will call coeftest. coeftestis a member of the lmtest package which does not provide an implementation for ivreg (ie no coeftest.ivreg) however a default: coeftest.default which does work fine for me.

Rgui commented 9 years ago

It worked once I loaded the AER package by hand...which is strange cause I thought it should do that automatically since AER is in the Imports, in the DESCRIPTION file.

pschil commented 9 years ago

Imports will only load a package but not attach it. This way you can directly access it with :: but it is not in the search path and therefore wont be found automatically (i.e. when simply using a command without ::)

In your DESCRIPTION file: Imports: Load the package. Simply make sure it is installed but doesn’t make it available for use. Depends: Attach the package.

It is advised to use Ìmports because Depends will pollute the users environment and search path and possibly overwrite other functions with the same name as yours. -> Bad practice

In the NAMESPACE file you can make functions available for use without ::, internally in your package but also functions of your package to the outside. In the NAMESPACE file: import() all functions of a package importFrom() specific functions of a package export()make a function available for use (outside your package after calling library(REndo)) -> To make internalIV available for usage put export(internalIV)on a new line in your NAMESPACE file.

More details on this topic can be found on this very informativ website: http://r-pkgs.had.co.nz/namespace.html

However, I am still unable to reproduce your error. Only having REndo attached:

(.packages())
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     

obj <- hmlewbel(Data_hmlewbel$y1 ~ Data_hmlewbel$X1 +  Data_hmlewbel$X2 + Data_hmlewbel$P1, IIV = "yp")
Error: could not find function "hmlewbel"

library(REndo)

(.packages())
[1] "REndo"     "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
[8] "base"

detach("package:AER", unload = T) #make sure it is detached
Error in detach("package:AER", unload = T) : invalid 'name' argument

data("Data_hmlewbel")
obj <- hmlewbel(Data_hmlewbel$y1 ~ Data_hmlewbel$X1 +  Data_hmlewbel$X2 + Data_hmlewbel$P1, IIV = "yp")
summary(obj)

Call:
hmlewbel(formula = Data_hmlewbel$y1 ~ Data_hmlewbel$X1 + Data_hmlewbel$X2 + 
    Data_hmlewbel$P1, IIV = "yp")

Residuals:
[……..]

Starting from a fresh R session and cleared global environment, which commands do you run to create this error? If you do not build / compile a package (pressing the Build button or run devtools::build()) and then load it (devtools::load()) in a fresh R session the Description and Namespace file are not processed. So if you only source() some files and then run them, you have to load all the depending packages such as AERby hand using library().

( Or are you by any chance trying to use Summary instead of summary?)

Rgui commented 9 years ago

It works now. Indeed, I had to start with a new environment.