How to do ANOVA for regression models in R? - r

When I run ANOVA for the regression models which I develop, this error appears:
Error in anova.nls(model3) : nova is only defined for sequences of "nls" objects
What is the meaning of this error?
It should be mentioned that when I run summary of model, I see just parameters estimated for the model and no other statistical parameters. Does it mean that the model still needs modification and that is not the final model? please look at the instruction of my model, and the summary and ANOVA:
model3 = nls(Height ~ 1.30 + a*(I(1- exp(-b*Diameter))^c), data = dat1, start = list(a=48,b=0.012,c=0.491), algorithm="port")
summary(model3)
anova(model3)
Here are the result:
model3 = nls(Height ~ 1.30 + a*(I(1- exp(-b*Diameter))^c), data = dat1, start = list(a=48,b=0.012,c=0.491), algorithm="port")
summary(model3)
Formula: Height ~ 1.3 + a * (I(1 - exp(-b * Diameter))^c)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a 43.121923 1.653027 26.087 < 2e-16 ***
b 0.022037 0.003811 5.783 1.38e-08 ***
c 0.914263 0.116202 7.868 2.75e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 5.369 on 446 degrees of freedom
Algorithm "port", convergence message: relative convergence (4)
anova(model3)
Error in anova.nls(model3) :
anova is only defined for sequences of "nls" objects
I am a beginner in R. Is there somebody who help me?
Thank you

The error message means you need to specify a sub-model - for non-linear regression, there's no obvious choice, and so you'll need to do anova(model3, model0) where model0 corresponds to a fit of another model - probably where one or more of your parameters are held constant.

Related

How to get the overall significance from an mblogit model (mclogit package)

I am modeling a multiple choice response with the mclogit package (mblogit function) and I don't get an overall model significance in the summary(). Also running anova() for a comparison with a null-model doesn't work. Moreover, I'm not sure if the Chi-square test even applies here. I get this warning:
Warning in anova.mclogitlist(c(list(object), dotargs), dispersion = dispersion,
Results are unreliable, since deviances from quasi-likelihoods are not comparable.
Analysis of Deviance Table
Model 1: this_response ~ 1
Model 2: this_response ~ classLevel
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 7755 6530.4
2 7704 6058.6 51 471.82 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Here's my code:
m1 = mclogit::mblogit(
formula = this_response ~ classLevel,
random = ~ 1 + classLevel | question,
data = test_set_8_df)
summary(m1) # -> I get sign. for the coefficients but not for the overall model
m0 = mclogit::mblogit(
formula = this_response ~ 1,
data = test_set_8_df)
anova(m0,m1, test="Chisq") # -> I get a warning

Error in h(simpleError(msg, call)) when using emmeans in R

I am running mixed models in R using the lme4 package. I am interested in the effect of a brain stimulation intervention (2 levels: real, placebo) across three sessions (3 levels: 1, 2, 3) and two block types (2 levels: sequential, random) on reaction time (dependent variable).
> LMER_model3_SSLE_Conventional_M1 <- lmer(Stimulus.RT ~ StimCondition*SessionNrSimplified*BlockType + (1|Subject), data = Conventional_M1)
> LMER_model3_SSLE_Conventional_M1
Linear mixed model fit by REML ['lmerModLmerTest']
Formula: Stimulus.RT ~ StimCondition * SessionNrSimplified * BlockType + (1 | Subject)
Data: Conventional_M1
REML criterion at convergence: 1290940
Random effects:
Groups Name Std.Dev.
Subject (Intercept) 48.54
Residual 137.13
Number of obs: 101795, groups: Subject, 45
Fixed Effects:
(Intercept) StimConditionSham
428.885 -5.211
SessionNrSimplified2 SessionNrSimplified3
-41.386 -14.354
BlockTypeTrialList StimConditionSham:SessionNrSimplified2
-29.535 13.798
StimConditionSham:SessionNrSimplified3 StimConditionSham:BlockTypeTrialList
22.106 10.473
SessionNrSimplified2:BlockTypeTrialList SessionNrSimplified3:BlockTypeTrialList
-8.239 -39.049
StimConditionSham:SessionNrSimplified2:BlockTypeTrialList StimConditionSham:SessionNrSimplified3:BlockTypeTrialList
-18.722 -24.607
> car::Anova(LMER_model3_SSLE_Conventional_M1, type=3)
Analysis of Deviance Table (Type III Wald chisquare tests)
Response: Stimulus.RT
Chisq Df Pr(>Chisq)
(Intercept) 3131.9700 1 < 2.2e-16 ***
StimCondition 2.1334 1 0.144122
SessionNrSimplified 141.6687 2 < 2.2e-16 ***
BlockType 118.5960 1 < 2.2e-16 ***
StimCondition:SessionNrSimplified 18.6116 2 9.089e-05 ***
StimCondition:BlockType 7.4732 1 0.006262 **
SessionNrSimplified:BlockType 107.9022 2 < 2.2e-16 ***
StimCondition:SessionNrSimplified:BlockType 21.3060 2 2.363e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The model converges fine and Type III Wald Chi Square tests are no issue, using the car package.
However, recently, even when running relatively simple models (large datasets), I receive the following error when running emmeans:
> LMER_model3_SSLE_Conventional_M1_emm <- emmeans(LMER_model3_SSLE_Conventional_M1, pairwise ~ StimCondition*SessionNrSimplified*BlockType)
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'chol2inv': error in evaluating the argument 'x' in selecting a method for function 'chol': error in evaluating the argument 'x' in selecting a method for function 'forceSymmetric': Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 102
> LMER_model3_SSLE_Conventional_M1_emm
Error: object 'LMER_model3_SSLE_Conventional_M1_emm' not found
As I have not encountered this error before, I figured it may be a compatibility issue of the emmeans package with the latest version of R. I re-ran my script in different versions of R (4.1.3, 4.1.2, 4.1.1, 4.1.0), but I still get the same error.
Does anyone know how to fix this, please?

Adding robust standard errors to original panel model in R

I use a within plm model:
model <- plm(Y ~ x1 + x2 + x3, data=dataset, model="within", effect="twoways")
I detected heteroskedasticity and calculated robust standard errors with the vcovHC function from the plm package:
coeftest(model, vcov = vcovHC(model, method = "arellano"))
But unfortunately, I don't know how to "add" these robust standard errors to my original model. I do get the results with the vcovHC function:
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
x1 0.04589038 0.02465875 1.8610 0.06317 **
x2 -0.00065238 0.00027054 1.4114 0.01615 *
x3 -0.00087420 0.00043580 1.0059 0.04525 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
But it does not print the usual regression statistic like when I would run summary(model):
Total Sum of Squares:
Residual Sum of Squares:
R-Squared:
Adj. R-Squared:
F-statistic: , p-value:
So, I would like to find a way to merge the robust standard errors of the vcovHC function with my plm model.
May I suggest to have a look at the documentation: ?summary.plm.
You will find explanation as well as an example that is easy to transfer to your requirement:
summary(model, vcov = function(x) vcovHC(x, method = "arellano"))

R code to test the difference between coefficients of regressors from one panel regression

I am trying to compare two regression coefficient from the same panel regression used over two different time periods in order to confirm the statistical significance of difference. Therefore, running my panel regression first with observations over 2007-2009, I get an estimate of one coefficient I am interested in to compare with the estimate of the same coefficient obtained from the same panel model applied over the period 2010-2017.
Based on R code to test the difference between coefficients of regressors from one regression, I tried to compute a likelihood ratio test. In the linked discussion, they use a simple linear equation. If I use the same commands in R than described in the answer, I get results based on a chi-squared distribution and I don't understand if and how I can interpret that or not.
In r, I did the following:
linearHypothesis(reg.pannel.recession.fe, "Exp_Fri=0.311576")
where reg.pannel.recession.fe is the panel regression over the period 2007-2009, Exp_Fri is the coefficient of this regression I want to compare, 0.311576 is the estimated coefficient over the period 2010-2017.
I get the following results using linearHypothesis():
How can I interpret that? Should I use another function as it is plm objects?
Thank you very much for your help.
You get a F test in that example because as stated in the vignette:
The method for "lm" objects calls the default method, but it changes
the
default test to "F" [...]
You can also set the test to F, but basically linearHypothesis works whenever the standard error of the coefficient can be estimated from the variance-covariance matrix, as also said in the vignette:
The default method will work with any model
object for which the coefficient vector can be retrieved by ‘coef’
and the coefficient-covariance matrix by ‘vcov’ (otherwise the
argument ‘vcov.’ has to be set explicitly)
So using an example from the package:
library(plm)
data(Grunfeld)
wi <- plm(inv ~ value + capital,
data = Grunfeld, model = "within", effect = "twoways")
linearHypothesis(wi,"capital=0.3",test="F")
Linear hypothesis test
Hypothesis:
capital = 0.3
Model 1: restricted model
Model 2: inv ~ value + capital
Res.Df Df F Pr(>F)
1 170
2 169 1 6.4986 0.01169 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
linearHypothesis(wi,"capital=0.3")
Linear hypothesis test
Hypothesis:
capital = 0.3
Model 1: restricted model
Model 2: inv ~ value + capital
Res.Df Df Chisq Pr(>Chisq)
1 170
2 169 1 6.4986 0.0108 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
And you can also use a t.test:
tested_value = 0.3
BETA = coefficients(wi)["capital"]
SE = coefficients(summary(wi))["capital",2]
tstat = (BETA- tested_value)/SE
pvalue = as.numeric(2*pt(-tstat,wi$df.residual))
pvalue
[1] 0.01168515

why step() returns weird results from backward elimination for full model using lmerTest

I am confused that why the results from processing step(model) in lmerTest are abnormal.
m0 <- lmer(seed ~ connection*age + (1|unit), data = test)
step(m0)
note: Both "connection" and "age" have been set as.factor()
Random effects:
Chi.sq Chi.DF elim.num p.value
unit 0.25 1 1 0.6194
Fixed effects:
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
connection 1 0.01746 0.017457 1.5214 0.22142
age 1 0.07664 0.076643 6.6794 0.01178 *
connection:age 1 0.04397 0.043967 3.8317 0.05417 .
Residuals 72 0.82617 0.011475
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Least squares means:
Estimate Standard Error DF t-value Lower CI Upper CI p-value
Final model:
Call:
lm(formula = fo, data = mm, contrasts = l.lmerTest.private.contrast)
Coefficients:
(Intercept) connectionD ageB connectionD:ageB
-0.84868 -0.07852 0.01281 0.09634
Why it does not show me the Final model?
The thing here is that random effect was eliminated as being NS according to the LR test. Then the anova method for the fixed effects model, the "lm" object was applied and no elimination of NS fixed effects was done. You are right, that the output is different from "lmer" objects and there are no (differences of ) least squares means. If you want to get the latter you may try the lsmeans package. For the backward elimination of NS effect of the final model you may use stats::step function.

Resources