IV Regression in R - could not find function "linearHypothesis" - r

I am very new to R, and trying to reproduce a Stata Output. I have the following Regression
formula2 <- as.formula(paste("lnwd ~ dreformd + ", paste("", PredictorVar2, collapse='+'), "+", paste("", PredictorYr, collapse='+')))
with dreformd being endogenous. I want to run a 2SLS by regressing dreformd on three instruments
datatwo$qz1 <- factor (with (datatwo, ifelse ((q1 == 1), 1, 0)))
datatwo$qz2 <- factor (with (datatwo, ifelse ((q1 == 2), 1, 0)))
datatwo$qz3 <- factor (with (datatwo, ifelse ((q1 == 0|q1 == 3), 1, 0)))
This would be my code:
library(car)
iv = ivregress(formula2, dreformd ~ qz1 + z2 + qz3, datatwo)
And all I get as Output is
Error in ivregress(formula2, dreformd ~ qz1 + qz2 + qz3, datatwo) :
could not find function "linearHypothesis"
I am lost here, I've tried everything and have no clue where the problem is. I've also tried using the ivreg function, but R cannot find that one either. The AER and CAR packages are installed. What am I missing ?
EDIT: tried installing CAR and AER with dependencies, still get the same error. The car package cannot load, and there is no pbkrtest package. Tried installing it, I get this error:
Warning in install.packages :
package ‘pbkrtest’ is not available (for R version 3.2.1)

I was struggling with this too.
You have to install car. R is going to ask you to install several other packages in the process. I went one by one (as far as I remember there were 7 or 8 different packages) and at the end could run library(car) and then R accept the function linearHypothesis

Related

Error: No tidy method for objects of class dgCMatrix

I'm trying out a package regarding double machine learning (https://rdrr.io/github/yixinsun1216/crossfit/) and in trying to run the main function dml(), I get the following "Error: No tidy method for objects of class dgCMatrix" using example dataframe data. When looking through the documentation (https://rdrr.io/github/yixinsun1216/crossfit/src/R/dml.R), I can't find anything wrong with how tidy() is used. Does anyone have any idea what could be going wrong here?
R version 4.2.1
I have already tried installing broom.mixed, although broomextra doesn't seem to be available for my R version, and the same problem occurs. Code used below;
install.packages("remotes")
remotes::install_github("yixinsun1216/crossfit", force = TRUE)
library("remotes")
library("crossfit")
library("broom.mixed")
library("broom")
# Effect of temperature and precipitation on corn yield in the presence of
# time and locational effects
data(corn_yield)
library(magrittr)
dml_yield <- "logcornyield ~ lower + higher + prec_lo + prec_hi | year + fips" %>%
as.formula() %>%
dml(corn_yield, "linear", n = 5, ml = "lasso", poly_degree = 3, score = "finite")

Failing to produce glmmTMB diagnostics plots with package DHARMa

I have tried to produce diagnostics plots for glmmTMB models using package DHARMa without success. Example 1.1 in this vignette gives:
owls_nb1 <- glmmTMB(SiblingNegotiation ~ FoodTreatment*SexParent +
(1|Nest)+offset(log(BroodSize)),
contrasts=list(FoodTreatment="contr.sum",
SexParent="contr.sum"),
family = nbinom1,
zi = ~1,
data=Owls)
plot(owls_nb1_simres <- simulateResiduals(owls_nb1) )
# Error in on.exit(add = TRUE, { : invalid 'add' argument
The same happens with:
if (!require(RCurl)) install.packages('RCurl'); library(RCurl)
unicorns <- read.csv(text= RCurl::getURL("https://raw.githubusercontent.com/marcoplebani85/datasets/master/unicorns.csv"))
# simulated data, obviously
unicorns_glmmTMB <- glmmTMB(Herd_size_n ~ food.quantity
+ (1 + food.quantity | Locality)
+ (1 + food.quantity | Year_Month),
family="poisson",
data=unicorns)
plot(simulateResiduals(unicorns_glmmTMB))
# Error in on.exit(add = TRUE, { : invalid 'add' argument
If I run the same model in lme4::glmer:
unicorns_glmer <- glmer(Herd_size_n ~ food.quantity
+ (1 + food.quantity | Locality)
+ (1 + food.quantity | Year_Month),
family="poisson",
data=unicorns)
...and "feed" it to:
plot(simulateResiduals(unicorns_glmer))
I obtain diagnostics plots without issues (by the way I am aware that model unicorns_glmer is suboptimal and can be improved).
I'm using:
glmmTMB version 1.0.2.9000 freshly installed from github;
DHARMa version 0.4.1;
R version 3.6.3;
MacOS Sierra version 10.12.6.
Has anyone encountered the same problem? Does anyone know how to solve it?
EDIT: my question was originally on how packages performance and DHARMa handle glmmTMB objects. For the sake of focus and clarity I removed the references to package performance, thus making this question specific to glmmTMB and DHARMa.
It looks like this is a bug that was present in R <= 4.0.1. From the R NEWS file for version 4.0.2:
on.exit() now correctly matches named arguments, thanks to PR#17815 (including patch) by Brodie Gaslam.
I have attempted to fix the glmmTMB code so it works around the bug.
You could try
remotes::install_github("glmmTMB/glmmTMB/glmmTMB#on_exit_order")
and see if that helps (provided nothing goes wrong, this branch should be merged into master shortly ...)

How to run zero inflated poisson in R if zeroinfl function cannot be found?

I am using R version 3.6.0 and I am following this guide in order to run a zero-inflated poisson regression on cholera data: https://stats.idre.ucla.edu/r/dae/zip/
I have installed the pscl package as instructed, but when I run my code, an error message shows.
cholera2 <- within(cholera, {wateraccess <- factor(wateraccess, order=TRUE, levels=c(1,2,3,4))})
summary(cholera2)
summary(m1 <- zeroinfl(cases ~ wateraccess + atpc, data = cholera2))
I tried using a different version of R and it has the same issue.
Error message: Error in zeroinfl(cases ~ wateraccess + atpc, data = cholera2) : could not find function "zeroinfl"

R: pwfdtest produces an error message even with the test data

In R using the plm package, pwfdtest produces an error message even with the test data:
pwfdtest(log(emp) ~ log(wage) + log(capital), data = EmplUK, h0 = "fe")
Error message:
"Error in order(nchar(cnames), decreasing = TRUE) : 4 arguments
passed to .Internal(nchar) which requires 3".
Bug or am I missing something?
I had a similar question myself with a similar test (linearHypothesis). Apparently the nchar function changed, so you need to install the newest version of R and then update all your packages.
After re-installing R,
update.packages(checkBuilt=TRUE, ask=FALSE)

R: Glmer Model Error

By run of follow code in R:
m3 <- glmer(accoccur ~ Time + Year + Month + holiday + HolidayNum + (1 + Time | DistfKaraj) , data = accident, family = "binomial" , nAGQ =1)
Run stooped with this error:
Error in intI(i, n = x#Dim[1], dn[[1]], give.dn = FALSE) :
"anyNA" is not a BUILTIN function
anyNA() is a function that was introduced in R 3.1.0 -- search http://cran.r-project.org/src/base/NEWS for the function name. My guess is that you have the development version of lme4 (hosted here), which makes use of anyNA() (based on the blame, it was added in November 2014), but doesn't define it for you.
You have a few options:
Upgrade to at least R 3.1.0 -- current is 3.2.0.
Define anyNA() yourself, with anyNA <- function(x) any(is.na(x))
I'd recommend at least upgrading your version of R, unless you rely on packages that are unavailable in future versions.

Resources