这是我的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
好吧,我现在想要估计3种不同的模型。
X1 = X2 + X3 + X4
X1 = X2^2 + X3(Lag1) + X4^2
ln(X1)=X2^2 + X3(Lag1) + X4^2
因为我是R的初学者,我无法实现这样做的功能。 我正在阅读lm函数,但仍无法实现这些模型。我可以实现的方式非常简陋,即我为每个模型都使用了data.frame。
由于
答案 0 :(得分:0)
要使用lm(),至少需要两件事。您需要一个公式,并且您需要数据来应用该公式。在您的情况下,您可以使用以下代码对数据进行非常简单的基本线性回归。同样,你需要一个&#34; X3_Lag1&#34;数据中的变量(实际上是一个名为“应用了延迟的列”),用于对滞后数据进行回归(使用X3向下推一行新列),然后在第一行中删除该缺失值的数据。
# 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)
警告:我相信你会在第三个公式上出错,因为你正在采用负数的自然对数,这是不可能的。请注意这种情况。