2015-12-17 141 views
1

總結我有一個R數據表,TEST_DATA,它看起來像這樣:,使用R的R - 多變量回歸

duration distance speed hincome fi_cost 
264 1121 4 1 28 
294 1107 4 1 28 
111 570 5 1 14 
829 2742 3 1 69 
181 1111 6 1 28 
213 1111 5 1 28 
83 445 5 1 11 
257 1150 4 1 29 
147 812 6 1 20 
966 3969 4 1 99 
748 3363 4 1 84 

林怎麼看fi_cost和hincome解釋時間。因此我說:

fit <- lm(duration ~ fi_cost + hincome, data=test_data) 

結果造成:

Call: 
lm(formula = duration ~ fi_cost + hincome, data = test_data) 

Residuals: 
    Min  1Q Median  3Q  Max 
-2688.0 -287.5 -28.6 93.5 7042.9 

Coefficients: (1 not defined because of singularities) 
       Estimate Std. Error t value Pr(>|t|)  
(Intercept) 1507.2462 74.2827 20.291 < 2e-16 *** 
fi_cost0  -961.7269 75.8125 -12.686 < 2e-16 *** 
fi_cost1.5 -200.4032 78.1713 -2.564 0.010400 * 
fi_cost1.7 -258.1474 99.7290 -2.588 0.009680 ** 
fi_cost10 1256.7538 429.9421 2.923 0.003488 ** 
fi_cost10.5 4227.7538 429.9421 9.833 < 2e-16 *** 
fi_cost10.8 3204.7538 603.4754 5.310 1.16e-07 *** 
fi_cost100 -1128.4962 308.5192 -3.658 0.000258 *** 
fi_cost101 -1054.5795 353.6563 -2.982 0.002884 ** 
fi_cost1017 195.7538 603.4754 0.324 0.745672  
fi_cost103 -1035.2462 429.9421 -2.408 0.016097 * 
fi_cost104 -949.2462 238.2346 -3.985 6.90e-05 *** 
fi_cost1044 191.7538 603.4754 0.318 0.750694  
fi_cost105 -1035.4128 255.5296 -4.052 5.19e-05 *** 
fi_cost106 -934.9962 308.5192 -3.031 0.002459 ** 
fi_cost107 -972.1212 224.3903 -4.332 1.52e-05 *** 
fi_cost108 -983.7462 255.5296 -3.850 0.000120 *** 
fi_cost109 -1029.2462 429.9421 -2.394 0.016722 * 
fi_cost11 -1443.9962 308.5192 -4.680 2.97e-06 *** 
fi_cost11.6 5096.7538 429.9421 11.855 < 2e-16 *** 
fi_cost110 -905.4962 308.5192 -2.935 0.003358 ** 
fi_cost111 -914.2462 603.4754 -1.515 0.129871  
fi_cost112 -953.7462 308.5192 -3.091 0.002008 ** 
fi_cost113 -976.7462 308.5192 -3.166 0.001559 ** 
fi_cost114 -937.5795 353.6563 -2.651 0.008059 ** 
fi_cost115 -979.2462 429.9421 -2.278 0.022809 * 
fi_cost116 -936.9962 308.5192 -3.037 0.002407 ** 
fi_cost117 -942.6906 213.0013 -4.426 9.91e-06 *** 
fi_cost118 -947.2462 353.6563 -2.678 0.007431 ** 
fi_cost119 -968.6747 238.2346 -4.066 4.89e-05 *** 
fi_cost12 -1388.7462 429.9421 -3.230 0.001249 ** 
fi_cost120 -917.4462 277.9404 -3.301 0.000974 *** 
fi_cost121 -922.4962 308.5192 -2.990 0.002809 ** 
fi_cost122 -960.4462 277.9404 -3.456 0.000556 *** 
fi_cost123 -883.0462 277.9404 -3.177 0.001501 ** 
fi_cost124 -944.5795 353.6563 -2.671 0.007600 ** 
fi_cost125 -815.2462 353.6563 -2.305 0.021214 * 
fi_cost126 -1032.2462 429.9421 -2.401 0.016407 * 
fi_cost129 -781.2462 308.5192 -2.532 0.011377 * 
fi_cost13 -1343.2462 353.6563 -3.798 0.000148 *** 
fi_cost13.1 3853.7538 603.4754 6.386 1.93e-10 *** 
fi_cost130 -863.0795 255.5296 -3.378 0.000739 *** 
fi_cost131 -966.7462 255.5296 -3.783 0.000157 *** 
fi_cost132 -961.5795 353.6563 -2.719 0.006581 ** 
fi_cost133 -236.2462 603.4754 -0.391 0.695469  
fi_cost134 -966.5795 353.6563 -2.733 0.006306 ** 
fi_cost135 -906.5795 353.6563 -2.563 0.010405 * 
fi_cost136 -636.2462 429.9421 -1.480 0.139006  
fi_cost137 -922.2462 603.4754 -1.528 0.126548  
fi_cost138 -760.2462 255.5296 -2.975 0.002948 ** 
fi_cost139 -954.2462 429.9421 -2.219 0.026519 * 
fi_cost14 -1436.2462 603.4754 -2.380 0.017368 * 
fi_cost140 -876.5795 255.5296 -3.430 0.000610 *** 
fi_cost141 -638.7462 429.9421 -1.486 0.137461  
fi_cost142 -780.7462 429.9421 -1.816 0.069467 . 
fi_cost143 -560.2462 353.6563 -1.584 0.113249  
fi_cost144 -877.2462 277.9404 -3.156 0.001612 ** 
fi_cost145 -864.0462 277.9404 -3.109 0.001894 ** 
fi_cost146 -893.5795 353.6563 -2.527 0.011558 * 
fi_cost147 -882.2462 603.4754 -1.462 0.143847  
fi_cost148 -1032.2462 603.4754 -1.711 0.087262 . 
fi_cost149 -788.2462 353.6563 -2.229 0.025888 * 
fi_cost15 -1408.6462 277.9404 -5.068 4.23e-07 *** 
fi_cost150 -771.2462 429.9421 -1.794 0.072926 . 
fi_cost152 -829.9128 353.6563 -2.347 0.018998 * 
fi_cost154 -956.2462 429.9421 -2.224 0.026204 * 
fi_cost155 -777.6462 277.9404 -2.798 0.005172 ** 
fi_cost156 -856.2462 603.4754 -1.419 0.156030  
fi_cost157 -533.7462 308.5192 -1.730 0.083714 . 
fi_cost158 -774.3712 224.3903 -3.451 0.000565 *** 
fi_cost159 -754.7462 308.5192 -2.446 0.014480 * 
fi_cost16 -1363.4962 224.3903 -6.076 1.36e-09 *** 
fi_cost16.2 952.7538 603.4754 1.579 0.114478  
fi_cost160 -839.5795 353.6563 -2.374 0.017651 * 
fi_cost162 -560.7462 308.5192 -1.818 0.069220 . 
fi_cost163 -779.2462 429.9421 -1.812 0.070004 . 
fi_cost164 -947.2462 603.4754 -1.570 0.116587  
fi_cost165 -784.5795 255.5296 -3.070 0.002154 ** 
fi_cost1659 1019.7538 603.4754 1.690 0.091155 . 
fi_cost166 -808.2462 603.4754 -1.339 0.180554  
fi_cost167 -738.5795 255.5296 -2.890 0.003871 ** 
fi_cost168 -734.5795 353.6563 -2.077 0.037865 * 
fi_cost169 -705.2462 308.5192 -2.286 0.022319 * 
fi_cost17 -1402.2462 603.4754 -2.324 0.020203 * 
fi_cost170 -596.6462 277.9404 -2.147 0.031888 * 
fi_cost171 -904.2462 603.4754 -1.498 0.134121  
fi_cost172 -673.2462 429.9421 -1.566 0.117463  
fi_cost173 -674.7462 429.9421 -1.569 0.116648  
fi_cost175 -378.2462 603.4754 -0.627 0.530845  
fi_cost176 -837.9128 353.6563 -2.369 0.017877 * 
fi_cost177 -575.9128 353.6563 -1.628 0.103519  
fi_cost178 -600.2462 429.9421 -1.396 0.162771  
fi_cost179 -766.2462 255.5296 -2.999 0.002731 ** 
fi_cost18 -1360.2462 353.6563 -3.846 0.000122 *** 
fi_cost180 -673.5795 353.6563 -1.905 0.056912 . 
fi_cost182 -592.2462 429.9421 -1.378 0.168446  
fi_cost183 -750.7462 429.9421 -1.746 0.080872 . 
fi_cost184 -724.2462 353.6563 -2.048 0.040646 * 
fi_cost186 -452.2462 603.4754 -0.749 0.453665  
fi_cost188 -409.2462 429.9421 -0.952 0.341232  
fi_cost189 -484.2462 603.4754 -0.802 0.422360  
fi_cost19 -1416.2462 429.9421 -3.294 0.000997 *** 
fi_cost190 -675.9128 353.6563 -1.911 0.056059 . 
fi_cost191 -659.5795 353.6563 -1.865 0.062261 . 
fi_cost192 -878.2462 603.4754 -1.455 0.145673  
fi_cost193 -643.2462 429.9421 -1.496 0.134712  
fi_cost195 -603.4962 308.5192 -1.956 0.050533 . 
fi_cost197 -653.5795 353.6563 -1.848 0.064678 . 
fi_cost198 -849.2462 429.9421 -1.975 0.048318 * 
fi_cost2.3 -53.4366 150.3236 -0.355 0.722254  
fi_cost2.4 764.1983 213.0013 3.588 0.000338 *** 
fi_cost2.8 669.7538 603.4754 1.110 0.267150  
fi_cost2.9 510.1068 163.1436 3.127 0.001782 ** 
fi_cost20 -1344.0795 255.5296 -5.260 1.53e-07 *** 
fi_cost200 -767.2462 353.6563 -2.169 0.030115 * 
fi_cost201 -615.9604 238.2346 -2.586 0.009763 ** 
fi_cost202 -195.2462 603.4754 -0.324 0.746309  
fi_cost204 -525.0462 277.9404 -1.889 0.058967 . 
fi_cost2042 1798.7538 603.4754 2.981 0.002896 ** 
fi_cost205 -711.2462 603.4754 -1.179 0.238645  
fi_cost206 -625.8462 277.9404 -2.252 0.024402 * 
fi_cost207 -871.9128 353.6563 -2.465 0.013733 * 
fi_cost208 -463.9962 308.5192 -1.504 0.132686  
fi_cost2080 1895.7538 603.4754 3.141 0.001696 ** 
fi_cost209 -555.4462 203.4315 -2.730 0.006358 ** 
fi_cost21 -1372.0462 277.9404 -4.936 8.33e-07 *** 
fi_cost210 -539.2462 308.5192 -1.748 0.080578 . 
fi_cost2106 1934.7538 603.4754 3.206 0.001358 ** 
fi_cost211 -798.2462 429.9421 -1.857 0.063447 . 
fi_cost213 -587.7462 429.9421 -1.367 0.171702  
fi_cost214 -488.4462 277.9404 -1.757 0.078941 . 
fi_cost2154 1790.7538 603.4754 2.967 0.003024 ** 
fi_cost217 -703.2462 603.4754 -1.165 0.243966  
fi_cost2177 1776.7538 603.4754 2.944 0.003259 ** 
fi_cost218 -556.2462 603.4754 -0.922 0.356729  
fi_cost219 -692.2462 429.9421 -1.610 0.107469  
fi_cost22 -1380.2462 429.9421 -3.210 0.001338 ** 
fi_cost220 -413.7462 429.9421 -0.962 0.335951  
fi_cost221 -745.2462 603.4754 -1.235 0.216942  
fi_cost222 -197.2462 429.9421 -0.459 0.646425  
fi_cost223 -437.2462 429.9421 -1.017 0.309230  
fi_cost225 -280.2462 603.4754 -0.464 0.642400  
fi_cost226 -842.2462 603.4754 -1.396 0.162906  
fi_cost227 -625.2462 429.9421 -1.454 0.145965  
fi_cost228 -717.7462 429.9421 -1.669 0.095128 . 
fi_cost229 -727.2462 429.9421 -1.691 0.090831 . 
fi_cost2294 2133.7538 429.9421 4.963 7.28e-07 *** 
fi_cost23 -1382.2462 277.9404 -4.973 6.90e-07 *** 
fi_cost230 -693.2462 603.4754 -1.149 0.250735  
fi_cost232 -754.2462 603.4754 -1.250 0.211443  
fi_cost236 -451.2462 308.5192 -1.463 0.143662  
fi_cost2368 2449.7538 429.9421 5.698 1.31e-08 *** 

等等等等....

當我完成了這一點,我得到了這是一個總結前:

lm(formula = duration ~ fi_cost + hincome, data=test_data) 

Residuals: 
    Min  1Q Median  3Q  Max 
-36.027 -1.581 -0.371 1.023 13.713 

Coefficients: 
        Estimate Std. Error t value Pr(>|t|) 
(Intercept)  6.543e+00 5.786e-02 113.083 <2e-16 *** 
fi_cost   4.359e-03 8.773e-05 49.686 <2e-16 *** 
hincome   7.090e-02 8.362e-03 8.479 <2e-16 *** 
fi_cost:hincome -1.024e-04 1.174e-05 -8.722 <2e-16 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 2.58 on 18926 degrees of freedom 
Multiple R-squared: 0.5223, Adjusted R-squared: 0.5222 
F-statistic: 6897 on 3 and 18926 DF, p-value: < 2.2e-16 

爲什麼我得到這個fi_cost的分解?數據有問題嗎?對R的呼叫是一樣的。我是否更改了「摘要」顯示的設置?

編輯

str(test_data) 

'data.frame': 2129 obs. of 5 variables: 
$ duration: int 264 294 111 829 181 213 83 257 147 966 ... 
$ distance: int 1121 1107 570 2742 1111 1111 445 1150 812 3969 ... 
$ speed : Factor w/ 25 levels "\\N","1","10",..: 20 20 21 19 22 21 21 20 22 20 ... 
$ hincome : int 1 1 1 1 1 1 1 1 1 1 ... 
$ fi_cost : Factor w/ 307 levels "\\N","0","1",..: 132 132 46 263 132 132 16 137 90 307 ... 
> 
+0

檢查一下你混了總結方差分析(FIT)(FIT)? – MLavoie

+2

你可以發佈'str(test_data)'嗎? – David

+2

'fi_cost'是一個因素!最終你想把它轉換爲'as.numeric(...)' – jogo

回答

4

的問題是,fi_cost是一個因素!最終你想用as.numeric(...)來轉換它。由於一些奇怪的「\ N」,您必須更換它們或返回讀取數據。在read.table()(及類似)中,您可以使用stringsAsFactors=FALSEas.is=TRUE來防止轉換爲因素。

強制性:讀取數據後,用str(...)

相關問題