This is my data.frame:
data <- matrix(rnorm(50*5),nrow=50)
m <- data.frame(data )
m
X1 X2 X3 X4
1 -0.47903358 1.92799699 -0.584364168 -1.475276350
2 0.05464517 1.90064618 1.721449935 1.215188405
3 2.23782855 0.16839367 -0.074212064 -0.719745849
4 0.44476924 0.37247676 0.300423948 1.722222410
5 -0.66889333 0.26094142 -0.001254037 0.617630295
6 -1.17663634 -1.93020031 -2.813433555 0.350943310
7 2.16609882 -0.06594556 0.133278642 1.693078802
8 -0.16090564 -1.14454452 -1.340407149 0.574943347
9 0.46691283 0.98394614 -0.277273498 0.030499576
10 1.19049154 1.03160103 0.626364442 0.429348535
11 0.72593476 0.89130574 -0.599052058 2.012316875
12 -0.04748923 -1.38044706 0.058364936 0.825752213
13 -0.90407306 0.14790680 0.002608154 -0.991979288
14 -1.50113350 0.78807179 -2.371160466 0.559174890
15 0.04075635 -1.14486991 1.585769771 -0.495937216
16 1.29724449 0.47729721 -0.871033202 1.524746619
17 1.12384656 -0.07589516 -0.463837527 0.206108262
18 0.65092671 0.29660588 -1.096981115 -0.326524160
19 -0.54953490 -0.06792527 -0.543922865 0.211205138
20 0.19187346 -0.65190412 -1.674653668 0.712704645
21 0.97108788 0.91087005 0.182424518 -0.296413534
22 1.64975949 0.22140448 -0.075355571 -0.472299544
23 1.45613296 -1.29101443 1.231839302 0.774475499
24 -0.15324579 -1.31596417 -0.343467527 0.087122108
25 0.20305822 -0.96867715 -0.311745324 -1.483964874
26 -2.22916775 1.46982603 -0.254705650 -0.003573105
27 0.26135095 -0.44053524 0.208284330 0.459095264
28 0.55020951 -0.37742141 0.346186077 0.825811292
29 -0.78695097 -0.21014858 0.003055816 2.142121347
30 -0.33506969 0.31572240 -0.139970552 1.344697176
31 -0.94164226 -0.88366990 -0.906886069 0.774590884
32 3.23764133 -0.47556802 0.166917302 0.959307700
33 -0.64114926 0.16089400 1.942563410 0.142073261
34 0.03848502 -0.83066179 0.934403254 1.730448144
35 -0.34621698 0.65682342 1.601365990 0.160012282
36 0.62848314 0.33126945 0.188823549 2.380319403
37 1.02807329 1.07299920 0.398083643 -0.208241920
38 -0.50616984 1.68998408 0.427342040 -0.345264684
39 1.31721397 -1.17111247 1.754036030 0.755414080
40 1.23714888 -0.12253370 0.887065669 0.001770354
41 0.59541135 0.66438057 0.880643498 1.441513269
42 0.41100680 0.52757460 1.219644246 1.450262235
43 0.08984815 1.44830794 0.113193523 -0.620546777
44 -1.51411951 -0.17346499 -0.698924584 -0.767921061
45 -1.03652530 1.27296507 1.740954529 -0.467568299
46 1.14533381 -0.67304957 -0.384012335 0.637582075
47 0.37996214 0.53566774 -1.413930797 1.222728459
48 -0.53705734 0.77591264 0.375236588 -1.430924067
49 0.64051195 -0.22562951 -1.667050192 0.345009059
50 -1.19521122 1.18436746 -0.905004199 -0.005221906
Well, I want now estimate 3 different models.
X1 = X2 + X3 + X4
X1 = X2^2 + X3(Lag1) + X4^2
ln(X1)=X2^2 + X3(Lag1) + X4^2
As im a begginer in R im not able to implement a function to do this. I'm reading the lm function and still can not implement these models. The way I could implement was very rudimentary, ie, I was riding a data.frame for each model.
Thanks
To use lm() you need at least two things. You need a formula and you need data to apply that formula to. In your case, here's how you would code to run a very simple, basic, linear regression on your data. Again, you need an "X3_Lag1" variable (literally a column called that which has the lag applied) in your data to regress on lagged data (make a new column with X3 pushed down a row) and then cut the data for that missing value in row one.
# Getting the first model.
formula1 <- "X1 ~ X2 + X3 + X4"
lin1 <- lm(formula1, data)
summary(lin1) # This will give you the coefficients for each value.
# Getting the second model.
formula2 <- "X1 ~ X2^2 + X3_Lag1 + X4^2"
lin2 <- lm(formula2, data)
summary(lin2)
# Getting the third model.
formula3 <- "log(X1) ~ X2^2 + X3_Lag1 + X4^2"
lin3 <- lm(formula3, data)
summary(lin3)
WARNING: I believe you will get an error on the third formula because you're taking the natural log of a negative number, which is not possible. Please take care of that being the case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.