You are on page 1of 2

R for Chapter 3

Running List of Functions Applied to the Data Set, class.data:


> attach(class.data) > reg < - lm(wt ~ ht) >summary(reg) > res < - residuals(reg) > !it < - !itted(reg) ## stores the residuals in the variable res, ie the e (sub i) s

## stores the !itted values in the variable !it, ie the " hat (sub i) s, the results o! #lugging the ## original data $-values into the !itted line

> semi.st.res < - res%&'.()((* ## !orms semi-studenti+ed residuals - value o! residual stand error (&'.()((*) is !rom ## summary(reg) > #lot(ht,semi.st.res) ## scatter #lot o! ht (hori+ontal) by semi-studenti+ed residuals (vertical) !or each data #oint

## What follows is a description of what is needed for the Modified Le ene !est ## > median(ht) ## ,inds the median o! the values stored in ht ## constructs a vector with */ entries and a . in each one (ie it re#eats . */ times) ## re#laces the ith iht value with an 6 i! the ith ht value is above the ## median(ht) which is e8ual to (4.5

> iht <- re#(-.-,*/)

> !or (i in &0*/ ) 1i! (ht2i3 > (4.5) iht2i3 < - -6-7

> alt.class.data < - cbind( class.data, res, iht) ## alters the class.data set by adding a res column and an iht column > alt.class.data > library(car) ## dis#lays the alt.class.data set (altered class.data) with the new columns added ## brings in the library that includes the 9evene :est ;;;<== =>? @, >=$: A6B=;;;

> levene:est(res ~ iht, dataCalt.class.data, centerCmedian)

## runs the Dodi!ied 9evene :est analy+ing the residuals ## (res) as a !unction o! the grou# indicator (iht) ## centering each grou# s residuals at their res#. median

## "nd of the the Modified Le ene !est # the output of the test is shown $elow ## 9evene s :est !or Eomogeneity o! Fariance (center C median) ?! , value Ar(>,) grou# & /.'G(H /.HGH( &) ## "nd of the the Modified Le ene !est output##

> hist(res)

## gives a histogram o! the residuals ## gives a boI#lot o! the residuals ## gives the J-J #lot o! the residuals versus what is eI#ected to be when under a normal distribution ## it is liKe a normal #robability #lot (i! it !orms a line then the residuals looK liKe they came !rom a ## normal distribution ## #er!orms the <ha#iro-LilK test which uses Eo 0 (#o#. is some normal dist.) versus ## Ea0 (#o#. is not a normal dist.) and reMects Eo when the test statistic L is small.

> boI#lot(res) > 88norm(res)

> sha#iro.test(res)

%% &egin Lac' of Fit !est %%


> anova(lm(wt~ht)) > cht < - !actor(ht) > anova(lm(wt ~ cht)) ## Bives the usual (#revious) 6>@F6 !or this data - #roduces <<N and <<=C('4(.& !or our ## eIam#le ## Ohanges the numerical variable ht into a variable that uses these numbers as labels and not ## as numbers ## #roduces an anova with res#ect to the !actor variable - :he residuals << !or this model ## is actually the <<A= in the lacK o! !it test and its mean s8uare is D<A=. Pn this eIam#le, ## <<A= C G4HG.* and D<A= C GH/.H) ## com#utes the << !or lacK o! !it (<<9,) as the <<= o! the original 9< model minus the ## <<A=

> ssl! < - ('4(.& Q G4HG.* > msl! < - ssl!%4

## Dean s8uare !or lacK o! !it - denominator is the (number o! distinct levels o! $) minus *

> !stat < - msl!%GH/.H) ## , test statistic - denominator is the D<A= > #val < - &./ Q #!(!stat, 4, &&) ## #roduces the #-value !or this , test. Pn this eIam#le, 4 is the d! o! D<9, and && is the ## d! o! D<A=.

%% "nd Lac' of Fit !est %%

Loading "(ternal Li$raries:


,rom time to time we need to use libraries o! #rograms stored on the N system but not included in the installation o! N. <u##ose you need to download the set o! #rograms in the library called car. :his can be accom#lished in the !ollowing manner. ,irst you must be in a setting in which you are connected to the internet. Lhen in the N worKs#ace window ty#e0 > install.#acKages(car) ## "ou will have to answer a !ew 8uestions in the #rocess, but it will download onto your com#uter. ## :hen ty#e0 > library(car) ## :his brings the library into your active worKs#ace. "ou should have access to the #rograms in car ## now.

You might also like