change null hypothesis in lmtest in R - r

I have a linear model generated using lm. I use the coeftest function in the package lmtest go test a hypothesis with my desired vcov from the sandwich package. The default null hypothesis is beta = 0. What if I want to test beta = 1, for example. I know I can simply take the estimated coefficient, subtract 1 and divide by the provided standard error to get the t-stat for my hypothesis. However, there must be functionality for this already in R. What is the right way to do this?
MWE:
require(lmtest)
require(sandwich)
set.seed(123)
x = 1:10
y = x + rnorm(10)
mdl = lm(y ~ x)
z = coeftest(mdl, df=Inf, vcov=NeweyWest)
b = z[2,1]
se = z[2,2]
mytstat = (b-1)/se
print(mytstat)

the formally correct way to do this:
require(multcomp)
zed = glht(model=mdl, linfct=matrix(c(0,1), nrow=1, ncol=2), rhs=1, alternative="two.sided", vcov.=NeweyWest)
summary(zed)

Use an offset of -1*x
mdl<-lm(y~x)
mdl2 <- lm(y ~ x-offset(x) )
> mdl
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
0.5255 0.9180
> mdl2
Call:
lm(formula = y ~ x - offset(x))
Coefficients:
(Intercept) x
0.52547 -0.08197
You can look at summary(mdl2) to see the p-value (and it is the same as in mdl.

As far as I know, there is no default function to test the model coefficients against arbitrary value (1 in your case). There is the offset trick presented in the other answer, but it's not that straightforward (and always be careful with such model modifications). So, your expression (b-1)/se is actually a good way to do it.
I have two notes on your code:
You can use summary(mdl) to get the t-test for 0.
You are using lmtest with covariance structure (which will change the t-test values), but your original lm model doesn't have it. Perhaps this could be a problem? Perhaps you should use glm and specify the correlation structure from the start.

Related

r loess: coefficients of global "parametric" terms

Is there a way how I can extract coefficients of globally fitted terms in local regression modeling?
Maybe I do misunderstand the role of globally fitted terms in the function loess, but what I would like to have is the following:
# baseline:
x <- sin(seq(0.2,0.6,length.out=100)*pi)
# noise:
x_noise <- rnorm(length(x),0,0.1)
# known structure:
x_1 <- sin(seq(5,20,length.out=100))
# signal:
y <- x + x_1*0.25 + x_noise
# fit loess model:
x_seq <- seq_along(x)
mod <- loess(y ~ x_seq + x_1,parametric="x_1")
The fit is done perfectly, however, how can I extract the estimated value of the globally fitted term x_1 (i.e. some value near 0.25 for the example above)?
Finally, I found a solution to my problem using the function gam from the package gam:
require(gam)
mod2 <- gam(y ~ lo(x_seq,span=0.75,degree=2) + x_1)
However, the fits from the two models are not exactly the same (which might be due to different control settings?)...

Apply Durbin Watson test on Prais Winsten Model

I am having trouble running a Durbin Watson test on the prais winsten model I generated.
value3<-prais.winsten(value1$model)
dwtest(value3)
I receive this error:
Error in terms.default(formula) : no terms component nor attribute
Without any reasonable reproducible example is hard to pinpoint where is the issue, this is a kind of default way to go:
# Calculate Durbin-Watson
ols <- lm(y ~ x1 + x2)
dwtest(y ~ x1 + x2)
prais.winsten.lm(ols)
# Calcuate Autocorrelation and Partial Autocorrelation Coefficients
acf(resid(ols),lag.max = 5,plot = FALSE)
pacf(resid(ols),lag.max = 5,plot = FALSE)
You need to call dtest with the lm function on the list that prais.winsten returns
From your example it would be:
dwtest(lm(value3[[1]]))
When you display value3 you should see somthing like this at the top
[[1]]
Call:
lm(formula = fo)
This is what I am referring to.

`rms::ols()`: how to fit a model without intercept

I'd like to use the ols() (ordinary least squares) function from the rms package to do a multivariate linear regression, but I would not like it to calculate the intercept. Using lm() the syntax would be like:
model <- lm(formula = z ~ 0 + x + y, data = myData)
where the 0 stops it from calculating an intercept, and only two coefficients are returned, on for x and the other for y. How do I do this when using ols()?
Trying
model <- ols(formula = z ~ 0 + x + y, data = myData)
did not work, it still returns an intercept and a coefficient each for x and y.
Here is a link to a csv file
It has five columns. For this example, can only use the first three columns:
model <- ols(formula = CorrEn ~ intEn_anti_ncp + intEn_par_ncp, data = ccd)
Thanks!
rms::ols uses rms:::Design instead of model.frame.default. Design is called with the default of intercept = 1, so there is no (obvious) way to specify that there is no intercept. I assume there is a good reason for this, but you can try changing ols using trace.

How to set the Coefficient Value in Regression; R

I'm looking for a way to specify the value of a predictor variable. When I run a glm with my current data, the coefficient for one of my variables is close to one. I'd like to set it at .8.
I know this will give me a lower R^2 value, but I know a priori that the predictive power of the model will be greater.
The weights component of glm looks promising, but I haven't figured it out yet.
Any help would be greatly appreciated.
I believe you are looking for the offset argument in glm. So for example, you might do something like this:
glm(y ~ x1, offset = x2,...)
where in this case the coefficient of x2 would be set at 1. In your case, you may perhaps want to multiply that column by 0.8?
To expand, here is what ?glm says about the offset argument:
this can be used to specify an a priori known component to be included
in the linear predictor during fitting. This should be NULL or a
numeric vector of length equal to the number of cases. One or more
offset terms can be included in the formula instead or as well, and if
more than one is specified their sum is used. See model.offset.
So you can add offsets in the model formula itself using the offset() function, as well. Here is a simple example illustrating its use:
set.seed(123)
d <- data.frame(y = factor(sample(0:1,size = 100,replace = TRUE)),x1 = runif(100),x2 = runif(100))
glm1 <- glm(y~x1+x2,data = d,family = binomial)
coef(glm1)
(Intercept) x1 x2
0.4307718 -0.4128541 -0.6994810
glm2 <- glm(y~x1,data = d,offset = x2,family = binomial)
coef(glm2)
(Intercept) x1
-0.4963699 -0.2185571
glm3 <- glm(y~x1+offset(x2),data = d,family = binomial)
coef(glm3)
(Intercept) x1
-0.4963699 -0.2185571
Note that the last two have the same coefficients.

MCMClogit confusion

Could anybody explain to me why
simulatedCase <- rbinom(100,1,0.5)
simDf <- data.frame(CASE = simulatedCase)
posterior_m0 <<- MCMClogit(CASE ~ 1, data = simDf, b0 = 0, B0 = 1)
always results in a MCMC acceptance ratio of 0? Any explanation would be greatly appreciated!
I think your problem is the model formula, since logistic regression models have no error term. Thus you model CASE ~ 1 should be replaced by something like CASE ~ x (the predictor variable x is mandatory). Here is your example, modified:
CASE <- rbinom(100,1,0.5)
x <- 1:100
posterior_m0 <- MCMClogit (CASE ~ x, b0 = 0, B0 = 1)
classic_m0 <- glm (CASE ~ x, family=binomial(link="logit"), na.action=na.pass)
So I think your problem is not related to the MCMCpack library (disclaimer: I have never used this package).
For anyone stumbling into this same problem :
It seems that the MCMClogit function cannot handle anything but B0=0 if your model only has an intercept.
If you add a covariate, then you can specify a precision just fine.
I would consider other packages (such as arm or rjags) if you really want to sample from this model. For a list of options available for Bayesian regression, see http://cran.r-project.org/web/views/Bayesian.html

Resources