Stargazer error with polr in R - r

I obtain an error when using stargazer in conjunction with polr from the MASS package in R. Here is an example:
library(MASS)
library(stargazer)
# Fake data
set.seed(1234)
fake_data <- data.frame(y = as.factor(sample.int(4, 20, replace = TRUE)),
x1 = rnorm(20, mean = 1, sd = 1),
x2 = rnorm(20, mean = -1, sd = 1))
# Ordered logistic regression
o_log <- MASS::polr(y ~ x1 + x2,
data = fake_data,
Hess = TRUE, method = "logistic")
summary(o_log)
# Create regression table
stargazer(o_log)
I receive the following error message:
% Error: Unrecognized object type.
Does anyone know how to solve this? Thanks in advance.
P.S.: I'm on OS X 10.13, using R 3.4.3, MASS 7.3.47, and stargazer 5.2.
EDIT: According to stargazer's vignette, objects from polr should be supported.

I don't know the reason but when I change MASS::polr into plor, the error is removed and it works fine. It seems that it is a bug of package stargazer.

I encountered the same problem. For some strange reason, this only happens when you call the function using :: (in your case: MASS::polr). It doesn't happen when you first load the package via library(MASS) and then call the specific function.
See: Why do I get different results when using library(MASS) vs. MASS::?

I guess it was because you didn't load the MASS library and instead called the function using ::. MASS library doing some updates on how summary works for polr, which is being used by stargazer to generate the table. By not loading the library, the update was not happened, hence bringing you some trouble with stargazer.

Related

Problem with running emmeans (error in assign '.Last.ref_grid')

I have been having trouble with running emmeans function (from the emmeans package) whenever I try to follow up a two way between groups ANOVA with estimated marginal means.
A simple example:
library(emmeans)
library(tidyverse)
df <- tibble(fct1 = factor(rep(1:3, 10)),
fct2 = factor(rep(2:1, 15)),
DV = rnorm(30, 100, 15))
model1 <- lm(DV ~ fct1 * fct2, df)
emmeans(model1, "fct1", by = "fct2")
Returns:
Error in assign(".Last.ref_grid", object, inherits = TRUE) :
cannot change value of locked binding for '.Last.ref_grid'
No matter what data I run it on, always the same error shows up.
Thank you for any help!
This should stop it:
emm_options(save.ref_grid = FALSE)
This will keep it from saving the most recently created reference grid (or trying to, in your case). However, it may be worth trying to understand why this is happening. If you do:
.Last.ref_grid
you should see what it is that was last saved. That might be a clue. And try to delete it.

Is R Sandwich package not generating the expected clustered robust standard errors?

Load data
utils::data("InstInnovation", package = "sandwich")
df <- InstInnovation
Create group variable combining 'company' and 'year'
df[['cluster_var']] <- factor(paste0(df$company,"-",df$year))
Linear regression model
model <- lm(sales ~ competition + log(capital/employment) + year, data = df)
Why this:
lmtest::coeftest(model, vcov = vcovCL(model, type="HC3", cluster=~company+year))
Produces Standard Errors DIFFERENT than this?
lmtest::coeftest(model, vcov = vcovCL(model, type="HC3", cluster=~cluster_var))
Shouldn't cluster=~company+year and cluster=~cluster_var be equivalent?
In addition, I cannot find a place (e.g. Github) to report issues on R sandwich package, I found this but is just a read-only mirror: https://github.com/cran/sandwich
Thank you very much in advance.
cluster=~company+year is indeed something different: 'multiway clustering'. I found the explanation here:
http://fmwww.bc.edu/repec/bost10/BOS10.baum.pdf
https://francish.netlify.app/post/note-on-robust-standard-errors/

Error in eval(parse()) - r unable to find argument input

I am very new to R, and this is my first time of encountering the eval() function. So I am trying to use the med and boot.med function from the following package: mma. I am using it to conduct mediation analysis. med and boot.med take in models such as linear models, and dataframes that specify mediators and predictors and then estimate the mediation effect of each mediator.
The author of the package gives the flexible option of specifying one's own custom.function. From the source code of med, it can be seen that the custom.function is passed to the eval(). So I tried insert the gbmt function as the custom function. However, R kept giving me error message: Error during wrapup: Number of trees to be used in prediction must be provided. I have been searching online for days and tried many ways of specifying the number of trees parameter n.trees, but nothing works (I believe others have raised similar issues: post 1, post 2).
The following codes are part of the source code of the med function:
cf1 = gsub("responseY", "y[,j]", custom.function[j])
cf1 = gsub("dataset123", "x2", cf1)
cf1 = gsub("weights123", "w", cf1)
full.model[[j]] <- eval(parse(text = cf1))
One custom function example the author gives in the package documentation is as follows:
temp1<-med(data=data.bin,n=2,custom.function = 'glm(responseY~.,data=dataset123,family="quasibinomial",
weights=weights123)')
Here the glm is the custom function. This example code works and you can replicate it easily (if you have mma installed and loaded). However when I am trying to use the gbmt function on a survival object, I got errors and here is what my code looks like:
temp1 <- med(data = data.surv,n=2,type = "link",
custom.function = 'gbmt(responseY ~.,
data = dataset123,
distribution = dist,
train_params = start_stop,
cv_folds=10,
keep_gbm_data = TRUE,
)')
Anyone has any idea how the argument about number of trees n.trees can be added somewhere in the above code?
Many thanks in advance!
Update: in order to replicate the example code, please install mma and try the following:
library("mma")
data("weight_behavior") ##binary x #binary y
x=weight_behavior[,c(2,4:14)]
pred=weight_behavior[,3]
y=weight_behavior[,15]
data.bin<-data.org(x,y,pred=pred,contmed=c(7:9,11:12),binmed=c(6,10), binref=c(1,1),catmed=5,catref=1,predref="M",alpha=0.4,alpha2=0.4)
temp1<-med(data=data.bin,n=2) #or use self-defined final function
temp1<-med(data=data.bin,n=2, custom.function = 'glm(responseY~.,data=dataset123,family="quasibinomial",
weights=weights123)')
I changed the custom.function to gbmt and used a survival object as responseY and the error occurs. When I use the gbmt function on my data outside the med function, there is no error.

Error in Bagging with party::cforest

I'm trying to bag conditional inference trees following the advice of Kuhn et al in 'Applied Predictive Modeling', Ch.8:
Conditional inference trees can also be bagged using the cforest function > in the party package if the argument mtry is equal to the number of
predictors:
library(party)
The mtry parameter should be the number of predictors (the
number of columns minus 1 for the outcome).
bagCtrl <- cforest_control(mtry = ncol(trainData) - 1)
baggedTree <- cforest(y ~ ., data = trainData, controls = bagCtrl)
Note there may be a typo in the above code (and also in the package's help file), as discussed here:
R package 'partykit' unused argument in ctree_control
However when I try to replicate this code using a dataframe (and trainData in above code is also a dataframe) such that there is more than one independent/predictor variable, I'm getting an error though it works for just one independent variable:
Some dummy code for simulations:
library(party)
df = data.frame(y = runif(5000), x = runif(5000), z = runif(5000))
bagCtrl <- cforest_control(mtry = ncol(df) - 1)
baggedTree_cforest <- cforest(y ~ ., data = df, control = bagCtrl)
The error message is:
Error: $ operator not defined for this S4 class
Thanks for any help.
As suggested, posting my comment from above as an answer as a general R 'trick' if something expected doesn't work and the program has several libraries loaded:
but what solved it was adding the party namespace explicitly to the function > call, so party::cforest() instead of just cforest(). I've also got
library(partykit) loaded in my actual program which too has a cforest()
function and the error could be stemming from there though both functions are > essentially the same
caret::train() is another example where this often pops up

R Leaps Package: Regsubsets - coef "Reordr" Fortran error

I'm using the R leaps package to obtain a fit to some data:
(My dataframe df contains a Y variable and 41 predictor variables)
require(leaps)
N=3
regsubsets(Y ~ ., data = df, nbest=1, nvmax=N+1,force.in="X", method = 'exhaustive')-> regfit
coef(regfit,id = N)
When I run the code more than once (the first time works fine) I get the following error when I run the coef command:
Error in .Fortran("REORDR", np = as.integer(object$np), nrbar = as.integer(object$nrbar), :
"reordr" not resolved from current namespace (leaps)
Any help with why this is happening would be much appreciated.
A.
I had to build the package from source inserting the (PACKAGE = 'leaps') argument into the REORDR function in the leaps.R file. It now works fine every time.
The solution is related to:
R: error message --- package error: "functionName" not resolved from current namespace

Resources