Rev 72545 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
R version 3.4.0 RC (2017-04-18 r72543) -- "You Stupid Darkness"Copyright (C) 2017 The R Foundation for Statistical ComputingPlatform: x86_64-pc-linux-gnu (64-bit)R is free software and comes with ABSOLUTELY NO WARRANTY.You are welcome to redistribute it under certain conditions.Type 'license()' or 'licence()' for distribution details.R is a collaborative project with many contributors.Type 'contributors()' for more information and'citation()' on how to cite R or R packages in publications.Type 'demo()' for some demos, 'help()' for on-line help, or'help.start()' for an HTML browser interface to help.Type 'q()' to quit R.> ###-- Linear Models, basic functionality -- weights included.>> ## From John Maindonald :> roller <- data.frame(+ weight = c(1.9, 3.1, 3.3, 4.8, 5.3, 6.1, 6.4, 7.6, 9.8, 12.4),+ depression = c( 2, 1, 5, 5, 20, 20, 23, 10, 30, 25))>> roller.lmu <- lm(weight~depression, data=roller)> roller.lsfu <- lsfit(roller$depression, roller$weight)>> roller.lsf <- lsfit(roller$depression, roller$weight, wt = 1:10)> roller.lsf0 <- lsfit(roller$depression, roller$weight, wt = 0:9)> roller.lm <- lm(weight~depression, data=roller, weights= 1:10)> roller.lm0 <- lm(weight~depression, data=roller, weights= 0:9)> roller.lm9 <- lm(weight~depression, data=roller[-1,],weights= 1:9)> roller.glm <- glm(weight~depression, data=roller, weights= 1:10)> roller.glm0<- glm(weight~depression, data=roller, weights= 0:9)>> predict(roller.glm0, type="terms")# failed till 2003-03-31depression1 -2.66922112 -2.88981793 -2.00743084 -2.00743085 1.30152116 1.30152117 1.96331148 -0.90444689 3.507488910 2.4045050attr(,"constant")[1] 6.743646>> ## FIXME : glm()$residual [1] is NA, lm()'s is ok.> ## all.equal(residuals(roller.glm0, type = "partial"),> ## residuals(roller.lm0, type = "partial") )>>> all.equal(deviance(roller.lm),+ deviance(roller.glm))[1] TRUE> all.equal(weighted.residuals(roller.lm),+ residuals (roller.glm))[1] TRUE>> all.equal(deviance(roller.lm0),+ deviance(roller.glm0))[1] TRUE> all.equal(weighted.residuals(roller.lm0, drop=FALSE),+ residuals (roller.glm0))[1] TRUE>> (im.lm0 <- influence.measures(roller.lm0))Influence measures oflm(formula = weight ~ depression, data = roller, weights = 0:9) :dfb.1_ dfb.dprs dffit cov.r cook.d hat inf2 -0.0530 0.0482 -0.0530 1.551 0.00164 0.12773 -0.1769 0.1538 -0.1782 1.569 0.01806 0.17424 0.0130 -0.0113 0.0131 1.842 0.00010 0.26135 -0.1174 -0.0185 -0.3370 1.049 0.05552 0.08926 -0.1031 -0.0162 -0.2960 1.229 0.04577 0.11147 -0.0118 -0.1891 -0.5010 1.070 0.11932 0.15558 0.7572 -0.5948 0.7972 1.467 0.30998 0.35109 0.1225 -0.2067 -0.2664 2.391 0.04079 0.4470 *10 -0.3348 1.1321 2.0937 0.233 0.89584 0.2826 *>> all.equal(unname(im.lm0 $ infmat),+ unname(cbind( dfbetas (roller.lm0)+ , dffits (roller.lm0)+ , covratio (roller.lm0)+ ,cooks.distance(roller.lm0)+ ,lm.influence (roller.lm0)$hat)+ ))[1] TRUE>> all.equal(rstandard(roller.lm9),+ rstandard(roller.lm0),tolerance = 1e-14)[1] TRUE> all.equal(rstudent(roller.lm9),+ rstudent(roller.lm0),tolerance = 1e-14)[1] TRUE> all.equal(rstudent(roller.lm),+ rstudent(roller.glm))[1] TRUE> all.equal(cooks.distance(roller.lm),+ cooks.distance(roller.glm))[1] TRUE>>> all.equal(summary(roller.lm0)$coefficients,+ summary(roller.lm9)$coefficients, tolerance = 1e-14)[1] TRUE> all.equal(print(anova(roller.lm0), signif.st=FALSE),+ anova(roller.lm9), tolerance = 1e-14)Analysis of Variance TableResponse: weightDf Sum Sq Mean Sq F value Pr(>F)depression 1 158.41 158.408 5.4302 0.05259Residuals 7 204.20 29.172[1] TRUE>>> ### more regression tests for lm(), glm(), etc :>> ## moved from ?influence.measures:> lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)> (IM <- influence.measures(lm.SR))Influence measures oflm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) :dfb.1_ dfb.pp15 dfb.pp75 dfb.dpi dfb.ddpi dffit cov.rAustralia 0.01232 -0.01044 -0.02653 0.04534 -0.000159 0.0627 1.193Austria -0.01005 0.00594 0.04084 -0.03672 -0.008182 0.0632 1.268Belgium -0.06416 0.05150 0.12070 -0.03472 -0.007265 0.1878 1.176Bolivia 0.00578 -0.01270 -0.02253 0.03185 0.040642 -0.0597 1.224Brazil 0.08973 -0.06163 -0.17907 0.11997 0.068457 0.2646 1.082Canada 0.00541 -0.00675 0.01021 -0.03531 -0.002649 -0.0390 1.328Chile -0.19941 0.13265 0.21979 -0.01998 0.120007 -0.4554 0.655China 0.02112 -0.00573 -0.08311 0.05180 0.110627 0.2008 1.150Colombia 0.03910 -0.05226 -0.02464 0.00168 0.009084 -0.0960 1.167Costa Rica -0.23367 0.28428 0.14243 0.05638 -0.032824 0.4049 0.968Denmark -0.04051 0.02093 0.04653 0.15220 0.048854 0.3845 0.934Ecuador 0.07176 -0.09524 -0.06067 0.01950 0.047786 -0.1695 1.139Finland -0.11350 0.11133 0.11695 -0.04364 -0.017132 -0.1464 1.203France -0.16600 0.14705 0.21900 -0.02942 0.023952 0.2765 1.226Germany -0.00802 0.00822 0.00835 -0.00697 -0.000293 -0.0152 1.226Greece -0.14820 0.16394 0.02861 0.15713 -0.059599 -0.2811 1.140Guatamala 0.01552 -0.05485 0.00614 0.00585 0.097217 -0.2305 1.085Honduras -0.00226 0.00984 -0.01020 0.00812 -0.001887 0.0482 1.186Iceland 0.24789 -0.27355 -0.23265 -0.12555 0.184698 -0.4768 0.866India 0.02105 -0.01577 -0.01439 -0.01374 -0.018958 0.0381 1.202Ireland -0.31001 0.29624 0.48156 -0.25733 -0.093317 0.5216 1.268Italy 0.06619 -0.07097 0.00307 -0.06999 -0.028648 0.1388 1.162Japan 0.63987 -0.65614 -0.67390 0.14610 0.388603 0.8597 1.085Korea -0.16897 0.13509 0.21895 0.00511 -0.169492 -0.4303 0.870Luxembourg -0.06827 0.06888 0.04380 -0.02797 0.049134 -0.1401 1.196Malta 0.03652 -0.04876 0.00791 -0.08659 0.153014 0.2386 1.128Norway 0.00222 -0.00035 -0.00611 -0.01594 -0.001462 -0.0522 1.168Netherlands 0.01395 -0.01674 -0.01186 0.00433 0.022591 0.0366 1.229New Zealand -0.06002 0.06510 0.09412 -0.02638 -0.064740 0.1469 1.134Nicaragua -0.01209 0.01790 0.00972 -0.00474 -0.010467 0.0397 1.174Panama 0.02828 -0.05334 0.01446 -0.03467 -0.007889 -0.1775 1.067Paraguay -0.23227 0.16416 0.15826 0.14361 0.270478 -0.4655 0.873Peru -0.07182 0.14669 0.09148 -0.08585 -0.287184 0.4811 0.831Philippines -0.15707 0.22681 0.15743 -0.11140 -0.170674 0.4884 0.818Portugal -0.02140 0.02551 -0.00380 0.03991 -0.028011 -0.0690 1.233South Africa 0.02218 -0.02030 -0.00672 -0.02049 -0.016326 0.0343 1.195South Rhodesia 0.14390 -0.13472 -0.09245 -0.06956 -0.057920 0.1607 1.313Spain -0.03035 0.03131 0.00394 0.03512 0.005340 -0.0526 1.208Sweden 0.10098 -0.08162 -0.06166 -0.25528 -0.013316 -0.4526 1.086Switzerland 0.04323 -0.04649 -0.04364 0.09093 -0.018828 0.1903 1.147Turkey -0.01092 -0.01198 0.02645 0.00161 0.025138 -0.1445 1.100Tunisia 0.07377 -0.10500 -0.07727 0.04439 0.103058 -0.2177 1.131United Kingdom 0.04671 -0.03584 -0.17129 0.12554 0.100314 -0.2722 1.189United States 0.06910 -0.07289 0.03745 -0.23312 -0.032729 -0.2510 1.655Venezuela -0.05083 0.10080 -0.03366 0.11366 -0.124486 0.3071 1.095Zambia 0.16361 -0.07917 -0.33899 0.09406 0.228232 0.7482 0.512Jamaica 0.10958 -0.10022 -0.05722 -0.00703 -0.295461 -0.3456 1.200Uruguay -0.13403 0.12880 0.02953 0.13132 0.099591 -0.2051 1.187Libya 0.55074 -0.48324 -0.37974 -0.01937 -1.024477 -1.1601 2.091Malaysia 0.03684 -0.06113 0.03235 -0.04956 -0.072294 -0.2126 1.113cook.d hat infAustralia 8.04e-04 0.0677Austria 8.18e-04 0.1204Belgium 7.15e-03 0.0875Bolivia 7.28e-04 0.0895Brazil 1.40e-02 0.0696Canada 3.11e-04 0.1584Chile 3.78e-02 0.0373 *China 8.16e-03 0.0780Colombia 1.88e-03 0.0573Costa Rica 3.21e-02 0.0755Denmark 2.88e-02 0.0627Ecuador 5.82e-03 0.0637Finland 4.36e-03 0.0920France 1.55e-02 0.1362Germany 4.74e-05 0.0874Greece 1.59e-02 0.0966Guatamala 1.07e-02 0.0605Honduras 4.74e-04 0.0601Iceland 4.35e-02 0.0705India 2.97e-04 0.0715Ireland 5.44e-02 0.2122Italy 3.92e-03 0.0665Japan 1.43e-01 0.2233Korea 3.56e-02 0.0608Luxembourg 3.99e-03 0.0863Malta 1.15e-02 0.0794Norway 5.56e-04 0.0479Netherlands 2.74e-04 0.0906New Zealand 4.38e-03 0.0542Nicaragua 3.23e-04 0.0504Panama 6.33e-03 0.0390Paraguay 4.16e-02 0.0694Peru 4.40e-02 0.0650Philippines 4.52e-02 0.0643Portugal 9.73e-04 0.0971South Africa 2.41e-04 0.0651South Rhodesia 5.27e-03 0.1608Spain 5.66e-04 0.0773Sweden 4.06e-02 0.1240Switzerland 7.33e-03 0.0736Turkey 4.22e-03 0.0396Tunisia 9.56e-03 0.0746United Kingdom 1.50e-02 0.1165United States 1.28e-02 0.3337 *Venezuela 1.89e-02 0.0863Zambia 9.66e-02 0.0643 *Jamaica 2.40e-02 0.1408Uruguay 8.53e-03 0.0979Libya 2.68e-01 0.5315 *Malaysia 9.11e-03 0.0652> summary(IM)Potentially influential observations oflm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) :dfb.1_ dfb.pp15 dfb.pp75 dfb.dpi dfb.ddpi dffit cov.r cook.dChile -0.20 0.13 0.22 -0.02 0.12 -0.46 0.65_* 0.04United States 0.07 -0.07 0.04 -0.23 -0.03 -0.25 1.66_* 0.01Zambia 0.16 -0.08 -0.34 0.09 0.23 0.75 0.51_* 0.10Libya 0.55 -0.48 -0.38 -0.02 -1.02_* -1.16_* 2.09_* 0.27hatChile 0.04United States 0.33_*Zambia 0.06Libya 0.53_*> ## colnames will differ in the next line> all.equal(dfbetas(lm.SR), IM$infmat[, 1:5], check.attributes = FALSE,+ tolerance = 1e-12)[1] TRUE>> signif(dfbeta(lm.SR), 3)(Intercept) pop15 pop75 dpi ddpiAustralia 0.0916 -1.53e-03 -0.02910 4.27e-05 -3.16e-05Austria -0.0747 8.69e-04 0.04470 -3.46e-05 -1.62e-03Belgium -0.4750 7.50e-03 0.13200 -3.26e-05 -1.44e-03Bolivia 0.0429 -1.86e-03 -0.02470 3.00e-05 8.06e-03Brazil 0.6600 -8.92e-03 -0.19400 1.12e-04 1.34e-02Canada 0.0402 -9.87e-04 0.01120 -3.32e-05 -5.25e-04Chile -1.4000 1.83e-02 0.22700 -1.78e-05 2.25e-02China 0.1560 -8.33e-04 -0.09060 4.85e-05 2.18e-02Colombia 0.2900 -7.63e-03 -0.02700 1.58e-06 1.80e-03Costa Rica -1.7000 4.07e-02 0.15300 5.19e-05 -6.37e-03Denmark -0.2940 2.99e-03 0.04980 1.40e-04 9.46e-03Ecuador 0.5310 -1.39e-02 -0.06620 1.83e-05 9.44e-03Finland -0.8420 1.62e-02 0.12800 -4.10e-05 -3.39e-03France -1.2300 2.14e-02 0.23900 -2.76e-05 4.73e-03Germany -0.0597 1.20e-03 0.00915 -6.56e-06 -5.82e-05Greece -1.0900 2.38e-02 0.03110 1.47e-04 -1.17e-02Guatamala 0.1140 -7.95e-03 0.00667 5.46e-06 1.91e-02Honduras -0.0168 1.44e-03 -0.01120 7.64e-06 -3.74e-04Iceland 1.7800 -3.87e-02 -0.24700 -1.14e-04 3.55e-02India 0.1560 -2.31e-03 -0.01580 -1.29e-05 -3.76e-03Ireland -2.2800 4.28e-02 0.52200 -2.40e-04 -1.83e-02Italy 0.4910 -1.04e-02 0.00335 -6.57e-05 -5.67e-03Japan 4.6300 -9.33e-02 -0.71800 1.34e-04 7.49e-02Korea -1.2200 1.91e-02 0.23300 4.66e-06 -3.26e-02Luxembourg -0.5070 1.01e-02 0.04790 -2.63e-05 9.73e-03Malta 0.2700 -7.08e-03 0.00861 -8.09e-05 3.01e-02Norway 0.0165 -5.12e-05 -0.00670 -1.50e-05 -2.90e-04Netherlands 0.1040 -2.45e-03 -0.01300 4.07e-06 4.48e-03New Zealand -0.4440 9.48e-03 0.10300 -2.47e-05 -1.28e-02Nicaragua -0.0899 2.62e-03 0.01060 -4.46e-06 -2.08e-03Panama 0.2080 -7.73e-03 0.01570 -3.24e-05 -1.55e-03Paraguay -1.6700 2.33e-02 0.16800 1.31e-04 5.20e-02Peru -0.5150 2.07e-02 0.09670 -7.79e-05 -5.49e-02Philippines -1.1200 3.19e-02 0.16600 -1.01e-04 -3.26e-02Portugal -0.1590 3.73e-03 -0.00416 3.76e-05 -5.55e-03South Africa 0.1650 -2.97e-03 -0.00737 -1.93e-05 -3.24e-03South Rhodesia 1.0700 -1.97e-02 -0.10100 -6.54e-05 -1.15e-02Spain -0.2260 4.58e-03 0.00432 3.31e-05 1.06e-03Sweden 0.7390 -1.17e-02 -0.06650 -2.37e-04 -2.60e-03Switzerland 0.3200 -6.77e-03 -0.04760 8.52e-05 -3.72e-03Turkey -0.0807 -1.74e-03 0.02880 1.51e-06 4.96e-03Tunisia 0.5450 -1.53e-02 -0.08410 4.15e-05 2.03e-02United Kingdom 0.3450 -5.21e-03 -0.18700 1.17e-04 1.98e-02United States 0.5130 -1.06e-02 0.04100 -2.19e-04 -6.48e-03Venezuela -0.3740 1.46e-02 -0.03650 1.06e-04 -2.44e-02Zambia 1.1200 -1.06e-02 -0.34100 8.14e-05 4.16e-02Jamaica 0.8080 -1.45e-02 -0.06220 -6.57e-06 -5.81e-02Uruguay -0.9920 1.88e-02 0.03220 1.23e-04 1.97e-02Libya 4.0400 -6.98e-02 -0.41100 -1.80e-05 -2.01e-01Malaysia 0.2720 -8.88e-03 0.03520 -4.63e-05 -1.42e-02> covratio (lm.SR)Australia Austria Belgium Bolivia Brazil1.1928303 1.2678392 1.1761879 1.2238199 1.0823332Canada Chile China Colombia Costa Rica1.3283009 0.6547098 1.1498637 1.1666845 0.9681384Denmark Ecuador Finland France Germany0.9344047 1.1393880 1.2031561 1.2262654 1.2256855Greece Guatamala Honduras Iceland India1.1396174 1.0852720 1.1855450 0.8658808 1.2024438Ireland Italy Japan Korea Luxembourg1.2680432 1.1624611 1.0845999 0.8695843 1.1961844Malta Norway Netherlands New Zealand Nicaragua1.1282611 1.1680616 1.2285315 1.1336998 1.1742677Panama Paraguay Peru Philippines Portugal1.0667255 0.8732040 0.8312741 0.8177726 1.2331038South Africa South Rhodesia Spain Sweden Switzerland1.1945449 1.3130954 1.2081541 1.0864869 1.1471125Turkey Tunisia United Kingdom United States Venezuela1.1003557 1.1314365 1.1886236 1.6554816 1.0945955Zambia Jamaica Uruguay Libya Malaysia0.5116454 1.1995171 1.1872025 2.0905736 1.1126445>> ## predict.lm(.)>> all.equal(predict(roller.lm, se.fit=TRUE)$se.fit,+ predict(roller.lm, newdata=roller, se.fit=TRUE)$se.fit, tolerance = 1e-14)[1] TRUE>