Weighted regression with 2 variables and interaction - r

I want to perform a linear regression with 2 continuous predictors (Y~X1*X2) and an interaction between them.
Moreover, i want to use weighted least square with « weights » argument. However the weight for X1 and X2 are different car i want to use their variance (var(X1) and var(X2)). I did that for each univariate regression.
I do not know how to perform that in a model including these 2 variables and their interaction term.
Coud you help me please ?

Related

How to find RERI for additive scale interaction in a cox regression model in R

I would like to find the RERI of an additive interaction for a COX regression model in R. Any idea if it's possible to do it, and if so, how to do it?
I know we can do it with stdReg package which is well explained here https://link.springer.com/article/10.1007/s10654-018-0375-y#Sec11, but the authors explain how to do it with two binary variables, and i would like to do it with 1 continous variable and 1 binary variable.

Model formula for Multilevel Model where different sets of predictors are used for random intercept and random slope

I have been searching for many web sources just to try to get any information on this issue. I am working on a two-level multilevel model.
Problem Background
In level one, I have one response variable with one predictor variable. Therefore, I have one intercept and one slope coefficient at this level.
In level two, I have one model that predicts the level-one intercept and another model that predicts the level-one slope. In my study, I will be using different predictors in both level-two models. Nearly all tutorials in the internet and the books I have read assume the same level-two predictors are being used to predict the level-one intercept and level-one slope.
Question
How should I specify my model in r? (Packages in use: lmerTest, blmer, and brms. They all use the same model formulation)
List of Variables
y - level 1 response variable
L1x - level 1 predictor variable
L2a - level 2 predictor for the level 1 intercept
L2b - level 2 predictor for the level 1 slope
g - grouping variable
What I Know
Null Model: This is simple. I think I have done it correctly.
y ~ (1|g)
Random Intercept Model: I am pretty sure this is correct too. L1x will be a fixed effect predictor and this only allows the incept to be varying across the different groups.
y ~ L1x + (1 | g)
What I Don't Know
How do I make a formula for random intercept and random slope and beyond? I know that when you have the same level-two predictors for both the intercept and the slope, it is
y ~ L1x + (L2b | g)
But to my understanding, this assumes L2b to be the level-two predictor of both Level-one intercept and slope. How do I formulate my model when the level-two predictors for level-one intercept and slope are different? I hope my question makes sense to everybody.
Note:
This is my first time posting. Please let me know what I should do to make the question clearer to you. Thank you.
I could not figure out how to use LaTex code here, so I am adding the model as images.
Level One Model
Level Two Models

How to get each independent variables R^2 in a multivariate linear regression

When you do a multivariate linear regression you get the multiple R-squared, like this:
My question is, if I can get the R-squared for each independent variable, without having to make a regression for each of the predictor variables.
For example, is it possible to get the R-squared for each of the predictor variables, next to the p value:
In regression models, individual variables do not have an R-squared. There is only ever an R-squared for a complete model. The variance explained by any single independent variable in a regression model is depending on the other independent variables.
If you need some added value of an independent variable, that is, the variance this IV explains above all others, you can compute two regression models. One with this IV and one without. The difference in R-squared is the variance this IV explains after all others have explained their share. But if you do this for all variables, the differences won't add up to the total R-squared.
Alternatively, you may use squared Beta weights to roughly estimate the effect size of a variable in a model. But this value is not directly comparable to R-squared.
This said, this question would better be posted in CrossValidated than StackOverflow.

How to rectify heteroscedasticity for multiple linear regression model

I'm fitting a multiple linear regression model with 6 predictiors (3 continuous and 3 categorical). The residuals vs. fitted plot show that there is heteroscedasticity, also it's confirmed by bptest().
summary of sales_lm
rediduals vs. fitted plot
Also I calculated the sqrt for my train data and test data, as showed below:
sqrt(mean(sales_train_lm_pred-sales_train$SALES)^2)
2 3533.665
sqrt(mean(sales_test_lm_pred-sales_test$SALES)^2)
2 3556.036
I tried to fit glm() model, but still didn't rectify heteroscedasticity.
glm.test3<-glm(SALES~.,weights=1/sales_fitted$.resid^2,family=gaussian(link="identity"), data=sales_train)
resid vs. fitted plot for glm.test3
it looks weird.
glm.test3 plot
Could you please help me what should I do next?
Thanks in advance!
That you observe heteroscedasticity for your data means that the variance is not stationary. You can try the following:
1) Apply the one-parameter Box-Cox transformation (of the which the log transform is a special case) with a suitable lambda to one or more variables in the data set. The optimal lambda can be determined by looking at its log-likelihood function. Take a look at MASS::boxcox.
2) Play with your feature set (decrease, increase, add new variables).
2) Use the weighted linear regression method.

Linear predictor from coefficients of Cox PH model

I need to calculate the linear predictor of a Cox PH model by hand.
I can get continuous and binary variables to match the output of predict.coxph (specifying 'lp') but I can't seem to figure out how to calculate it for categorical variables with more than 2 levels.
My aim is to assess calibration of a published model in my own data-I only have coefficients so need to be able to do this by hand.
This previous post describes how to calculate for continuous variables...
(Coxph predictions don't match the coefficients)
Any advice would be appreciated! Thanks

Resources