在那里 R中是否有一种简单(或不是那么简单)的方法来创建 数据集中所有变量的列表?
我想遍历遍历变量的回归到另一个变量,以便我可以在关于我的独立变量的假设下预测每个变量的值。
在Stata中,这看起来像
foreach v of var a b c {
qui regress `v' i
di "`v' {col 2O}" _b[`i'] * 42
}
42 is an arbitrary number
例如,如果我有三个变量a,b和c,而另一个独立变量 变量i,我想对a i进行回归,对b i进行回归,然后对c i进行回归,之后 每个回归采用系数,并将其乘以i的值来预测 结果。
问题是我有未知数量的变量,并且我想 希望能够获取完整列表并遍历整个列表。
我的数据结构
structure(list(serial = c(19160206, 19160202, 19160203, 32051017,
32051017), strata = structure(c(347, 347, 347, 244, 244), label = "Strata", class = c("labelled",
"numeric")), psu = structure(c(553, 553, 553, 822, 822), label = "Primary sampling unit", class = c("labelled",
"numeric")), pnum = c(1, 3, 2, 5, 6), DMFlag = structure(c(1,
4, 3, 3, 3), label = "Final case status flag (IND)", class = c("labelled",
"numeric")), HhOut = structure(c(110, 210, 110, 110, 110), label = "Final outcome - household", class = c("labelled",
"numeric")), IndOut = structure(c(11, 43, 11, 11, 11), label = "Final outcome - individual", class = c("labelled",
"numeric")), ind_wt = c(0.783649023617231, NA, 0.677951319403552,
NA, NA), IMonth = structure(c(2, 4, 2, 10, 10), label = "Interview month", class = c("labelled",
"numeric")), IYear = structure(c(2015, 2015, 2015, 2015, 2015
), label = "Interview Year", class = c("labelled", "numeric")),
DMSex = structure(c(2, 1, 1, NA, NA), label = "Gender from household grid", class = c("labelled",
"numeric")), DM014 = structure(c(0, 1, 2, NA, NA), label = "Number of children aged 0-14", class = c("labelled",
"numeric")), DM016 = structure(c(0, 1, 2, NA, NA), label = "Number of children aged 0-16", class = c("labelled",
"numeric")), DM510 = structure(c(0, 0, 1, NA, NA), label = "Number of children aged 5-10", class = c("labelled",
"numeric")), DM1115 = structure(c(0, 1, 1, NA, NA), label = "Number of children aged 11-15", class = c("labelled",
"numeric")), DM1619 = structure(c(0, 0, 0, NA, NA), label = "Number of children aged 16-19", class = c("labelled",
"numeric")), WorkSta = structure(c(4, -1, -1, NA, NA), label = "Economic activity status", class = c("labelled",
"numeric")), act1_1 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 04:00-04:10", class = c("labelled",
"numeric")), act1_2 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 04:10-04:20", class = c("labelled",
"numeric")), act1_3 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 04:20-04:30", class = c("labelled",
"numeric")), act1_4 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 04:30-04:40", class = c("labelled",
"numeric")), act1_5 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 04:40-04:50", class = c("labelled",
"numeric")), act1_6 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 04:50-05:00", class = c("labelled",
"numeric")), act1_7 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 05:00-05:10", class = c("labelled",
"numeric")), act1_8 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 05:10-05:20", class = c("labelled",
"numeric")), act1_9 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 05:20-05:30", class = c("labelled",
"numeric")), act1_10 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 05:30-05:40", class = c("labelled",
"numeric")), act1_11 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 05:40-05:50", class = c("labelled",
"numeric")), act1_12 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 05:50-06:00", class = c("labelled",
"numeric")), act1_13 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 06:00-06:10", class = c("labelled",
"numeric")), act1_14 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 06:10-06:20", class = c("labelled",
"numeric")), act1_15 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 06:20-06:30", class = c("labelled",
"numeric")), act1_16 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 06:30-06:40", class = c("labelled",
"numeric")), act1_17 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 06:40-06:50", class = c("labelled",
"numeric")), act1_18 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 06:50-07:00", class = c("labelled",
"numeric")), act1_19 = structure(c(110, 110, 110, 111, 110
), label = "Primary activity: 07:00-07:10", class = c("labelled",
"numeric")), act1_20 = structure(c(110, 110, 110, 111, 110
), label = "Primary activity: 07:10-07:20", class = c("labelled",
"numeric")), act1_21 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 07:20-07:30", class = c("labelled",
"numeric")), act1_22 = structure(c(110, 110, 110, 111, 110
), label = "Primary activity: 07:30-07:40", class = c("labelled",
"numeric")), act1_23 = structure(c(110, 110, 110, 111, 110
), label = "Primary activity: 07:40-07:50", class = c("labelled",
"numeric")), act1_24 = structure(c(110, 110, 110, 310, 110
), label = "Primary activity: 07:50-08:00", class = c("labelled",
"numeric")), act1_25 = structure(c(110, 110, 110, 3110, 111
), label = "Primary activity: 08:00-08:10", class = c("labelled",
"numeric")), act1_26 = structure(c(110, 110, 110, 310, 210
), label = "Primary activity: 08:10-08:20", class = c("labelled",
"numeric")), act1_27 = structure(c(110, 110, 110, 310, 3130
), label = "Primary activity: 08:20-08:30", class = c("labelled",
"numeric")), act1_28 = structure(c(310, 110, 110, 310, 310
), label = "Primary activity: 08:30-08:40", class = c("labelled",
"numeric")), act1_29 = structure(c(310, 110, 110, 9610, 9610
), label = "Primary activity: 08:40-08:50", class = c("labelled",
"numeric")), act1_30 = structure(c(7330, 110, 110, 6190,
6190), label = "Primary activity: 08:50-09:00", class = c("labelled",
"numeric")), act1_31 = structure(c(8210, 110, 110, 6190,
6190), label = "Primary activity: 09:00-09:10", class = c("labelled",
"numeric")), act1_32 = structure(c(8210, 110, 110, 6190,
6190), label = "Primary activity: 09:10-09:20", class = c("labelled",
"numeric")), act1_33 = structure(c(8210, 310, 110, 6190,
6190), label = "Primary activity: 09:20-09:30", class = c("labelled",
"numeric")), act1_34 = structure(c(8210, 310, 310, 6190,
6190), label = "Primary activity: 09:30-09:40", class = c("labelled",
"numeric")), act1_35 = structure(c(8210, 5110, 210, 6190,
6190), label = "Primary activity: 09:40-09:50", class = c("labelled",
"numeric")), act1_36 = structure(c(8210, 5110, 310, 6190,
6190), label = "Primary activity: 09:50-10:00", class = c("labelled",
"numeric")), act1_37 = structure(c(8210, 5110, 7330, 6190,
6190), label = "Primary activity: 10:00-10:10", class = c("labelled",
"numeric")), act1_38 = structure(c(8210, 9510, 7330, 6190,
6190), label = "Primary activity: 10:10-10:20", class = c("labelled",
"numeric")), act1_39 = structure(c(8210, 9510, 7330, 6190,
6190), label = "Primary activity: 10:20-10:30", class = c("labelled",
"numeric")), act1_40 = structure(c(8210, 3611, 7330, 6190,
6190), label = "Primary activity: 10:30-10:40", class = c("labelled",
"numeric")), act1_41 = structure(c(8210, 210, 7330, 9610,
6190), label = "Primary activity: 10:40-10:50", class = c("labelled",
"numeric")), act1_42 = structure(c(8210, 210, 7330, 6190,
6190), label = "Primary activity: 10:50-11:00", class = c("labelled",
"numeric")), act1_43 = structure(c(8210, 9510, 7330, 6190,
9610), label = "Primary activity: 11:00-11:10", class = c("labelled",
"numeric")), act1_44 = structure(c(8210, 9510, 7330, 6190,
6190), label = "Primary activity: 11:10-11:20", class = c("labelled",
"numeric")), act1_45 = structure(c(8210, 3290, 8210, 6190,
6190), label = "Primary activity: 11:20-11:30", class = c("labelled",
"numeric")), act1_46 = structure(c(3240, 3210, 8210, 6190,
6190), label = "Primary activity: 11:30-11:40", class = c("labelled",
"numeric")), act1_47 = structure(c(3240, 3210, 8210, 6190,
6190), label = "Primary activity: 11:40-11:50", class = c("labelled",
"numeric")), act1_48 = structure(c(3210, 3210, 8210, 6190,
6150), label = "Primary activity: 11:50-12:00", class = c("labelled",
"numeric")), act1_49 = structure(c(3210, 3210, 8210, 6190,
6150), label = "Primary activity: 12:00-12:10", class = c("labelled",
"numeric")), act1_50 = structure(c(8210, 3210, 9370, 5310,
6150), label = "Primary activity: 12:10-12:20", class = c("labelled",
"numeric")), act1_51 = structure(c(8210, 3210, 9370, 5310,
6150), label = "Primary activity: 12:20-12:30", class = c("labelled",
"numeric")), act1_52 = structure(c(8210, 5140, 9370, 5310,
9610), label = "Primary activity: 12:30-12:40", class = c("labelled",
"numeric")), act1_53 = structure(c(8210, 3210, 9370, 5310,
3290), label = "Primary activity: 12:40-12:50", class = c("labelled",
"numeric")), act1_54 = structure(c(8210, 3210, 9370, 5310,
5140), label = "Primary activity: 12:50-13:00", class = c("labelled",
"numeric")), act1_55 = structure(c(8210, 3210, 5190, 5310,
310), label = "Primary activity: 13:00-13:10", class = c("labelled",
"numeric")), act1_56 = structure(c(8210, 3210, 3630, 5310,
310), label = "Primary activity: 13:10-13:20", class = c("labelled",
"numeric")), act1_57 = structure(c(8210, 5120, 3630, 5310,
6311), label = "Primary activity: 13:20-13:30", class = c("labelled",
"numeric")), act1_58 = structure(c(8210, 9500, 3630, 6150,
5120), label = "Primary activity: 13:30-13:40", class = c("labelled",
"numeric")), act1_59 = structure(c(8210, 3290, 5190, 6150,
9510), label = "Primary activity: 13:40-13:50", class = c("labelled",
"numeric")), act1_60 = structure(c(8210, 210, 6110, 6150,
5130), label = "Primary activity: 13:50-14:00", class = c("labelled",
"numeric")), act1_61 = structure(c(8210, 210, 6110, 6150,
6190), label = "Primary activity: 14:00-14:10", class = c("labelled",
"numeric")), act1_62 = structure(c(8210, 5120, 6110, 9610,
6190), label = "Primary activity: 14:10-14:20", class = c("labelled",
"numeric")), act1_63 = structure(c(8210, 5120, 6110, 3110,
6190), label = "Primary activity: 14:20-14:30", class = c("labelled",
"numeric")), act1_64 = structure(c(9500, 5120, 6110, 8210,
5310), label = "Primary activity: 14:30-14:40", class = c("labelled",
"numeric")), act1_65 = structure(c(9500, 5120, 6110, 310,
5310), label = "Primary activity: 14:40-14:50", class = c("labelled",
"numeric")), act1_66 = structure(c(9990, 9360, 6110, 310,
5310), label = "Primary activity: 14:50-15:00", class = c("labelled",
"numeric")), act1_67 = structure(c(8210, 9360, 6110, 210,
6190), label = "Primary activity: 15:00-15:10", class = c("labelled",
"numeric")), act1_68 = structure(c(8210, 9360, 6110, 9610,
6190), label = "Primary activity: 15:10-15:20", class = c("labelled",
"numeric")), act1_69 = structure(c(8210, 3610, 6110, 6171,
6190), label = "Primary activity: 15:20-15:30", class = c("labelled",
"numeric")), act1_70 = structure(c(8210, 3610, 6110, 6171,
210), label = "Primary activity: 15:30-15:40", class = c("labelled",
"numeric")), act1_71 = structure(c(8210, 3610, 6110, 6171,
210), label = "Primary activity: 15:40-15:50", class = c("labelled",
"numeric")), act1_72 = structure(c(8210, 3610, 6110, 6171,
6190), label = "Primary activity: 15:50-16:00", class = c("labelled",
"numeric")), act1_73 = structure(c(7330, 3610, 6110, 6171,
6190), label = "Primary activity: 16:00-16:10", class = c("labelled",
"numeric")), act1_74 = structure(c(7330, 3610, 6110, 6171,
6190), label = "Primary activity: 16:10-16:20", class = c("labelled",
"numeric")), act1_75 = structure(c(7330, 3610, 6110, 6171,
5310), label = "Primary activity: 16:20-16:30", class = c("labelled",
"numeric")), act1_76 = structure(c(7330, 3610, 6110, 310,
9510), label = "Primary activity: 16:30-16:40", class = c("labelled",
"numeric")), act1_77 = structure(c(7330, 3610, 6110, 310,
9510), label = "Primary activity: 16:40-16:50", class = c("labelled",
"numeric")), act1_78 = structure(c(8210, 3610, 9500, 310,
9510), label = "Primary activity: 16:50-17:00", class = c("labelled",
"numeric")), act1_79 = structure(c(8210, 3610, 9500, 310,
7241), label = "Primary activity: 17:00-17:10", class = c("labelled",
"numeric")), act1_80 = structure(c(8210, 3610, 3611, 3611,
7241), label = "Primary activity: 17:10-17:20", class = c("labelled",
"numeric")), act1_81 = structure(c(210, 9360, 5310, 9500,
8210), label = "Primary activity: 17:20-17:30", class = c("labelled",
"numeric")), act1_82 = structure(c(210, 9360, 210, 4230,
8210), label = "Primary activity: 17:30-17:40", class = c("labelled",
"numeric")), act1_83 = structure(c(210, 9360, 210, 4230,
8210), label = "Primary activity: 17:40-17:50", class = c("labelled",
"numeric")), act1_84 = structure(c(7330, 9360, 9520, 4230,
8210), label = "Primary activity: 17:50-18:00", class = c("labelled",
"numeric")), act1_85 = structure(c(7330, 9360, 3290, 4230,
8210), label = "Primary activity: 18:00-18:10", class = c("labelled",
"numeric")), act1_86 = structure(c(7330, 4220, 5310, 4230,
8210), label = "Primary activity: 18:10-18:20", class = c("labelled",
"numeric")), act1_87 = structure(c(7330, 4220, 5310, 8219,
3210), label = "Primary activity: 18:20-18:30", class = c("labelled",
"numeric")), act1_88 = structure(c(8210, 5310, 5310, 8219,
3210), label = "Primary activity: 18:30-18:40", class = c("labelled",
"numeric")), act1_89 = structure(c(8210, 5310, 5310, 8219,
3210), label = "Primary activity: 18:40-18:50", class = c("labelled",
"numeric")), act1_90 = structure(c(8210, 5100, 5310, 8219,
3210), label = "Primary activity: 18:50-19:00", class = c("labelled",
"numeric")), act1_91 = structure(c(8210, 5120, 7330, 8219,
5140), label = "Primary activity: 19:00-19:10", class = c("labelled",
"numeric")), act1_92 = structure(c(8210, 5120, 7330, 8219,
5140), label = "Primary activity: 19:10-19:20", class = c("labelled",
"numeric")), act1_93 = structure(c(8210, 5120, 7330, 8219,
5140), label = "Primary activity: 19:20-19:30", class = c("labelled",
"numeric")), act1_94 = structure(c(8210, 9360, 7330, 8219,
210), label = "Primary activity: 19:30-19:40", class = c("labelled",
"numeric")), act1_95 = structure(c(8210, 3610, 7330, 8219,
210), label = "Primary activity: 19:40-19:50", class = c("labelled",
"numeric")), act1_96 = structure(c(8210, 9360, 7330, 8219,
210), label = "Primary activity: 19:50-20:00", class = c("labelled",
"numeric")), act1_97 = structure(c(8210, 3290, 7330, 8219,
210), label = "Primary activity: 20:00-20:10", class = c("labelled",
"numeric")), act1_98 = structure(c(8210, 5310, 7330, 8219,
8210), label = "Primary activity: 20:10-20:20", class = c("labelled",
"numeric")), act1_99 = structure(c(310, 210, 7330, 8219,
8210), label = "Primary activity: 20:20-20:30", class = c("labelled",
"numeric")), act1_100 = structure(c(310, 210, 7330, 9500,
8210), label = "Primary activity: 20:30-20:40", class = c("labelled",
"numeric")), act1_101 = structure(c(111, 3110, 7330, 8219,
8210), label = "Primary activity: 20:40-20:50", class = c("labelled",
"numeric")), act1_102 = structure(c(111, 3110, 7330, 8219,
8210), label = "Primary activity: 20:50-21:00", class = c("labelled",
"numeric")), act1_103 = structure(c(111, 210, 7330, 8219,
8210), label = "Primary activity: 21:00-21:10", class = c("labelled",
"numeric")), act1_104 = structure(c(110, 210, 7330, 8219,
8210), label = "Primary activity: 21:10-21:20", class = c("labelled",
"numeric")), act1_105 = structure(c(110, 8210, 7330, 8219,
8210), label = "Primary activity: 21:20-21:30", class = c("labelled",
"numeric")), act1_106 = structure(c(110, 8210, 7330, 8219,
8210), label = "Primary activity: 21:30-21:40", class = c("labelled",
"numeric")), act1_107 = structure(c(110, 8210, 7330, 8219,
8210), label = "Primary activity: 21:40-21:50", class = c("labelled",
"numeric")), act1_108 = structure(c(110, 8210, 7330, 8219,
8210), label = "Primary activity: 21:50-22:00", class = c("labelled",
"numeric")), act1_109 = structure(c(110, 8210, 7330, 310,
310), label = "Primary activity: 22:00-22:10", class = c("labelled",
"numeric")), act1_110 = structure(c(110, 8210, 7330, 310,
310), label = "Primary activity: 22:10-22:20", class = c("labelled",
"numeric")), act1_111 = structure(c(110, 8210, 7330, 111,
310), label = "Primary activity: 22:20-22:30", class = c("labelled",
"numeric")), act1_112 = structure(c(110, 8210, 7330, 111,
310), label = "Primary activity: 22:30-22:40", class = c("labelled",
"numeric")), act1_113 = structure(c(110, 8210, 7330, 110,
310), label = "Primary activity: 22:40-22:50", class = c("labelled",
"numeric")), act1_114 = structure(c(110, 8210, 7330, 110,
110), label = "Primary activity: 22:50-23:00", class = c("labelled",
"numeric")), act1_115 = structure(c(110, 8210, 8210, 110,
110), label = "Primary activity: 23:00-23:10", class = c("labelled",
"numeric")), act1_116 = structure(c(110, 8210, 8210, 110,
110), label = "Primary activity: 23:10-23:20", class = c("labelled",
"numeric")), act1_117 = structure(c(110, 8210, 8210, 110,
110), label = "Primary activity: 23:20-23:30", class = c("labelled",
"numeric")), act1_118 = structure(c(110, 8210, 8210, 110,
110), label = "Primary activity: 23:30-23:40", class = c("labelled",
"numeric")), act1_119 = structure(c(110, 8210, 110, 110,
110), label = "Primary activity: 23:40-23:50", class = c("labelled",
"numeric")), act1_120 = structure(c(110, 310, 110, 110, 110
), label = "Primary activity: 23:50-00:00", class = c("labelled",
"numeric")), act1_121 = structure(c(110, 310, 110, 110, 110
), label = "Primary activity: 00:00-00:10", class = c("labelled",
"numeric")), act1_122 = structure(c(110, 8120, 110, 110,
110), label = "Primary activity: 00:10-00:20", class = c("labelled",
"numeric")), act1_123 = structure(c(110, 8120, 110, 110,
110), label = "Primary activity: 00:20-00:30", class = c("labelled",
"numeric")), act1_124 = structure(c(110, 8120, 110, 110,
110), label = "Primary activity: 00:30-00:40", class = c("labelled",
"numeric")), act1_125 = structure(c(110, 8120, 110, 110,
110), label = "Primary activity: 00:40-00:50", class = c("labelled",
"numeric")), act1_126 = structure(c(110, 8120, 110, 110,
110), label = "Primary activity: 00:50-01:00", class = c("labelled",
"numeric")), act1_127 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 01:00-01:10", class = c("labelled",
"numeric")), act1_128 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 01:10-01:20", class = c("labelled",
"numeric")), act1_129 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 01:20-01:30", class = c("labelled",
"numeric")), act1_130 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 01:30-01:40", class = c("labelled",
"numeric")), act1_131 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 01:40-01:50", class = c("labelled",
"numeric")), act1_132 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 01:50-02:00", class = c("labelled",
"numeric")), act1_133 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 02:00-02:10", class = c("labelled",
"numeric")), act1_134 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 02:10-02:20", class = c("labelled",
"numeric")), act1_135 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 02:20-02:30", class = c("labelled",
"numeric")), act1_136 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 02:30-02:40", class = c("labelled",
"numeric")), act1_137 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 02:40-02:50", class = c("labelled",
"numeric")), act1_138 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 02:50-03:00", class = c("labelled",
"numeric")), act1_139 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 03:00-03:10", class = c("labelled",
"numeric")), act1_140 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 03:10-03:20", class = c("labelled",
"numeric")), act1_141 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 03:20-03:30", class = c("labelled",
"numeric")), act1_142 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 03:30-03:40", class = c("labelled",
"numeric")), act1_143 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 03:40-03:50", class = c("labelled",
"numeric")), act1_144 = structure(c(110, 110, 110, 110, 110
), label = "Primary activity: 03:50-04:00", class = c("labelled",
"numeric"))), .Names = c("serial", "strata", "psu", "pnum",
"DMFlag", "HhOut", "IndOut", "ind_wt", "IMonth", "IYear", "DMSex",
"DM014", "DM016", "DM510", "DM1115", "DM1619", "WorkSta", "act1_1",
"act1_2", "act1_3", "act1_4", "act1_5", "act1_6", "act1_7", "act1_8",
"act1_9", "act1_10", "act1_11", "act1_12", "act1_13", "act1_14",
"act1_15", "act1_16", "act1_17", "act1_18", "act1_19", "act1_20",
"act1_21", "act1_22", "act1_23", "act1_24", "act1_25", "act1_26",
"act1_27", "act1_28", "act1_29", "act1_30", "act1_31", "act1_32",
"act1_33", "act1_34", "act1_35", "act1_36", "act1_37", "act1_38",
"act1_39", "act1_40", "act1_41", "act1_42", "act1_43", "act1_44",
"act1_45", "act1_46", "act1_47", "act1_48", "act1_49", "act1_50",
"act1_51", "act1_52", "act1_53", "act1_54", "act1_55", "act1_56",
"act1_57", "act1_58", "act1_59", "act1_60", "act1_61", "act1_62",
"act1_63", "act1_64", "act1_65", "act1_66", "act1_67", "act1_68",
"act1_69", "act1_70", "act1_71", "act1_72", "act1_73", "act1_74",
"act1_75", "act1_76", "act1_77", "act1_78", "act1_79", "act1_80",
"act1_81", "act1_82", "act1_83", "act1_84", "act1_85", "act1_86",
"act1_87", "act1_88", "act1_89", "act1_90", "act1_91", "act1_92",
"act1_93", "act1_94", "act1_95", "act1_96", "act1_97", "act1_98",
"act1_99", "act1_100", "act1_101", "act1_102", "act1_103", "act1_104",
"act1_105", "act1_106", "act1_107", "act1_108", "act1_109", "act1_110",
"act1_111", "act1_112", "act1_113", "act1_114", "act1_115", "act1_116",
"act1_117", "act1_118", "act1_119", "act1_120", "act1_121", "act1_122",
"act1_123", "act1_124", "act1_125", "act1_126", "act1_127", "act1_128",
"act1_129", "act1_130", "act1_131", "act1_132", "act1_133", "act1_134",
"act1_135", "act1_136", "act1_137", "act1_138", "act1_139", "act1_140",
"act1_141", "act1_142", "act1_143", "act1_144"), row.names = c(NA,
5L), class = "data.frame")
答案 0 :(得分:1)
以下是一种优雅但有效的方法。首先,由于您的数据看起来有点杂乱无章,无法理解该方法的要点:我创建了一个玩具示例数据集:
# Set the seed for reproducibility
set.seed(123)
# Create our variables
i <- rnorm(30) # explanatory variable of interest
A <- 5 * i + rnorm(30) # DV 1 -- true coefficient of 5
B <- -2 * i + rnorm(30) # DV 2 -- true coefficient of -2
C <- rnorm(30) # DV 3 -- true coefficient of 0 (independent of i)
# Make a dataframe out of them
dataset <- data.frame(A, B, C, i)
运行这些回归,我们将获得要用作DV的每一列的名称,然后使用as.formula()
和paste0()
的组合在{{1 }}:
lm()
现在我们可以看一下结果并做您要寻找的预测:
# And do the regressions
DVnames <- setdiff(colnames(dataset), "i")
models <- lapply(DVnames, function(j) {
# For every column name j of your dataframe *except* i,
# Run a linear regression with j as the DV and i as the IV
lm(as.formula(paste0(j, " ~ i")), data = dataset)
})
# Check the results
for ( model in models ) {
print(summary(model))
}
#>
#> Call:
#> lm(formula = as.formula(paste0(j, " ~ i")), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1.6085 -0.5056 -0.2152 0.6932 2.0118
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 0.1720 0.1534 1.121 0.272
#> i 4.8660 0.1589 30.629 <2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 0.8393 on 28 degrees of freedom
#> Multiple R-squared: 0.971, Adjusted R-squared: 0.97
#> F-statistic: 938.1 on 1 and 28 DF, p-value: < 2.2e-16
#>
#> Call:
#> lm(formula = as.formula(paste0(j, " ~ i")), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.39301 -0.56909 0.03468 0.51764 2.08387
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 0.0313 0.1596 0.196 0.846
#> i -1.8540 0.1653 -11.218 7.18e-12 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 0.8731 on 28 degrees of freedom
#> Multiple R-squared: 0.818, Adjusted R-squared: 0.8115
#> F-statistic: 125.8 on 1 and 28 DF, p-value: 7.177e-12
#>
#> Call:
#> lm(formula = as.formula(paste0(j, " ~ i")), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1.4989 -0.6435 -0.1436 0.5917 2.2613
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.09205 0.16855 -0.546 0.589
#> i 0.03914 0.17454 0.224 0.824
#>
#> Residual standard error: 0.9221 on 28 degrees of freedom
#> Multiple R-squared: 0.001793, Adjusted R-squared: -0.03386
#> F-statistic: 0.05029 on 1 and 28 DF, p-value: 0.8242
由reprex package(v0.2.1)于2019-02-08创建
您应该注意,此方法与this answer的相关问题非常相似(如何为解释变量模拟Stata宏)。主要区别以及为什么我觉得您的问题应单独回答,是因为您拥有多个DV,因此您需要多次运行# And do your predictions
lapply(models, function(model) {
predict(model, newdata = data.frame(i = 42))
})
#> [[1]]
#> 1
#> 204.5426
#>
#> [[2]]
#> 1
#> -77.8355
#>
#> [[3]]
#> 1
#> 1.551869
,这在一定程度上改变了方法。而在该问题中,在一个lm()
调用中使用as.formula()
和paste()
就足够了(即lm()
),但是我们需要在循环中或{{ 1}}通话(即lm(as.formula(paste("income~", paste(regressors, collapse="+"))), data = Duncan)
)。然后,我们还需要使用*apply()
函数来进行所需的预测。