Data Frame and Linear Regression

Asked

Viewed 244 times

4

With this data frame I need to run some regressions. I want to make regressions with terms raised to 3 and lagged.

I looked in the lm package and could not implement.

I want for example to rotate the first column in logarithm against the 2nd , 3rd and 4th column squared and the term lagged from the first column.

  AnoTrimestreLAG1 ENGTrimestralemT ENGTrimestralemLAG1 CotatoTrimestralemLAG1 CotatodTrimestralLAG12
2       2014-12-31        0.7695652           0.9367866                   -1.6                  2.56
3       2014-09-30        0.9367866           1.7134771                   -0.2                  0.04
4       2014-06-30        1.7134771           2.7852691                   -0.6                  0.36
5       2014-03-31        2.7852691           2.9320260                   -1.2                  1.44
6       2013-12-31        2.9320260           3.1011732                    2.7                  7.29
7       2013-09-30        3.1011732           2.7699729                    2.1                  4.41

How can I implement? can I do a function that does that? Any hint would be enough.

Thank you.

1 answer

6


To make the log you can put directly into the formula. Example:

# sem log
lm(mpg ~ cyl, mtcars)

# com log
lm(log(mpg) ~ cyl, mtcars)

To put the quadratic term you will use the auxiliary function I(). For more details see the question: How to include a variable high to n in regression .

Example:

lm(mpg ~ cyl + I(cyl^2), mtcars)

To put the lag is more complicated. You cannot use the function lag directly. The most appropriate would be to create manually or use own time series packages.

  • Great, Carlos. I can go from there, I just wanted a light.

  • Contributing. I can use the function "dynlm": Modelo1 <- dynlm(ENGTrimestralemT ~ L(CotatoTrimestralemLAG1 ,1) + L(ENGTrimestralemT , 1)-1, data = df) Modelo1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.