Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Output From R

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

94 40.45437 112.

75371
95 30.47628 134.28328
96 32.53046 132.63435
97 30.70418 131.96771
98 38.21548 110.73983
99 30.76730 136.11736
100 43.06904 137.21223
101 39.47384 136.27282
102 35.37062 128.76583
103 38.73079 125.82947
104 44.23093 94.95191
105 38.35623 116.26313
106 43.01599 100.99618
107 43.57989 123.48565
108 38.18321 137.85407
109 36.91595 110.28309
110 40.53882 102.64620
111 43.30989 130.74443
112 36.33266 93.08359
113 43.99361 107.82532
114 42.50224 135.21288
115 31.88724 127.86820
116 35.57847 97.31595
117 32.81843 134.84267
118 40.72847 111.75291
119 39.74200 96.20324
120 34.56578 111.08501
121 38.48888 107.34480
122 42.84736 136.24240
123 38.39197 94.16815
124 40.55587 97.32602
125 30.62818 133.80761
126 35.95740 127.23167
127 36.44058 109.20511
128 39.47688 124.53750
129 32.87825 115.34648
130 30.88856 127.03535
131 31.92086 113.64495
132 36.50269 119.48695
133 31.99435 115.81116
134 44.62640 129.29492
135 32.79103 90.71677
136 35.38556 95.77536
137 33.23623 95.34652
138 38.54780 100.39178
139 30.77576 108.47784
140 43.56586 109.14431
141 41.02930 92.73551
142 44.29885 128.14114
143 33.53292 106.78702
144 38.93325 91.14525
145 42.75066 126.77994
146 37.82575 136.55130
147 35.67410 114.31719
148 38.22135 117.55480
149 30.95461 133.38534
150 39.16620 105.37716
151 39.17140 115.20970
152 44.36654 113.57351
153 39.45781 116.04628
154 33.49771 97.99230
155 43.93177 105.20428
156 38.16329 105.82271
157 35.76982 103.68885
158 38.08509 136.09164
159 31.22486 97.68839
160 35.30151 103.62329
161 35.56978 108.15024
162 32.00364 126.06557
163 34.09461 120.48599
164 44.93120 102.05166
165 43.05503 125.89190
166 34.69997 121.97308
167 32.89706 110.27408
168 32.76754 115.83243
169 32.70640 106.20642
170 37.80248 99.80468
171 43.63039 112.87687
172 38.96517 91.50575
173 31.13523 95.79264
174 35.40290 127.57149
175 43.98244 112.50737
176 34.45389 108.40042
177 44.68224 123.05328
178 42.26074 131.10320
179 40.56152 99.03297
180 44.56569 134.77906
181 40.06493 99.97324
182 37.77037 120.26091
183 35.02740 116.23542
184 34.75289 110.90091
185 30.13389 117.72962
186 40.09632 131.76406
187 33.86289 128.90112
188 40.27346 100.40110
189 42.76247 112.53555
190 36.94853 90.10384
191 32.46727 105.86990
192 44.97708 104.45595
193 35.80670 134.06820
194 33.14362 122.47040
195 34.54679 124.93479
196 32.05156 130.13572
197 42.24046 135.92919
198 31.42850 136.30557
199 33.12607 108.31228
200 40.23689 375.00000
201 37.15633 94.96238
202 35.28273 107.87330
203 36.11407 100.57403
204 34.45991 116.37855
205 40.43033 135.25152
206 31.25393 130.98829
207 43.47129 92.22210
208 40.90275 112.67017
209 38.11875 90.60352
210 42.31739 132.06368
211 32.34956 96.46086
212 36.02597 106.92071
213 35.07917 129.95019
214 44.91757 109.24362
215 31.68004 98.98040
216 32.66707 115.95073
217 38.74012 109.44899
218 37.69457 105.17535
219 30.26692 121.70053
220 38.38075 124.54683
221 43.10108 102.25438
222 32.36704 94.33074
223 33.19607 107.39965
224 39.53575 120.75488
225 42.35379 106.88124
226 38.55818 113.58127
227 43.38460 124.23249
228 38.37999 124.99362
229 30.63679 131.50257
230 39.84404 127.57822
231 36.92780 135.53331
232 30.28694 101.18292
233 30.28717 122.32895
234 38.55339 126.33698
235 32.90175 110.32459
236 36.05920 126.86738
237 42.51422 125.99497
238 39.63485 105.59275
239 31.12073 95.79029
240 44.84598 123.75677
241 35.20325 130.20357
242 39.55188 118.11718
243 32.14493 114.15934
244 41.57336 116.31815
245 31.95411 110.97780
246 39.80342 102.22990
247 44.56159 124.99975
248 36.80579 131.92530
249 31.77706 119.51531
250 42.69705 112.82007
251 33.64718 450.00000
252 37.56074 99.39613
253 40.03503 99.51368
254 42.16915 130.25808
255 37.00539 136.37898
256 30.07083 104.45684
257 43.37957 122.78451
258 39.09754 118.13996
259 34.21551 98.81553
260 43.86870 130.23403
261 41.06795 109.29433
262 34.32060 102.71047
263 44.32556 97.42637
264 42.30580 119.84386
265 38.34663 120.92120
266 42.36026 117.68991
267 43.74046 120.66045
268 37.25140 119.16029
269 30.67882 122.03349
270 30.72436 96.12994
271 43.00900 116.95161
272 44.84990 124.68266
273 31.59984 96.04971
274 35.96272 101.28536
275 32.39508 123.51602
276 41.85561 111.01880
277 40.10806 94.32888
278 30.28843 93.67168
279 34.27129 96.25643
280 35.92898 114.80234
281 44.37158 134.71699
282 41.29361 138.47977
283 44.58362 123.08730
284 35.31307 99.78723
285 40.64850 135.03444
286 30.68692 135.01775
287 33.87698 123.94516
288 34.47063 134.92786
289 44.53251 121.89566
290 36.35694 98.83699
291 37.56071 90.75516
292 33.10988 130.64377
293 38.90944 96.27349
294 44.62624 130.52563
295 34.52392 115.58773
296 42.45604 110.95171
297 40.77087 120.00367
298 30.26910 126.33124
299 42.18645 111.81187
300 32.67378 93.62976
301 35.14432 107.30073
302 36.07841 115.86791
303 39.79086 96.17660
304 33.09997 115.28430
305 34.58063 136.21826
306 31.03680 102.83211
307 32.37162 110.56837
308 30.73146 105.05806
309 36.94520 132.26684
310 34.95600 115.84065
311 32.49675 118.96976
312 38.68105 112.75322
313 41.96577 93.82408
314 35.13973 128.15457
315 37.96979 133.54485
316 44.64541 110.09128
317 39.70122 108.18411
318 33.89871 119.86910
319 37.10749 136.57080
320 30.64695 104.74134
321 31.19329 134.72989
322 32.41870 135.04635
323 40.11039 100.87947
324 42.96056 106.72028
325 42.23610 133.75749
326 34.81942 107.60605
327 40.23859 125.72731
328 40.06746 104.75204
329 32.50173 119.43682
330 39.65733 132.38456
331 30.76706 105.45507
332 36.65775 116.05893
333 40.90731 111.83196
334 36.21418 97.47271
335 40.82165 136.87299
336 44.36093 96.67194
337 40.64072 121.32824
338 41.19468 104.34527
339 39.03434 137.00047
340 32.08520 108.99651
341 41.23975 127.21471
342 36.63727 132.76264
343 42.39999 117.07672
344 35.53409 133.26295
345 40.08038 95.86387
346 39.49847 99.41868
347 36.82418 136.05086
348 38.04509 128.09280
349 40.79490 125.18778
350 44.84328 115.82494
351 43.75386 127.65288
352 33.71250 93.19380
353 32.24545 115.25489
354 36.53138 121.61519
355 40.50498 122.47585
356 44.40553 105.43315
357 31.14487 135.05110
358 34.67250 120.70326
359 30.75226 95.68109
360 37.52929 116.34962
361 42.19543 103.35038
362 36.13316 121.19941
363 44.52009 126.47998
364 35.94076 94.91341
365 36.42218 92.97961
366 32.96708 113.43673
367 33.92168 96.04012
368 38.78711 107.66833
369 44.40130 92.85129
370 34.31604 123.25380
371 41.51567 125.15176
372 43.84692 124.50640
373 37.06639 135.27749
374 30.04191 133.13826
375 37.87791 93.41245
376 35.76646 122.96295
377 35.74156 98.58339
378 43.38914 111.80082
379 31.33520 127.52643
380 36.26802 114.23692
381 31.59500 113.48841
382 38.67813 106.84086
383 40.38009 133.65155
384 36.65876 103.15934
385 42.04990 113.32397
386 32.20276 101.71194
387 30.35964 93.25784
388 43.05525 132.31362
389 40.53747 106.22649
390 35.41008 128.32733
391 31.24756 125.34907
392 34.57423 116.50458
393 31.34164 126.86743
394 32.93344 93.34107
395 36.30543 121.85481
396 41.39887 102.80520
397 35.32954 124.98529
398 33.24201 98.13084
399 35.92550 390.00000
400 31.66947 116.69743
401 36.63457 119.30500
402 40.50554 460.00000
403 39.25644 117.96323
404 31.33773 132.87914
405 41.47367 98.64134
406 31.51457 116.87353
407 34.18770 129.18649
408 34.27158 97.85888
409 30.92662 127.18324
410 31.68293 134.71862
411 35.64319 131.11267
412 30.90475 129.64830
413 36.60954 126.75636
414 38.93288 123.33023
415 35.51218 132.15249
416 36.34380 104.11550
417 40.04953 127.37841
418 38.74588 133.45711
419 44.65250 119.43364
420 35.80627 121.07569
421 41.76470 105.41034
422 30.07044 122.09375
423 37.37783 135.73044
424 32.90418 135.61717
425 38.40586 107.35759
426 34.47384 126.26653
427 41.58747 131.09428
428 44.04763 98.09994
429 37.77990 94.73785
430 30.35050 103.70565
431 36.72546 121.21244
432 38.96132 135.18319
433 43.97333 122.39349
434 38.16974 127.35991
435 37.09376 127.16443
436 30.01328 121.08276
437 30.32498 105.07975
438 36.10887 105.24422
439 32.21373 131.56089
440 30.54680 131.98507
441 44.29931 118.50231
442 43.25882 117.22905
443 36.38977 115.43923
444 44.20936 92.24460
445 34.26548 124.51248
446 38.00625 89.02525
447 31.26775 130.60263
448 31.05995 102.53406
449 39.61943 90.60849
450 35.39974 133.06630
451 30.12668 95.38174
452 35.57360 104.68793
453 41.96819 130.65143
454 33.23802 112.85178
455 40.45990 115.13822
456 44.72103 117.88691
457 30.79338 96.83426
458 35.57900 105.58589
459 44.10924 94.52138
460 41.83096 129.25084
461 38.82198 134.96110
462 41.80647 116.18073
463 42.32963 99.92366
464 37.49980 113.06402
465 34.36910 104.96683
466 39.97574 100.09473
467 41.36588 104.83071
468 42.20819 100.70276
469 37.98087 97.88387
470 40.83281 138.16900
471 40.53606 105.80924
472 33.50374 113.88878
473 43.46940 125.06861
474 43.03585 107.06657
475 40.32757 103.10109
476 43.64799 113.78190
477 30.13511 95.61130
478 38.22933 96.06100
479 34.51925 112.77937
480 42.21239 113.20887
481 39.31372 105.50296
482 36.78726 136.62033
483 42.97319 124.24581
484 32.15322 100.46013
485 42.10955 129.93586
486 30.65756 112.59999
487 36.87220 106.62723
488 37.98384 131.86947
489 42.28465 124.50376
490 42.50927 105.84304
491 43.18062 110.36946
492 32.90918 120.07526
493 32.89330 102.64818
494 37.68184 111.43977
495 40.36703 109.67145
496 37.89518 95.40411
497 36.72732 119.78845
498 38.44497 95.97468
499 31.68155 109.72869
500 44.90420 129.86709
[ reached 'max' / getOption("max.print") -- omitted 500 rows ]
> # Get the directory path of the R script
> # Using psych package for descriptive statistics
> #install.packages("psych") # Install if not already installed
> library(psych)
> # Descriptive statistics with psych package for data1
> des1 = describe(data1)
> des1
vars n mean sd median trimmed mad min max range skew kurtosis se
X 1 1000 38.86 20.68 37.93 37.72 5.70 30.07 450.00 419.93 16.79 300.84 0.65
Y 2 1000 114.90 13.43 115.53 115.03 17.32 89.55 140.58 51.03 -0.07 -1.21 0.42
> # Descriptive statistics with psych package for data2
> des2 = describe(data2)
> des2
vars n mean sd median trimmed mad min max range skew kurtosis se
X 1 1000 37.38 4.37 37.48 37.36 5.67 30.01 44.98 14.97 0.01 -1.21 0.14
Y 2 1000 116.47 24.84 115.92 115.24 16.97 88.41 460.00 371.59 8.48 102.82 0.79
> # Load ggplot2 library
> library(ggplot2)
> # Create a box plot for data1
> ggplot(data1, aes(x = X, y = "")) +
+ geom_boxplot(fill = "8") + # Customize box plot appearance
+ theme_minimal() + # Customize plot theme (optional)
+ labs(y = "Y", x = "X (Data 1)") # Customize axis labels (optional)
> # Create a box plot for data1
> ggplot(data2, aes(x = "", y = Y)) +
+ geom_boxplot(fill = "8") + # Customize box plot appearance
+ theme_minimal() + # Customize plot theme (optional)
+ labs(x = "X", y = "Y (Data 2)") # Customize axis labels (optional)
> ## Linear
> ols_model = lm(data1$Y~data1$X, data = data1)
> summary(ols_model)
Call:
lm(formula = data1$Y ~ data1$X, data = data1)

Residuals:
Min 1Q Median 3Q Max
-65.784 -10.844 0.594 11.562 24.837

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 109.35289 0.88249 123.914 < 2e-16 ***
data1$X 0.14273 0.02005 7.119 2.07e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 13.11 on 998 degrees of freedom


Multiple R-squared: 0.04833, Adjusted R-squared: 0.04738
F-statistic: 50.68 on 1 and 998 DF, p-value: 2.074e-12

> m_est_model = rlm(data1$Y~data1$X, data = data1)


> summary(m_est_model)

Call: rlm(formula = data1$Y ~ data1$X, data = data1)


Residuals:
Min 1Q Median 3Q Max
-1.135e+03 -1.485e+00 -6.847e-03 1.625e+00 5.894e+00

Coefficients:
Value Std. Error t value
(Intercept) 10.3974 0.1590 65.4117
data1$X 2.7751 0.0036 768.4964

Residual standard error: 2.318 on 998 degrees of freedom


> # Fit Least Median of Squares (LMS) regression
> lms_model <- lqs(Y ~ X, data = data1)
> summary(lms_model)
Length Class Mode
crit 1 -none- numeric
sing 1 -none- character
coefficients 2 -none- numeric
bestone 2 -none- numeric
fitted.values 1000 -none- numeric
residuals 1000 -none- numeric
scale 2 -none- numeric
terms 3 terms call
call 3 -none- call
xlevels 0 -none- list
model 2 data.frame list
> # Fit Least Absolute Deviation (LAD) regression (robust to outliers)
> lad_model <- lmrob(Y ~ X, data = data1)
> summary(lad_model)

Call:
lmrob(formula = Y ~ X, data = data1)
\--> method = "MM"
Residuals:
Min 1Q Median 3Q Max
-1.226e+03 -1.473e+00 3.805e-02 1.329e+00 5.266e+00

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.06585 0.54321 3.803 0.000152 ***
X 2.99658 0.01418 211.278 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Robust residual standard error: 2.075


Multiple R-squared: 0.9771, Adjusted R-squared: 0.9771
Convergence in 9 IRWLS iterations

Robustness weights:
5 observations c(21,110,230,510,615) are outliers with |weight| = 0 ( < 0.0001);
86 weights are ~= 1. The remaining 909 ones are summarized as
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.4090 0.8691 0.9494 0.9130 0.9861 0.9990
Algorithmic parameters:
tuning.chi bb tuning.psi refine.tol rel.tol scale.tol
1.548e+00 5.000e-01 4.685e+00 1.000e-07 1.000e-07
1.000e-10
solve.tol zero.tol eps.outlier eps.x warn.limit.reject warn.limit.meanrw
1.000e-07 1.000e-10 1.000e-04 8.185e-10 5.000e-01 5.000e-
01
nResample max.it best.r.s k.fast.s k.max maxit.scale trace.lev
500 50 2 1 200 200 0
mts compute.rd fast.s.large.n
1000 0 2000
psi subsampling cov compute.outlier.stats
"bisquare" "nonsingular" ".vcov.avar1" "SM"
seed : int(0)
> lts_model = robustbase::ltsReg(data1$Y~data1$X, data = data1)
> summary(lts_model)

Call:
ltsReg.formula(formula = data1$Y ~ data1$X, data = data1)

Residuals (from reweighted LS):


Min 1Q Median 3Q Max
-4.501 -1.411 0.000 1.223 4.563

Coefficients:
Estimate Std. Error t value Pr(>|t|)
Intercept 2.27670 0.52357 4.348 1.52e-05 ***
data1$X 2.99184 0.01381 216.690 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.916 on 978 degrees of freedom


Multiple R-Squared: 0.9796, Adjusted R-squared: 0.9796
F-statistic: 4.695e+04 on 1 and 978 DF, p-value: < 2.2e-16

> # MM
> mm_model = robustbase::lmrob(data1$Y~data1$X, data = data1)
> summary(mm_model)

Call:
robustbase::lmrob(formula = data1$Y ~ data1$X, data = data1)
\--> method = "MM"
Residuals:
Min 1Q Median 3Q Max
-1.226e+03 -1.473e+00 3.805e-02 1.329e+00 5.266e+00

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.06585 0.54321 3.803 0.000152 ***
data1$X 2.99658 0.01418 211.278 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Robust residual standard error: 2.075


Multiple R-squared: 0.9771, Adjusted R-squared: 0.9771
Convergence in 9 IRWLS iterations

Robustness weights:
5 observations c(21,110,230,510,615) are outliers with |weight| = 0 ( < 0.0001);
86 weights are ~= 1. The remaining 909 ones are summarized as
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.4090 0.8691 0.9494 0.9130 0.9861 0.9990
Algorithmic parameters:
tuning.chi bb tuning.psi refine.tol rel.tol scale.tol
1.548e+00 5.000e-01 4.685e+00 1.000e-07 1.000e-07
1.000e-10
solve.tol zero.tol eps.outlier eps.x warn.limit.reject warn.limit.meanrw
1.000e-07 1.000e-10 1.000e-04 8.185e-10 5.000e-01 5.000e-
01
nResample max.it best.r.s k.fast.s k.max maxit.scale trace.lev
500 50 2 1 200 200 0
mts compute.rd fast.s.large.n
1000 0 2000
psi subsampling cov compute.outlier.stats
"bisquare" "nonsingular" ".vcov.avar1" "SM"
seed : int(0)
> # Fit Huber Regression
> huber_model <- lmrob(data1$Y~data1$X, data = data1, method = "MM") # Use method
= "MM" for Huber Regression
> summary(huber_model)

Call:
lmrob(formula = data1$Y ~ data1$X, data = data1, method = "MM")
\--> method = "MM"
Residuals:
Min 1Q Median 3Q Max
-1.226e+03 -1.473e+00 3.805e-02 1.329e+00 5.266e+00

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.06585 0.54321 3.803 0.000152 ***
data1$X 2.99658 0.01418 211.278 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Robust residual standard error: 2.075


Multiple R-squared: 0.9771, Adjusted R-squared: 0.9771
Convergence in 9 IRWLS iterations

Robustness weights:
5 observations c(21,110,230,510,615) are outliers with |weight| = 0 ( < 0.0001);
86 weights are ~= 1. The remaining 909 ones are summarized as
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.4090 0.8691 0.9494 0.9130 0.9861 0.9990
Algorithmic parameters:
tuning.chi bb tuning.psi refine.tol rel.tol scale.tol
1.548e+00 5.000e-01 4.685e+00 1.000e-07 1.000e-07
1.000e-10
solve.tol zero.tol eps.outlier eps.x warn.limit.reject warn.limit.meanrw
1.000e-07 1.000e-10 1.000e-04 8.185e-10 5.000e-01 5.000e-
01
nResample max.it best.r.s k.fast.s k.max maxit.scale trace.lev
500 50 2 1 200 200 0
mts compute.rd fast.s.large.n
1000 0 2000
psi subsampling cov compute.outlier.stats
"bisquare" "nonsingular" ".vcov.avar1" "SM"
seed : int(0)
> # Fit RANSAC Regression
> ransac_model <- rlm(data1$Y~data1$X, data = data1, method = "MM") # Use method =
"MM" for RANSAC Regression
> summary(ransac_model)

Call: rlm(formula = data1$Y ~ data1$X, data = data1, method = "MM")


Residuals:
Min 1Q Median 3Q Max
-1.226e+03 -1.473e+00 3.809e-02 1.329e+00 5.266e+00

Coefficients:
Value Std. Error t value
(Intercept) 2.0661 0.1411 14.6420
data1$X 2.9966 0.0032 934.7592

Residual standard error: 2.074 on 998 degrees of freedom


> ## Linear
> ols_model1 = lm(data2$Y~data2$X, data = data2)
> summary(ols_model1)

Call:
lm(formula = data2$Y ~ data2$X, data = data2)

Residuals:
Min 1Q Median 3Q Max
-28.02 -12.87 -0.36 10.39 342.95

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 109.5221 6.7678 16.183 <2e-16 ***
data2$X 0.1858 0.1799 1.033 0.302
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 24.84 on 998 degrees of freedom


Multiple R-squared: 0.001069, Adjusted R-squared: 6.786e-05
F-statistic: 1.068 on 1 and 998 DF, p-value: 0.3017

> m_est_model1 = rlm(data2$Y~data2$X, data = data2)


> summary(m_est_model1)

Call: rlm(formula = data2$Y ~ data2$X, data = data2)


Residuals:
Min 1Q Median 3Q Max
-26.7130 -11.5187 0.9531 11.6302 344.5056

Coefficients:
Value Std. Error t value
(Intercept) 110.9782 3.8028 29.1835
data2$X 0.1115 0.1011 1.1033

Residual standard error: 17.15 on 998 degrees of freedom


> # Fit Least Median of Squares (LMS) regression
> lms_model1 <- lqs(data2$Y~data2$X, data = data2)
> # Fit Least Absolute Deviation (LAD) regression (robust to outliers)
> lad_model1 <- lmrob(data2$Y~data2$X, data = data2)
> lts_model1 = robustbase::ltsReg(data2$Y~data2$X, data = data2)
> summary(lts_model1)

Call:
ltsReg.formula(formula = data2$Y ~ data2$X, data = data2)

Residuals (from reweighted LS):


Min 1Q Median 3Q Max
-26.5723 -11.3299 0.8534 11.6129 24.8654

Coefficients:
Estimate Std. Error t value Pr(>|t|)
Intercept 111.18551 3.68430 30.178 <2e-16 ***
data2$X 0.10214 0.09792 1.043 0.297
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 13.5 on 993 degrees of freedom


Multiple R-Squared: 0.001094, Adjusted R-squared: 8.842e-05
F-statistic: 1.088 on 1 and 993 DF, p-value: 0.2972

> # MM
> mm_model1 = robustbase::lmrob(data2$Y~data2$X, data = data2)
> summary(mm_model1)

Call:
robustbase::lmrob(formula = data2$Y ~ data2$X, data = data2)
\--> method = "MM"
Residuals:
Min 1Q Median 3Q Max
-26.644 -11.416 1.027 11.675 344.599

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 111.1734 3.8147 29.14 <2e-16 ***
data2$X 0.1044 0.1014 1.03 0.303
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Robust residual standard error: 16.37


Multiple R-squared: 0.001003, Adjusted R-squared: 1.93e-06
Convergence in 7 IRWLS iterations

Robustness weights:
5 observations c(79,200,251,399,402) are outliers with |weight| = 0 ( < 0.0001);
77 weights are ~= 1. The remaining 918 ones are summarized as
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.7731 0.8907 0.9489 0.9348 0.9829 0.9990
Algorithmic parameters:
tuning.chi bb tuning.psi refine.tol rel.tol scale.tol
1.548e+00 5.000e-01 4.685e+00 1.000e-07 1.000e-07
1.000e-10
solve.tol zero.tol eps.outlier eps.x warn.limit.reject warn.limit.meanrw
1.000e-07 1.000e-10 1.000e-04 8.181e-11 5.000e-01 5.000e-
01
nResample max.it best.r.s k.fast.s k.max maxit.scale trace.lev
500 50 2 1 200 200 0
mts compute.rd fast.s.large.n
1000 0 2000
psi subsampling cov compute.outlier.stats
"bisquare" "nonsingular" ".vcov.avar1" "SM"
seed : int(0)
> # Fit Huber Regression
> huber_model1 <- lmrob(data2$Y~data2$X, data = data2, method = "MM") # Use method
= "MM" for Huber Regression
> # Fit RANSAC Regression
> ransac_model1 <- rlm(data2$Y~data2$X, data = data2, method = "MM") # Use method
= "MM" for RANSAC Regression
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "Linear Model (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> # Add linear regression line (OLS)
> abline(ols_model, col = "blue", lwd = 2,lty=1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "Linear Model (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> # Add linear regression line (OLS)
> abline(ols_model1, col = "blue", lwd = 2,lty=1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "LMS Model (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> abline(lms_model, col = "red", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "LMS Model (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> abline(lms_model1, col = "red", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "LAD Model (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> abline(lad_model, col = "cyan", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "LAD Model (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> abline(lad_model1, col = "cyan", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "M Estimatoin (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> # Add M-estimation line
> abline(m_est_model, col = "green", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "M Estimation (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> # Add M-estimation line
> abline(m_est_model1, col = "green", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "LTS Model (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> # Add LTS line
> abline(lts_model, col = "purple", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "LTS Model (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> # Add LTS line
> abline(lts_model1, col = "purple", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "MM Model (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> # Add MM line
> abline(mm_model, col = "orange", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "MM Model (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> # Add MM line
> abline(mm_model1, col = "orange", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "Huber Model (Data 1)", xlab = "X", ylab = "Y", pch = 1)
> abline(huber_model, col = "purple", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "Huber Model (Data 2)", xlab = "X", ylab = "Y", pch = 1)
> abline(huber_model1, col = "purple", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "RANSAC Model (Data 1)", xlab = "X", ylab = "Y", pch =
1)
> abline(ransac_model, col = "pink", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "RANSAC Model (Data 2)", xlab = "X", ylab = "Y", pch =
1)
> abline(ransac_model1, col = "pink", lwd = 2, lty = 1)
> # Plot the dataset and regression lines
> plot(data1$X, data1$Y, main = "Robust Regression Models (Data 1)", xlab = "X", ylab =
"Y", pch = 1)
> # Add linear regression line (OLS)
> abline(ols_model, col = "blue", lwd = 2,lty=1)
> abline(lms_model, col = "red", lwd = 2, lty = 2)
> abline(lad_model, col = "cyan", lwd = 2, lty = 3)
> # Add M-estimation line
> abline(m_est_model, col = "green", lwd = 2, lty = 4)
> # Add LTS line
> abline(lts_model, col = "green", lwd = 2, lty = 5)
> # Add MM line
> abline(mm_model, col = "orange", lwd = 2, lty = 6)
> abline(huber_model, col = "purple", lwd = 2, lty = 7)
> abline(ransac_model, col = "pink", lwd = 2, lty = 10)
> # Add legend for regression lines
> legend("topright", legend = c("OLS", "M-Estimation","LMS","LAD", "LTS",
"MM","Huber","Ransac"),
+ col = c("blue", "green","red","cyan", "green", "orange","purple","pink"),
+ lty = c(1, 2, 3, 4),
+ lwd = 2,
+ title = "Regression Models",cex=.45)
> # Plot the dataset and regression lines
> plot(data2$X, data2$Y, main = "Robust Regression Models (Data 2)", xlab = "X", ylab =
"Y", pch = 1)
> abline(ols_model1, col = "red", lwd = 2,lty=1)
> abline(m_est_model1, col = "blue", lwd = 2, lty = 2)
> abline(lms_model1, col = "gold", lwd = 2, lty = 3)
> abline(lad_model1, col = "cyan", lwd = 2, lty = 4)
> abline(lts_model1, col = "green", lwd = 2, lty = 5)
> abline(mm_model1, col = "orange", lwd = 2, lty = 6)
> abline(huber_model1, col = "purple", lwd = 2, lty = 7)
> abline(ransac_model1, col = "pink", lwd = 2, lty = 10)
> # Add legend for regression lines
> legend("topright", legend = c("OLS", "M-Estimation","LMS","LAD", "LTS",
"MM","Huber","Ransac"),
+ col = c("red", "blue","gold","cyan", "green", "orange","purple","pink"),
+ lty = c(1, 2, 3, 4),
+ lwd = 2,
+ title = "Regression Models",cex=.45)
> # Load necessary libraries
> library(Metrics) # For calculating MAE and RMSE
> # Define a function to calculate MAE and RMSE
> calculate_errors <- function(actual, predicted) {
+ mae <- mae(actual, predicted) # Calculate Mean Absolute Error (MAE)
+ rmse <- rmse(actual, predicted) # Calculate Root Mean Squared Error (RMSE)
+ return(list(MAE = mae, RMSE = rmse))
+}
> # Calculate MAE and RMSE for each model
> mae_rmse_results <- list()
> # Ordinary Least Squares (OLS) Linear Regression
> ols_predicted <- predict(ols_model, newdata = data1)
> mae_rmse_results[["OLS"]] <- calculate_errors(data1$Y, ols_predicted)
> # Robust Regression - M-Estimation
> m_est_predicted <- predict(m_est_model, newdata = data1)
> mae_rmse_results[["M Estimation"]] <- calculate_errors(data1$Y, m_est_predicted)
> # Robust Regression - Least Median of Squares (LMS)
> lms_predicted <- fitted(lms_model)
> mae_rmse_results[["LMS"]] <- calculate_errors(data1$Y, lms_predicted)
> # Robust Regression - Least Absolute Deviation (LAD)
> lad_predicted <- predict(lad_model, newdata = data1)
> mae_rmse_results[["LAD"]] <- calculate_errors(data1$Y, lad_predicted)
> # Robust Regression - MM (M-Estimator)
> mm_predicted <- fitted(mm_model)
> mae_rmse_results[["MM"]] <- calculate_errors(data1$Y, mm_predicted)
> # Combine results into a data frame
> mae_rmse_df <- do.call(rbind, mae_rmse_results)
> # Add model names as row names
> rownames(mae_rmse_df) <- names(mae_rmse_results)
> # Print the MAE and RMSE for each model
> print(mae_rmse_df)
MAE RMSE
OLS 11.19357 13.09262
M Estimation 5.146385 56.06971
LMS 5.182051 59.67248
LAD 5.230575 60.53898
MM 5.230575 60.53898
> # Load necessary libraries
> library(Metrics) # For calculating MAE and RMSE
> library(robustbase) # For robust regression (lts)
> # Define a function to calculate MAE and RMSE
> calculate_errors <- function(actual, predicted) {
+ mae <- mae(actual, predicted) # Calculate Mean Absolute Error (MAE)
+ rmse <- rmse(actual, predicted) # Calculate Root Mean Squared Error (RMSE)
+ return(list(MAE = mae, RMSE = rmse))
+}
> # Example: Fit an LTS (Least Trimmed Squares) model
> lts_model <- robustbase::ltsReg(Y ~ X, data = data1)
> # Get fitted values (predictions) from the LTS model
> lts_predicted <- fitted(lts_model)
> # Calculate MAE and RMSE for the LTS model
> mae_rmse_results <- calculate_errors(data1$Y, lts_predicted)
> # Print the MAE and RMSE for the LTS model
> print(mae_rmse_results)
$MAE
[1] 5.223885

$RMSE
[1] 60.44527

> # Load necessary libraries


> library(Metrics) # For calculating MAE and RMSE
> # Define a function to calculate MAE and RMSE
> calculate_errors <- function(actual, predicted) {
+ mae <- mae(actual, predicted) # Calculate Mean Absolute Error (MAE)
+ rmse <- rmse(actual, predicted) # Calculate Root Mean Squared Error (RMSE)
+ return(list(MAE = mae, RMSE = rmse))
+ }
> # Calculate MAE and RMSE for each model
> mae_rmse_results <- list()
> # Ordinary Least Squares (OLS) Linear Regression
> ols_predicted <- predict(ols_model, newdata = data2)
> mae_rmse_results[["OLS"]] <- calculate_errors(data2$Y, ols_predicted)
> # Robust Regression - M-Estimation
> m_est_predicted <- predict(m_est_model, newdata = data2)
> mae_rmse_results[["M Estimation"]] <- calculate_errors(data2$Y, m_est_predicted)
> # Robust Regression - Least Median of Squares (LMS)
> lms_predicted <- fitted(lms_model)
> mae_rmse_results[["LMS"]] <- calculate_errors(data2$Y, lms_predicted)
> # Robust Regression - Least Absolute Deviation (LAD)
> lad_predicted <- predict(lad_model, newdata = data2)
> mae_rmse_results[["LAD"]] <- calculate_errors(data2$Y, lad_predicted)
> # Robust Regression - MM (M-Estimator)
> mm_predicted <- fitted(mm_model)
> mae_rmse_results[["MM"]] <- calculate_errors(data2$Y, mm_predicted)
> # Combine results into a data frame
> mae_rmse_df <- do.call(rbind, mae_rmse_results)
> # Add model names as row names
> rownames(mae_rmse_df) <- names(mae_rmse_results)
> # Print the MAE and RMSE for each model
> print(mae_rmse_df)
MAE RMSE
OLS 12.63737 24.69791
M Estimation 6.614741 59.71556
LMS 6.637546 63.099
LAD 16.74641 27.79195
MM 6.678393 63.91979
> # Load necessary libraries
> library(Metrics) # For calculating MAE and RMSE
> library(robustbase) # For robust regression (lts)
> # Define a function to calculate MAE and RMSE
> calculate_errors <- function(actual, predicted) {
+ mae <- mae(actual, predicted) # Calculate Mean Absolute Error (MAE)
+ rmse <- rmse(actual, predicted) # Calculate Root Mean Squared Error (RMSE)
+ return(list(MAE = mae, RMSE = rmse))
+}
> # Example: Fit an LTS (Least Trimmed Squares) model
> lts_model <- robustbase::ltsReg(Y ~ X, data = data2)
> # Get fitted values (predictions) from the LTS model
> lts_predicted <- fitted(lts_model)
> # Calculate MAE and RMSE for the LTS model
> mae_rmse_results <- calculate_errors(data2$Y, lts_predicted)
> # Print the MAE and RMSE for the LTS model
> print(mae_rmse_results)
$MAE
[1] 13.05608

$RMSE
[1] 24.86313

> # Fit Ordinary Least Squares (OLS) Linear Regression Model


> ols_model_data1 <- lm(Y ~ X, data = data1)
> # Get summary of OLS model
> summary_data1 <- summary(ols_model_data1)
> # Extract R-squared and adjusted R-squared
> r_squared_data1 <- summary_data1$r.squared
> adj_r_squared_data1 <- summary_data1$adj.r.squared
> # Calculate AIC and BIC
> aic_data1 <- AIC(ols_model_data1)
> bic_data1 <- BIC(ols_model_data1)
> # Print results for data1
> cat("R-squared (data1):", r_squared_data1, "\n")
R-squared (data1): 0.04832922
> cat("Adjusted R-squared (data1):", adj_r_squared_data1, "\n")
Adjusted R-squared (data1): 0.04737564
> cat("AIC (data1):", aic_data1, "\n")
AIC (data1): 7987.975
> cat("BIC (data1):", bic_data1, "\n")
BIC (data1): 8002.698
> # Fit Ordinary Least Squares (OLS) Linear Regression Model
> ols_model_data2 <- lm(Y ~ X, data = data2)
> # Get summary of OLS model
> summary_data2 <- summary(ols_model_data2)
> # Extract R-squared and adjusted R-squared
> r_squared_data2 <- summary_data2$r.squared
> adj_r_squared_data2 <- summary_data2$adj.r.squared
> # Calculate AIC and BIC
> aic_data2 <- AIC(ols_model_data2)
> bic_data2 <- BIC(ols_model_data2)
> # Print results for data2
> cat("R-squared (data2):", r_squared_data2, "\n")
R-squared (data2): 0.001068792
> cat("Adjusted R-squared (data2):", adj_r_squared_data2, "\n")
Adjusted R-squared (data2): 6.785927e-05
> cat("AIC (data2):", aic_data2, "\n")
AIC (data2): 9266.952
> cat("BIC (data2):", bic_data2, "\n")
BIC (data2): 9281.676

You might also like