Regression in R

Practice

Starter Pack

First step to get started fitting linear regression in R is to open it on your computer. It’s by default loaded stats library each time R is started and able to run function lm for fitting basic linear regression.

Basic lm

lm(formula, data)

formula defines the linear regression form and data indicates the dataset in the analysis.

Example

Let’s try predicting students mathematics score (MATH) from measure of economic social culture status (ESCS). This model is defined in R as:

Model1.1 <- lm(MATH ~ sex + ESCS, PISA)

The code above represents regression equation. Where MATH is dependent variable, followed by the independent variable ESCS separated by ~.

Regression Output

After running regression equation with lm function. The next step is to see the output of Model1.1. Further information on our model can be obtained by requesting a summary of the model.

summary(Model1.1)

summary calls the result of the model as the following:


Call:
lm(formula = MATH ~ sex + ESCS, data = PISA)

Residuals:
     Min       1Q   Median       3Q      Max 
-164.989  -42.697   -5.302   39.108  240.733 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  404.618      3.447 117.371  < 2e-16 ***
sex           -9.218      3.429  -2.688  0.00728 ** 
ESCS          17.555      1.655  10.606  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 61.7 on 1294 degrees of freedom
Multiple R-squared:  0.08406,   Adjusted R-squared:  0.08264 
F-statistic: 59.38 on 2 and 1294 DF,  p-value: < 2.2e-16

Often in a regression model, we are interested in additional information that the model produces, such as predicted values and residuals. Using the R call attributes() we can get a list of the additional information available for the lm function.

Using R call attributes to find the additional information in the model produces in lm function. Such as predicted values and residuals.

attributes(Model1.1)
$names
 [1] "coefficients"  "residuals"     "effects"       "rank"         
 [5] "fitted.values" "assign"        "qr"            "df.residual"  
 [9] "xlevels"       "call"          "terms"         "model"        

$class
[1] "lm"

The list of attribute is shown above can be pulled out of the fitted regression model. For example, to obtain the predicted MATH for each individual in the sample, we can type the following:

Model1.1$fitted.values
       1        2        3        4        5        6        7        8 
405.6453 355.9665 367.2600 370.0753 388.0145 379.3464 376.9713 356.2948 
       9       10       11       12       13       14       15       16 
333.6772 380.7421 379.0493 369.6294 360.5466 407.3792 410.0252 368.4726 
      17       18       19       20       21       22       23       24 
412.3670 383.7632 379.9451 397.5889 352.1190 361.6600 370.1688 375.1877 
      25       26       27       28       29       30       31       32 
360.5852 393.0497 362.8555 358.9087 369.7365 369.1752 366.0926 314.0948 
      33       34       35       36       37       38       39       40 
367.1753 366.7123 393.1581 359.0439 381.0914 357.6996 338.7856 398.5176 
      41       42       43       44       45       46       47       48 
366.2922 391.3855 378.1018 354.6609 420.0139 385.0390 368.2163 378.7439 
      49       50       51       52       53       54       55       56 
355.0256 372.8442 360.7313 369.6382 398.0370 360.8349 362.1230 336.0370 
      57       58       59       60       61       62       63       64 
365.3184 359.2883 360.3236 381.7251 351.8346 345.4214 353.5883 380.8434 
      65       66       67       68       69       70       71       72 
402.5311 401.3373 374.4307 386.2701 353.8464 361.3528 389.6371 372.8529 
      73       74       75       76       77       78       79       80 
382.7657 355.0945 384.9219 373.5705 360.3710 362.1796 356.4107 403.0718 
      81       82       83       84       85       86       87       88 
356.4528 390.0755 347.6811 379.7046 380.4120 371.9150 355.2362 357.6733 
      89       90       91       92       93       94       95       96 
360.7537 392.1224 385.6201 343.6518 402.6066 347.0645 382.4514 391.2398 
      97       98       99      100      101      102      103      104 
391.8485 398.9674 368.7855 383.2554 359.0439 367.0717 372.5826 392.4735 
     105      106      107      108      109      110      111      112 
398.1669 369.3630 359.3814 366.7452 375.7964 384.0717 403.3509 349.5555 
     113      114      115      116      117      118      119      120 
373.6108 366.6486 363.8105 368.9435 349.3413 375.5265 370.6200 395.4600 
     121      122      123      124      125      126      127      128 
400.8476 406.4107 364.7035 386.9740 357.3082 339.8793 361.6021 357.0409 
     129      130      131      132      133      134      135      136 
370.9131 368.4853 372.8700 384.3596 357.2239 375.2492 388.0461 382.1354 
     137      138      139      140      141      142      143      144 
380.5696 375.3422 375.2299 386.4965 381.1581 374.5026 403.4803 404.2128 
     145      146      147      148      149      150      151      152 
371.4801 368.4726 369.6575 381.2419 390.7689 380.9948 399.8469 380.9259 
     153      154      155      156      157      158      159      160 
363.8087 388.7260 356.0144 377.7854 346.9210 399.1899 374.9455 399.1074 
     161      162      163      164      165      166      167      168 
371.0553 365.6480 323.6113 402.5785 370.5129 370.8600 393.6496 383.0518 
     169      170      171      172      173      174      175      176 
387.0003 345.2656 370.9043 397.3998 361.7526 344.5717 373.8531 352.3612 
     177      178      179      180      181      182      183      184 
366.7474 362.8620 374.4289 364.5701 377.8964 393.2182 349.4436 386.8213 
     185      186      187      188      189      190      191      192 
355.0554 371.8115 330.7736 357.1199 375.2013 383.7435 379.1669 392.7666 
     193      194      195      196      197      198      199      200 
373.6885 379.2231 364.7316 367.8568 363.0832 353.0494 392.1790 354.8641 
     201      202      203      204      205      206      207      208 
385.1975 362.5395 371.7909 399.4186 363.9820 414.4350 392.2400 364.0053 
     209      210      211      212      213      214      215      216 
375.3949 381.0686 362.0106 373.2738 368.4726 369.2748 399.0271 366.2120 
     217      218      219      220      221      222      223      224 
369.0874 371.3081 386.8476 363.9733 387.3971 407.4727 380.3085 364.7918 
     225      226      227      228      229      230      231      232 
368.3765 358.5001 397.6877 389.2017 367.0541 375.1456 384.5176 364.0365 
     233      234      235      236      237      238      239      240 
408.1152 416.5481 413.4027 369.2537 384.8635 368.8224 385.4924 354.4638 
     241      242      243      244      245      246      247      248 
383.1835 337.6586 356.0824 390.2761 368.0003 381.4390 382.3536 377.6910 
     249      250      251      252      253      254      255      256 
380.4278 384.9793 376.7185 360.0726 383.9401 401.6160 381.7054 357.9208 
     257      258      259      260      261      262      263      264 
371.2800 381.4736 410.6695 351.6480 390.6114 357.2761 404.4954 408.4488 
     265      266      267      268      269      270      271      272 
394.2979 361.7192 374.6418 378.2054 373.8711 391.6414 354.6517 376.0163 
     273      274      275      276      277      278      279      280 
354.4445 367.2688 387.8548 350.4473 373.9255 383.2419 372.3298 364.5438 
     281      282      283      284      285      286      287      288 
393.1077 369.3766 404.2075 366.0991 366.7755 391.0046 390.8413 377.1714 
     289      290      291      292      293      294      295      296 
343.9678 368.4484 384.8569 366.3875 386.4210 376.1300 363.2131 381.7304 
     297      298      299      300      301      302      303      304 
388.1291 375.9193 352.5719 369.0769 345.9708 397.0834 416.3221 345.8198 
     305      306      307      308      309      310      311      312 
358.8354 364.9603 396.3267 368.9470 401.3198 393.5584 402.4850 368.4726 
     313      314      315      316      317      318      319      320 
373.1250 383.4946 364.6829 379.4465 360.6085 375.9057 413.4466 414.0575 
     321      322      323      324      325      326      327      328 
366.1659 368.1478 373.1562 380.7210 349.7350 393.0866 356.0038 377.5436 
     329      330      331      332      333      334      335      336 
377.7841 415.4865 363.9504 380.4888 376.1023 383.5240 374.0186 377.6910 
     337      338      339      340      341      342      343      344 
337.5902 364.5267 368.0986 378.2629 352.0623 368.4234 385.0289 408.3452 
     345      346      347      348      349      350      351      352 
375.3053 365.8679 366.2471 370.9618 384.2754 373.3023 368.3027 382.0587 
     353      354      355      356      357      358      359      360 
351.5783 403.5879 363.3360 370.7148 378.5121 372.3175 380.0311 361.1614 
     361      362      363      364      365      366      367      368 
375.5142 336.4473 350.5526 359.8378 384.5848 377.8117 367.6387 365.7796 
     369      370      371      372      373      374      375      376 
362.6251 393.9907 391.2679 361.8404 391.7625 357.9783 353.3912 385.3234 
     377      378      379      380      381      382      383      384 
352.6908 369.0940 379.5905 387.4217 378.0943 387.0003 381.4636 371.4867 
     385      386      387      388      389      390      391      392 
409.6267 375.5441 411.8597 370.4409 374.1814 362.0510 371.3994 382.8785 
     393      394      395      396      397      398      399      400 
400.9985 392.1908 386.6400 361.8333 390.9690 376.5289 369.9594 389.1719 
     401      402      403      404      405      406      407      408 
389.4211 362.6396 383.2805 382.4040 349.3273 413.0973 352.0448 343.6185 
     409      410      411      412      413      414      415      416 
395.0646 397.8966 406.1908 350.7685 380.0627 394.8052 365.8872 378.9984 
     417      418      419      420      421      422      423      424 
379.6409 338.9717 396.2530 402.0782 371.7751 388.3551 383.8190 348.9832 
     425      426      427      428      429      430      431      432 
385.3607 352.2383 384.2390 398.2512 360.2797 376.1093 380.1750 371.5346 
     433      434      435      436      437      438      439      440 
386.6826 409.8093 364.5899 388.3398 377.2903 360.8380 364.7931 400.6364 
     441      442      443      444      445      446      447      448 
368.4726 360.0309 370.9377 362.6852 365.5343 386.1362 396.3781 359.0895 
     449      450      451      452      453      454      455      456 
337.9623 363.4822 377.0604 371.1637 373.9268 414.7316 370.0648 350.9283 
     457      458      459      460      461      462      463      464 
369.0418 371.8782 379.3219 397.1202 346.6186 378.2032 369.9226 367.9845 
     465      466      467      468      469      470      471      472 
418.6692 376.8782 348.6519 392.9615 371.9168 371.4692 378.1347 362.8151 
     473      474      475      476      477      478      479      480 
360.9244 356.3211 396.2179 363.6327 370.2232 373.4428 366.4911 380.4906 
     481      482      483      484      485      486      487      488 
336.3227 354.2163 379.6782 362.3178 375.0315 385.2378 330.5208 359.3915 
     489      490      491      492      493      494      495      496 
363.4343 412.8335 392.0293 350.8445 380.1610 392.7754 357.6733 368.4726 
     497      498      499      500      501      502      503      504 
369.2344 357.7067 388.9063 343.6553 403.4176 361.8268 382.8802 348.6168 
     505      506      507      508      509      510      511      512 
362.0457 363.9452 343.5167 345.8238 404.2374 400.6961 378.0825 389.4874 
     513      514      515      516      517      518      519      520 
361.0890 384.9780 343.9648 360.1415 354.6311 379.2815 372.5593 393.3688 
     521      522      523      524      525      526      527      528 
383.1501 379.3113 365.2144 388.7225 387.8056 370.1706 382.7486 369.9897 
     529      530      531      532      533      534      535      536 
391.6524 350.8252 362.5654 389.3527 337.8715 392.1544 364.4332 374.0655 
     537      538      539      540      541      542      543      544 
357.4749 380.7263 381.2511 384.1178 364.5197 409.9967 367.3284 412.7111 
     545      546      547      548      549      550      551      552 
362.3161 377.3224 383.4450 371.3696 396.1323 377.2996 385.3708 374.7782 
     553      554      555      556      557      558      559      560 
360.2832 368.3326 346.3781 402.6552 385.7272 388.9595 349.3365 352.6013 
     561      562      563      564      565      566      567      568 
347.7281 364.4701 398.7142 360.0480 354.9308 412.5865 387.5533 371.6961 
     569      570      571      572      573      574      575      576 
367.2951 408.2996 375.1943 392.1070 376.7641 327.0906 375.2597 392.7601 
     577      578      579      580      581      582      583      584 
345.3283 368.2970 356.9987 359.2651 379.7111 350.1769 342.4669 373.3690 
     585      586      587      588      589      590      591      592 
373.1672 383.7492 377.7929 374.3271 383.4806 389.2065 381.4811 367.9090 
     593      594      595      596      597      598      599      600 
367.5070 375.2645 363.9316 359.3177 370.5094 341.9688 373.8641 372.5896 
     601      602      603      604      605      606      607      608 
356.0367 363.3817 377.3347 355.1981 395.4402 361.1176 363.9509 382.4080 
     609      610      611      612      613      614      615      616 
376.5570 353.0529 366.7065 365.2008 352.9177 370.1074 374.9174 417.2174 
     617      618      619      620      621      622      623      624 
358.9601 360.2376 419.3187 358.0099 405.8981 379.9393 370.9109 371.5082 
     625      626      627      628      629      630      631      632 
363.2974 382.8236 345.4552 363.2921 357.9801 375.7947 389.2245 383.8861 
     633      634      635      636      637      638      639      640 
340.7816 388.9502 419.5785 355.3170 360.4974 367.5878 367.1647 363.6608 
     641      642      643      644      645      646      647      648 
376.9906 375.3207 380.7820 345.9568 385.2782 376.9884 365.9991 344.8701 
     649      650      651      652      653      654      655      656 
356.5985 376.0053 347.4805 374.0554 335.9330 354.2461 397.2168 386.8318 
     657      658      659      660      661      662      663      664 
422.4891 330.0170 416.6943 369.8809 355.0098 389.1890 366.4151 368.2132 
     665      666      667      668      669      670      671      672 
408.1762 378.1259 396.8938 346.8350 391.1801 377.5225 402.6311 346.3469 
     673      674      675      676      677      678      679      680 
396.1547 351.1394 345.5026 374.4605 406.2018 402.2555 411.8965 355.7980 
     681      682      683      684      685      686      687      688 
386.6615 343.3609 402.3638 354.7570 361.3036 366.1185 373.8483 364.3880 
     689      690      691      692      693      694      695      696 
365.3127 372.5914 361.8070 370.7599 368.8768 397.6118 357.5798 374.4574 
     697      698      699      700      701      702      703      704 
353.5497 405.8401 356.1109 376.6900 379.6835 398.6282 398.0651 366.3186 
     705      706      707      708      709      710      711      712 
382.1477 355.7265 375.1364 380.7592 373.0965 382.3742 384.1297 358.6985 
     713      714      715      716      717      718      719      720 
357.7962 378.6513 380.9492 367.9253 395.5197 383.4169 376.4218 388.8976 
     721      722      723      724      725      726      727      728 
400.9805 368.7815 337.1671 369.8998 420.9513 366.3625 407.2090 369.9020 
     729      730      731      732      733      734      735      736 
412.5075 375.7267 391.4241 356.4177 395.9866 366.8281 357.8278 411.7772 
     737      738      739      740      741      742      743      744 
357.4095 385.9769 366.1378 371.0325 351.1126 381.8656 405.5452 357.6803 
     745      746      747      748      749      750      751      752 
368.1390 397.8510 369.6948 421.6342 371.2655 345.5289 404.4059 347.1698 
     753      754      755      756      757      758      759      760 
373.9448 407.3867 351.6621 351.9956 382.7341 362.0918 383.2151 370.7775 
     761      762      763      764      765      766      767      768 
365.5830 345.0264 369.5399 387.2128 370.9583 371.9945 371.2537 347.1685 
     769      770      771      772      773      774      775      776 
359.9444 375.0561 343.0304 378.1913 388.3288 367.6036 370.1442 364.1313 
     777      778      779      780      781      782      783      784 
361.8860 398.3565 409.7408 369.0365 369.8265 358.0450 362.6255 394.6314 
     785      786      787      788      789      790      791      792 
362.8186 379.1459 376.6478 374.3675 375.6722 394.0464 363.7784 342.6582 
     793      794      795      796      797      798      799      800 
353.7726 366.1891 378.0123 395.6812 340.2673 371.4714 381.2880 420.3334 
     801      802      803      804      805      806      807      808 
391.3873 348.6923 386.6370 362.6391 376.4411 386.2472 376.0935 374.9771 
     809      810      811      812      813      814      815      816 
352.4362 356.7969 396.6765 393.1476 411.9615 402.0531 371.1145 344.2527 
     817      818      819      820      821      822      823      824 
399.4063 391.1604 401.2180 378.4143 379.6730 379.8661 370.3720 359.5108 
     825      826      827      828      829      830      831      832 
410.9890 415.9271 375.9053 391.9016 371.8716 382.2114 364.9124 360.1182 
     833      834      835      836      837      838      839      840 
349.8417 379.3390 375.2259 356.1228 374.4469 361.7420 408.1481 384.8319 
     841      842      843      844      845      846      847      848 
365.9311 350.5671 406.7354 391.7678 379.0024 355.7945 336.7686 371.9892 
     849      850      851      852      853      854      855      856 
363.0253 376.3498 387.9145 354.8167 371.4714 363.0867 380.6613 409.9304 
     857      858      859      860      861      862      863      864 
399.8289 379.8252 361.0930 377.8227 341.5650 342.3673 359.0807 414.7878 
     865      866      867      868      869      870      871      872 
360.8222 329.8380 409.0614 355.8278 355.9442 358.4159 383.4064 400.1348 
     873      874      875      876      877      878      879      880 
394.9456 369.0957 337.4480 358.7090 349.7943 412.3069 346.5049 372.6405 
     881      882      883      884      885      886      887      888 
337.0109 369.1361 369.9805 376.0075 388.5535 397.5819 374.0467 373.9501 
     889      890      891      892      893      894      895      896 
359.0584 378.5157 396.1214 367.8200 392.6986 431.2876 359.8005 414.2068 
     897      898      899      900      901      902      903      904 
411.5134 356.2193 371.0799 340.9015 351.6621 370.8846 383.7246 407.9169 
     905      906      907      908      909      910      911      912 
338.0922 364.5315 343.6681 373.9338 377.6910 369.9072 407.4903 361.8386 
     913      914      915      916      917      918      919      920 
365.9148 395.4104 364.3226 368.4555 401.4093 359.6008 343.5979 419.9682 
     921      922      923      924      925      926      927      928 
370.8934 356.7389 407.0791 334.1968 361.2014 374.4662 375.0719 392.3839 
     929      930      931      932      933      934      935      936 
397.3032 351.2992 388.5201 350.8914 364.9269 355.1221 354.0746 338.1660 
     937      938      939      940      941      942      943      944 
360.3346 387.8237 367.7076 341.8086 342.9361 370.7231 357.5816 364.0158 
     945      946      947      948      949      950      951      952 
356.6621 366.5134 378.5969 360.4641 367.5878 334.1143 337.1074 411.6806 
     953      954      955      956      957      958      959      960 
348.8152 361.2580 374.9099 373.2426 378.0171 376.1831 409.5192 340.7500 
     961      962      963      964      965      966      967      968 
371.5288 366.2361 355.8717 381.5706 363.0025 419.8857 367.0598 378.1492 
     969      970      971      972      973      974      975      976 
369.3064 387.3146 374.7664 350.5052 388.8418 402.9734 408.5804 392.3962 
     977      978      979      980      981      982      983      984 
372.5598 356.9939 414.7580 367.8968 369.2665 344.5739 365.8183 389.2000 
     985      986      987      988      989      990      991      992 
353.0208 345.7571 377.6910 350.6474 413.9570 365.6502 377.6910 383.4982 
     993      994      995      996      997      998      999     1000 
405.7647 398.3965 355.0102 397.2536 366.9365 365.5795 382.1864 352.4836 
    1001     1002     1003     1004     1005     1006     1007     1008 
371.0759 370.6463 356.8706 380.3120 374.8660 391.9332 372.8700 387.3391 
    1009     1010     1011     1012     1013     1014     1015     1016 
408.9241 371.2445 372.6704 354.5060 371.0847 383.4859 368.8803 361.4283 
    1017     1018     1019     1020     1021     1022     1023     1024 
351.9382 339.7986 396.4834 398.3929 388.0220 339.1174 394.2048 378.3599 
    1025     1026     1027     1028     1029     1030     1031     1032 
386.3363 390.7513 385.8237 373.2655 377.4716 381.3600 405.6312 395.3805 
    1033     1034     1035     1036     1037     1038     1039     1040 
372.1011 386.3416 371.3906 358.4650 371.6552 363.7766 374.7817 350.2459 
    1041     1042     1043     1044     1045     1046     1047     1048 
382.6568 363.7170 341.7107 355.4364 401.3105 394.6542 364.7531 381.7392 
    1049     1050     1051     1052     1053     1054     1055     1056 
370.1267 376.1124 385.6188 380.1101 387.2737 377.1341 372.6664 364.8602 
    1057     1058     1059     1060     1061     1062     1063     1064 
342.7166 410.5413 357.0062 353.6721 381.9498 375.2347 378.1698 388.3218 
    1065     1066     1067     1068     1069     1070     1071     1072 
367.2209 405.0976 353.0634 379.7414 362.1989 387.0245 352.9998 353.0072 
    1073     1074     1075     1076     1077     1078     1079     1080 
353.3092 345.9133 388.3481 359.1246 399.0899 351.6516 354.5937 389.1666 
    1081     1082     1083     1084     1085     1086     1087     1088 
363.5871 395.6285 373.5968 417.8213 405.0853 388.5798 384.9618 379.2512 
    1089     1090     1091     1092     1093     1094     1095     1096 
353.5282 372.2350 345.0913 354.5661 412.8814 379.5040 376.7391 377.0762 
    1097     1098     1099     1100     1101     1102     1103     1104 
358.6998 353.3105 364.4525 370.3742 399.0934 374.4184 371.8506 375.4370 
    1105     1106     1107     1108     1109     1110     1111     1112 
358.4246 380.7118 382.1201 385.9611 357.3270 404.8641 386.2415 371.1220 
    1113     1114     1115     1116     1117     1118     1119     1120 
369.7194 380.5735 385.2062 355.9705 348.5514 389.0854 397.3010 358.8056 
    1121     1122     1123     1124     1125     1126     1127     1128 
386.0976 408.2539 346.7788 351.0266 362.8568 368.6183 367.5544 365.0235 
    1129     1130     1131     1132     1133     1134     1135     1136 
345.4723 377.7696 349.9211 350.0054 391.4079 396.7573 349.2852 356.5423 
    1137     1138     1139     1140     1141     1142     1143     1144 
396.1635 370.8570 385.3888 402.5995 397.2887 396.1236 364.1400 364.5473 
    1145     1146     1147     1148     1149     1150     1151     1152 
423.0649 408.9280 357.8484 399.9518 382.7784 405.4873 382.8153 365.9552 
    1153     1154     1155     1156     1157     1158     1159     1160 
403.6704 365.6221 380.5590 360.4483 360.1428 341.6839 381.9446 409.6179 
    1161     1162     1163     1164     1165     1166     1167     1168 
376.2230 353.6370 343.4504 405.0076 365.4250 365.6638 371.9805 371.7676 
    1169     1170     1171     1172     1173     1174     1175     1176 
370.9952 332.1166 357.6430 394.0714 392.7895 392.5384 371.9331 376.2884 
    1177     1178     1179     1180     1181     1182     1183     1184 
392.0930 380.1469 347.4191 399.1741 352.3665 382.5309 377.4224 341.9209 
    1185     1186     1187     1188     1189     1190     1191     1192 
349.7311 328.7812 359.6513 367.1243 381.5579 382.3150 379.7059 388.4117 
    1193     1194     1195     1196     1197     1198     1199     1200 
392.7425 358.4475 371.8786 368.7411 356.9057 376.5500 396.3851 370.9219 
    1201     1202     1203     1204     1205     1206     1207     1208 
400.4820 336.0524 368.4726 394.4664 382.3553 408.5102 355.3894 372.4123 
    1209     1210     1211     1212     1213     1214     1215     1216 
387.6955 376.8098 364.2717 367.1437 379.3021 371.1848 349.2729 359.1685 
    1217     1218     1219     1220     1221     1222     1223     1224 
354.2865 349.3190 360.5834 396.6094 372.4347 397.6930 391.0779 363.6525 
    1225     1226     1227     1228     1229     1230     1231     1232 
370.8126 403.9653 362.5693 369.6615 408.1890 374.7945 371.3515 354.7535 
    1233     1234     1235     1236     1237     1238     1239     1240 
352.6860 396.4817 354.5428 348.9920 376.6588 373.2409 377.5330 374.7085 
    1241     1242     1243     1244     1245     1246     1247     1248 
378.2914 379.3530 368.8675 408.0516 377.3996 375.4054 356.2456 404.7711 
    1249     1250     1251     1252     1253     1254     1255     1256 
358.8196 403.9144 342.9694 351.0148 368.0780 359.6359 393.1902 380.6069 
    1257     1258     1259     1260     1261     1262     1263     1264 
359.3498 389.0275 355.5347 381.6040 376.5061 366.2466 348.1792 373.6201 
    1265     1266     1267     1268     1269     1270     1271     1272 
374.2288 375.1311 384.4461 350.9511 347.1558 379.9042 363.3097 382.4168 
    1273     1274     1275     1276     1277     1278     1279     1280 
413.3097 383.6017 371.5451 373.3616 355.0800 375.6384 343.2621 351.7902 
    1281     1282     1283     1284     1285     1286     1287     1288 
380.9211 337.9535 379.2494 380.5402 380.9277 356.9443 380.2154 372.5703 
    1289     1290     1291     1292     1293     1294     1295     1296 
366.3594 335.4770 355.2889 369.8927 342.7991 373.0315 359.2532 348.8217 
    1297 
377.0972 

We can obtain another attributes in the same regression by typing the model following by $ then typing the list of attributes.

Interaction Terms in Regression

More complicated regression relationships can also be easily modeled using the lm() function. An interaction is simply computed as the product of the interacting variables, so that the moderation model using lm() is defined as:

Model1.2 <- lm(MATH ~ sex + ESCS + sex*ESCS, PISA)

summary(Model1.2)

Call:
lm(formula = MATH ~ sex + ESCS + sex * ESCS, data = PISA)

Residuals:
     Min       1Q   Median       3Q      Max 
-165.103  -42.686   -5.276   39.156  240.895 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 404.7758     4.1301  98.006  < 2e-16 ***
sex          -9.5613     6.0216  -1.588    0.113    
ESCS         17.6592     2.2390   7.887 6.53e-15 ***
sex:ESCS     -0.2304     3.3261  -0.069    0.945    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 61.72 on 1293 degrees of freedom
Multiple R-squared:  0.08406,   Adjusted R-squared:  0.08194 
F-statistic: 39.56 on 3 and 1293 DF,  p-value: < 2.2e-16

The slope for interaction is denoted sex:ESCS, takes the value -0.8908, and is non-significant (t=-0.284, p=0.7765).