lm(formula, data)

where *formula* is the structural formula that specifies the model and *data*
is the data frame containing the data (omitted if the data is not stored in a data frame).
The following example uses the dataframe *car.test.frame* described in the section
on data frames.

>A list of the components of an lm object and of the arguments which can be passed through the lm() function can be found at the end of this section.fuel.fit_lm(100/Mileage ~ Weight + Disp., car.test.frame)> fuel.fit Call: lm(formula=100/Mileage~Weight + Disp.,data=car.test.frame) Coefficients: (Intercept) Weight Disp. 0.4789733 0.001241421 0.0008543589 Degrees of freedom: 60 total; 57 residual Residual standard error: 0.3900812

The lm object *fuel.fit* can be used as an argument to other functions to obtain summaries
of the data or to extract specific information about the model. The function summary()
gives a more detailed description of the model than the one obtained by printing out the
lm object. The components of an lm.summary object are listed at the end of this section.

>Specific information about the model can be printed using the functions coef(), resid() and fitted(). The same information can be obtained using fuel.fit$coef, fuel.fit$resid, and fuel.fit$fitted.fuel.summary_summary(fuel.fit)> fuel.summary Call: lm(formula=100/Mileage~Weight + Disp.,data = car.test.frame) Residuals: Min 1Q Median 3Q Max -0.8109 -0.2559 0.01971 0.2673 0.9812 Coefficients: Value Std. Error t value Pr(>|t|) (Intercept) 0.4790 0.3418 1.4014 0.1665 Weight 0.0012 0.0002 7.2195 0.0000 Disp. 0.0009 0.0016 0.5427 0.5895 Residual standard error: 0.3901 on 57 degrees of freedom Multiple R-Squared: 0.7438 F-statistic: 82.75 on 2 and 57 degrees of freedom, the p-value is 0 Correlation of Coefficients: (Intercept) Weight Weight -0.8968 Disp. 0.4720 -0.8033

> coef(fuel.fit) (Intercept) Weight Disp. 0.4789733 0.001241421 0.0008543589 > formula(fuel.fit2) 100/Mileage ~ Weight + Disp. + Type * the formula() function extracts the model formula from the lm object fuel.fit2The update() function is used to modify the model formula used in

>By default, contrasts used for factors are the Helmert parametrization. Here, the coefficient forfuel.fit2_lm(update(fuel.fit, . ~ . + Type))> fuel.fit2 Call: lm(formula = update(fuel.fit, . ~ . + Type)) Coefficients: (Intercept) Weight Disp. Type1 Type2 2.960617 4.16396e-05 0.007594073 -0.1452804 0.09808411 Type3 Type4 Type5 -0.1240942 -0.04358047 0.1915062 Degrees of freedom: 60 total; 52 residual Residual standard error: 0.3139246

> **fuel.fit2_lm(update(fuel.fit, . ~ . + C(Type, sum)))**

* the first argument to the C() function specifies the factor and the second argument specifies the contrasts. >Suppose we have another data frame calledX11();par(mfrow=c(2,1));plot(fuel.fit)* the plot() function has a default method for lm objects * two plots are produced: one of the response against the fitted values, with the y=x line superimposed, the other of the absolute value of the residuals against their fitted values View plots

>predict(fuel.fit, new.cars)* all the variables used in the modelfuel.fitmust also be in the data framenew.cars

where the objects are extracted from an lm object (lmf) as described below:QuantityExpressionMeaningStandardized e/(s*(1-h)^.5)Residuals with equal varianceResiduals Studentized e/si*(1-h)^.5)Use si as standard errorResiduals DFBETAS bi/(si %o% xxi^.5)The change in the coefficients,scaled by the standard errorfor the coefficientsDFFIT h*e/(1-h)The change in the fitted valuewhen that observation is droppedDFFITS h^.5*e/(si*(1-h))Change in fitted values,standardized to variance 1

> lms_summary(lmf) > lmi_lm.influence(lmf) >Some of these quantities can be obtained from the function ls.diag(). The function ls.diag() takes as its argument a list like the output of lsfit(). The function lsfit() is similar to the lm() function except that the formula is not specified using a formula object, but rather the model and response matrices are passed on as arguments to the function. These matrices can be created from the variables or extracted from an lm object.e_residuals(lmf) >s_lms$sigma >si_lmi$sigma >xxi_diag(lms$cov.unscaled) >h_lmi$hat >bi_coef(lmf) - coef(lmi)

>An lm.object is a list with components (eg.: fuel.fit$coefficients):fuel.model_lm(fuel.fit,x=T,y=T)* an lm object is constructed which saves the model matrix and the response >fuel.ls_lsfit(fuel.model$x, fuel.model$y, intercept=F)* the model matrix and the response are then passed on to the lsfit() function * the argument intercept=F is specified because the intercept is already included in the model matrix * weights can also be specified using the argument wt= >ls.print(fuel.ls)* ls.print() is similar to summary() Residual Standard Error = 0.3901, Multiple R-Square = 0.7438 N = 60, F-statistic = 82.7518 on 2 and 57 df, p-value = 0 coef std.err t.stat p.value Intercept 0.4790 0.3418 1.4014 0.1665 Weight 0.0012 0.0002 7.2195 0.0000 Disp. 0.0009 0.0016 0.5427 0.5895 >fuel.diag_ls.diag(fuel.ls)* ls.diag() computes regression diagnostics * the function returns a list with components: std.devresidual standard deviationhatvector containing the diagonal of the hat matrixstd.resstandardized residualsstud.resstudentized residualscooksCook's distance for each observationdfitschange in fitted value when each observation is deletedcorrelationcorrelation matrix for the parameter estimatesstd.errstandard errors of the parameter estimatescov.unscaledunscaled covariance matrix for the parameter estimates

coefficients residuals fitted.values effectsThe following is a list of other possible arguments to the function lm(), with the default action when not specified, if applicable:orthogonal, single-degree-of-freedom effectsRtriangular facor of the decompositionrankthe computed rankassignlist of assignments of coefficients (and effects) to theterms of the modeltermsan object summarizing the formulacallcall that produced the objectcontrastsa list with matrices or vectors of contrasts used to codefactorsdf.residualnumber of degrees of freedom for residualsR.assignsame as assign, for an over-specified model (in terms offull-rank part of model)assign.residualsvector identifying effects assigned to residualsqrqr decomposition object (optional)modelmodel frame (optional)xmodel matrix (optional)yresponse (optional)

weights= *The function summary computes a list with the following components:vector of observation weightssubset= *expression specifying rows to be used*may be logical, numeric (row numbers),or character (row names)na.action=na.fail *a function to filter missing values*default creates an error message,a possible alternative is na.omitwhich deletes observations withmissing valuesmethod="qr" *least squares fitting method to be used*method="model.frame" returns the model.framemodel=F *if T returns the model frame component modelx=F *if T the model matrix is returned in component xy=F *if T the response is returned in component ycontrasts=NULL *a list giving contrasts for all or some of thefactors appearing in the model formula*the elements of the list should be a contrastmatrix, or a function to compute such a matrix

correlationcorrelation coefficient matrix for the coefficientscov.unscaledunscaled covariance matrixdfdegrees of freedom for the model and for the residualscoefficientsa matrix with three columns: coefficients, standard errors,t-statisticsr.squaredthe multiple R-squared statisticfstatisticnumeric vector of length three giving the F test- the first element is the statistic, the last two are the dfresidualsthe model residuals (weighted if applicable)sigmaresidual standard error estimatetermsthe terms object used in fitting this model