Continuous independent variable using Lm function in R [duplicate] - r

This question already has an answer here:
Datatype for linear model in R
(1 answer)
Closed 5 years ago.
I ran a regression in data set cwm. Intention:
Test the correlation between cm14 (continuous) and medran (continuous)
Control for type, disint, leverage, and end, which affect cm14
ranking <- lm(cm14 ~ medran + cm10a + type + disint + leverage + end, data=cwm)
summary(ranking)
The summary results gave an independent regression figure for every category in medran.
How can I obtain a figure for the overall impact of medran on cm14?

If any of your variables is categorical, you can use anova(ranking) to do this.

Related

R Regression analysis testing effect of independent variables in year(i) on dependent variable in year(i+1)

I have a question regarding a multiple linear regression model. I want to test the effect of my independent variables in year(i) on my dependent variable in the subsequent year (i+1):
**y(year i+1) = b0 + b1x1(year i) + b2x2(year i) + b3*x3(year i) + ei
How do I test this in R?**
My data is in yearly data format, covering 20 years with roughly 50,000 observations (Year - Dependent Variable - Independent variables 1-3)
Thank you so much for your help!

Fitting random factors for a linear model using lme4

I have 4 random factors and I want to provide its linear model using lme4. But struggled to fit the model.
Assuming A is nested within B (2 levels), which in turn nested within each of xx preceptors (P). All responded to xx Ms (M).
I want to fit my model to get variances for each factor and their interactions.
I have used the following codes to fit the model, but I was unsuccessful.
lme4::lmer(value ~ A +
(1 + A|B) +
(1 + P|A),
(1+ P|M),
data = myData, na.action = na.exclude)
I also read interesting materials here, but Still, I struggle to fit the model. Any help?
At a guess, if the nesting structure is ( P (teachers) / B (occasions) / A (participants) ), meaning that the occasions for one teacher are assumed to be completely independent of the occasions for any other teacher, and that participants in turn are never shared across occasions or teachers, but questions (M) are shared across all teachers and occasions and participants:
value ~ 1 + (1| P / B / A) + (1|M)
Some potential issues:
as you hint in the comments, it may not be practical to fit random effects for factors with small numbers of levels (say, < 5); this is likely to lead to the dreaded "singular model" message (see the GLMM FAQ for more detail).
if all of the questions (M) are answered by every participant, then in principle it's possible to fit a model that takes account of the among-question correlation within participants: the maximal model would be ~ 1 + (M | P / B / A) (which would look for among-question correlations at the level of teacher, occasion within teacher, and participant within occasion within teacher). However, this is very unlikely to work in practice (especially if each participant answers each question only once, in which case the teacher:occasion:participant:question variance will be confounded with the residual variance in a linear model). In this case, you will get an error about "probably unidentifiable": see e.g. this question for more explanation/detail.

Defining a constant as a coefficient from an r regression [duplicate]

This question already has answers here:
Extract regression coefficient values
(4 answers)
Is there a reason to prefer extractor functions to accessing attributes with $?
(2 answers)
Closed 3 years ago.
I am trying define a constant as a coefficient from an r regression. I.e. trying to define elasticity as the number -1.64431 that you can see in the photo.
reg <- lm(ln_q ~ ln_price, data=df)
elasticity <- reg[["coefficients","ln_price"]]
print(elasticity)
I get the error:
Error in reg[["coefficients", "ln_price"]] :
incorrect number of subscripts
Any help much appreciated! :)
elasticity <- reg$coefficients[names(reg$coefficients)=="ln_price"]

Is there any way to retroactively store model output in a variable? [duplicate]

This question already has an answer here:
How to assign the result of the previous expression to a variable?
(1 answer)
Closed 3 years ago.
Let's say I have just run a large linear regression that has taken a considerable amount of time to run. However, I forgot to assign the model to a variable prior to running it, i.e.
lm(outcome ~ iv1 + iv2)
Instead of
fit <- lm(outcome ~ iv1 + iv2)
Is there a way to retroactively assign the final output to a variable after its been run?
If you have not run anything else in the interim, the last object created is stored in .Last.value.
fit <- .Last.value

Why can't I add a one level factor in my lm? [duplicate]

This question already has an answer here:
How to do a GLM when "contrasts can be applied only to factors with 2 or more levels"?
(1 answer)
Closed 4 years ago.
I am performing a regression and I want to add a year as factor, but my year is just one year (2010) and when I run the equation I get an error:
contrasts<- (tmp, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
My equation:
Lanu <- lm(YFT ~ PR1*factor(DN1)*factor(NTM1)*factor(AÑO1))
All my factors have the same length, YFT is density and PR1 is depth, factor DN1 is two types of net, and NTM1 is three locations. I want to know if there is interaction in my factors.
Since you only have 1 year, year would just be a constant. There would be no identifying variation, which is what #MrFlick is implying in his comment. That's why you get an error if you try a contrast or if you were to try putting it in the lm equation.
Realistically, you should not include year since you do not have any variation in years.
Technically, you could include it in your regression if you were to omit the default constant (commonly called "Beta naught") which is calculated as the mean response when all predictor values are zero. That would be the same as artifically setting your y-intercept to 2010.
It's hard to think of a scenario where that would be advantageous and even then you could not have interaction terms with a constant.
Here are your options:
Forget about including year
Get more years
Use 2010 as your y-intercept: Lanu <- lm(YFT ~ 0 + PR1*factor(DN1)*factor(NTM1)*factor(AÑO1), offset=rep(2010, length(YFT))
Use months, quarters, or some subdivision of the year instead of year
Note that adding 0 + or - 1 to the equation removes the normal intercept and offset creates the artificial year intercept.

Resources