Unformatted text preview:

Introduction to Matlab III 1 As mentioned before you need to build your own X matrix for performing regression. For example, to perform the regression in S-Plus/R with lm(BTUIn ~ BTUOut * damper, data=furnace) > summary(furnace.lm2) Call: lm(formula = BTUIn ~ BTUOut * Damper, data = furnace) Residuals: Min 1Q Median 3Q Max -3.089841 -0.219128 0.003471 0.303370 1.812594 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.16470 0.30425 0.541 0.590 BTUOut 0.92183 0.02755 33.458 <2e-16 *** DamperTVD 0.01959 0.43054 0.045 0.964 BTUOut:DamperTVD -0.01717 0.03839 -0.447 0.656 Residual standard error: 0.5576 on 86 degrees of freedom Multiple R-Squared: 0.9635, Adjusted R-squared: 0.9622 F-statistic: 756.3 on 3 and 86 DF, p-value: < 2.2e-16 > anova(furnace.lm2) Analysis of Variance Table Response: BTUIn Df Sum Sq Mean Sq F value Pr(>F) BTUOut 1 704.66 704.66 2266.7663 <2e-16 *** Damper 1 0.61 0.61 1.9483 0.1664 BTUOut:Damper 1 0.06 0.06 0.2000 0.6559 Residuals 86 26.73 0.31Introduction to Matlab III 2 In Matlab you would need to do something like >> xmat = [ones(size(damper)) BTUOut damper BTUOut.*damper]; >> [b, bint, r, rint, stats] = regress(BTUIn, xmat) >> b b = 0.1647 0.9218 0.0196 -0.0172 >> bint bint = -0.4401 0.7695 0.8671 0.9766 -0.8363 0.8755 -0.0935 0.0591 >> stats stats = 0.9635 756.3049 0Introduction to Matlab III 3 There is another function that makes doing regression a bit easier in that is will automate some of the setup. The function is regstat. The form of the function is >> regstats(y ,data ,'model') data is a matrix with each column corresponding to a different variable. 'model' can be one of the following strings 'linear' Includes constant and linear terms (default). 'interaction' Includes constant, linear, and cross product terms. 'quadratic' Includes interactions and squared terms. 'purequadratic' Includes constant, linear, and squared terms.Introduction to Matlab III 4 The basic form of the function will bring up a dialog box asking with values you want returned by the function. One you checked off the variables you want returned, click Calculate Now and a second box will come up asking where you want to store themIntroduction to Matlab III 5 >> regstats(BTUIn,[BTUOut damper],'interaction') >> beta2 beta2 = 0.1647 0.9218 0.0196 -0.0172 >> sqrt(diag(covb2)) ans = 0.3043 0.0276 0.4305 0.0384 Note that the same values are being returned as R. For programming purposes, there is a second form of the function call that skips the dialog boxes. >> stattest = regstats(BTUIn,[BTUOut damper], 'interaction', {'beta' 'covb' 'mse'}) To access a component of the output you need to give a command like >> stattest.covb ans = 0.0926 -0.0080 -0.0926 0.0080 -0.0080 0.0008 0.0080 -0.0008 -0.0926 0.0080 0.1854 -0.0159 0.0080 -0.0008 -0.0159 0.0015Introduction to Matlab III 6 To help construct dummy variables to include categorical factors into your linear model, there is the dummyvar function. It will take a matrix, where the columns correspond to the different factors, and create a set of dummy variables which could tend be used in a regression >> group group = 1 1 1 2 1 3 2 1 2 2 2 3 >> D = dummyvar(group) D = 1 0 1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 1 0 0 0 1 0 1 0 0 1 0 0 1Introduction to Matlab III 7 Analysis of Variance There are 3 functions for peforming Analysis of Variance in Matlab. anova1: Balanced 1-way ANOVA anova2: Balanced 2-way ANOVA anovan: Unbalanced and higher way ANOVA In the first 2 functions there must be the same number of observations for each treatment combination. If this doesn’t hold, you must use anovan. The following example is taken from a study by Hogg and Ledolter (1987) of bacteria counts in shipments of milk. >> hogg hogg = 24 14 11 7 19 15 7 9 7 24 21 12 7 4 19 27 17 13 7 15 33 14 12 12 10 23 16 18 18 20 The desired form for the data is for each column to correspond to a different factor levels. To for this example, column 1 is 6 observations from shipment 1, column 2 is shipment 2, etc.Introduction to Matlab III 8 >> [pa1,tbla1,statsa1] = anova1(hogg) pa1 = 1.1971e-004 % p-value from F test tbla1 = % ANOVA table 'Source' 'SS' 'df' 'MS' 'F' 'Prob>F' 'Columns' [ 803.0000] [ 4] [200.7500] [9.0076] [1.1971e-004] 'Error' [ 557.1667] [25] [ 22.2867] [] [] 'Total' [1.3602e+003] [29] [] [] [] statsa1 = % ANOVA study information gnames: [5x1 char] n: [6 6 6 6 6] source: 'anova1' means: [23.8333 13.3333 11.6667 9.1667 17.8333] df: 25 s: 4.7209 In addition to the saved output, anova1 generates two figures, one with the ANOVA table and the other with boxplots of the observationsIntroduction to Matlab III 9 1 2 3 4 551015202530ValuesColumn Number Given the highly significant F test, it would be nice to figure which shipments are different. This can be done with the multcompare function. This function takes the ANOVA study information from the anova functions and compares the different groups, taking account of the different number of tests involved.Introduction to Matlab III 10 >> [ca1,ma1] = multcompare(statsa1) ca1 = 1.0000 2.0000 2.4953 10.5000 18.5047 1.0000 3.0000 4.1619 12.1667 20.1714 1.0000 4.0000 6.6619 14.6667 22.6714 1.0000 5.0000 -2.0047 6.0000 14.0047 2.0000 3.0000 -6.3381 1.6667 9.6714 2.0000 4.0000 -3.8381 4.1667 12.1714 2.0000 5.0000 -12.5047 -4.5000 3.5047 3.0000 4.0000 -5.5047 2.5000 10.5047 3.0000 5.0000 -14.1714 -6.1667 1.8381 4.0000 5.0000 -16.6714 -8.6667 -0.6619 ma1 = 23.8333 1.9273 13.3333 1.9273 11.6667 1.9273 9.1667 1.9273


View Full Document

HARVARD STAT 335 - Mat lab

Documents in this Course
Load more
Download Mat lab
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Mat lab and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Mat lab 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?