lm()动态函数-R

时间:2019-01-22 13:49:43

标签: r lm

假设我有这个数据框:

N <- 50
df  <- data.frame(
  LA1 = sample(1:10, size = N, replace = TRUE), 
  LA2 = sample(1:10, size = N, replace = TRUE), 
  LA3 = sample(1:10, size = N, replace = TRUE), 
  LA4 = sample(1:10, size = N, replace = TRUE), 
  LA5 = sample(1:10, size = N, replace = TRUE),
  LA6 = sample(1:10, size = N, replace = TRUE),
  LA7 = sample(1:10, size = N, replace = TRUE),
  LA8 = sample(1:10, size = N, replace = TRUE),
  LAY = sample(1:10, size = N, replace = TRUE),

  UF1 = sample(1:10, size = N, replace = TRUE), 
  UF2 = sample(1:10, size = N, replace = TRUE), 
  UF3 = sample(1:10, size = N, replace = TRUE), 
  UF4 = sample(1:10, size = N, replace = TRUE), 
  UF5 = sample(1:10, size = N, replace = TRUE),
  UF6 = sample(1:10, size = N, replace = TRUE),
  UFY = sample(1:10, size = N, replace = TRUE),

  EK1 = sample(1:10, size = N, replace = TRUE), 
  EK2 = sample(1:10, size = N, replace = TRUE), 
  EK3 = sample(1:10, size = N, replace = TRUE), 
  EK4 = sample(1:10, size = N, replace = TRUE), 
  EK5 = sample(1:10, size = N, replace = TRUE),
  EK6 = sample(1:10, size = N, replace = TRUE), 
  EK7 = sample(1:10, size = N, replace = TRUE), 
  EK8 = sample(1:10, size = N, replace = TRUE),
  EK9 = sample(1:10, size = N, replace = TRUE), 
  EK10 = sample(1:10, size = N, replace = TRUE), 
  EK11 = sample(1:10, size = N, replace = TRUE),
  EK12 = sample(1:10, size = N, replace = TRUE),
  EKY = sample(1:10, size = N, replace = TRUE),

  Z1 = sample(1:10, size = N, replace = TRUE), 
  Z2 = sample(1:10, size = N, replace = TRUE),
  Z3 = sample(1:10, size = N, replace = TRUE)
  )

我要在哪里计算此模型:

m1=lm(formula = LAY ~ LA1+LA2+LA3+LA4+LA5+LA6+LA7+LA8, data = df)
m11=step(m1,direction="both")

m2=lm(formula = UFY ~  UF1+UF2+UF3+UF4+UF5+UF6,data = df)
m22=step(m2,direction="both")

m3=lm(formula = EKY ~  EK1+EK2+EK3+EK4+EK5+EK6+EK7+EK8+EK9+EK10+EK11+EK12, data = df)
m33=step(m3,direction="both")

m8=lm(formula = Z1 ~ LAY+UFY+EKY, data = df)
m88=step(m8,direction="both")

m9=lm(formula = Z2 ~ LAY+UFY+EKY, data = df)
m99=step(m9,direction="both")

m10=lm(formula = Z3 ~ LAY+UFY+EKY, data = df)
m100=step(m10,direction="both")

如您所见,如果数据库的维数增加(增加LAUFEK自变量的数量),我将不得不手动修改输入模型)。所以,我正在寻找一种方法:

  • 为给定类别(LAUFEK)提供一定数量的自变量(可能为5、10、30或更多),输入模型会自动更改。

即使我发现了用于计算模型的不同语法(例如X*Z = [(X+Z)^3]),也找不到使这种计算更具动态性的方法。

注意事项:

  • 自变量(LAUFEK)的数量可以更改。
  • 因变量(LAYUFYEKY)的数量永不变。
  • 从该模型的输出中提取系数矢量(以防万一)。

0 个答案:

没有答案