Practicesession2HLM多層線性模型講義_第1頁
Practicesession2HLM多層線性模型講義_第2頁
Practicesession2HLM多層線性模型講義_第3頁
Practicesession2HLM多層線性模型講義_第4頁
Practicesession2HLM多層線性模型講義_第5頁
免費預覽已結束,剩余1頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、R-p ractice sessi on 2CS&SS 560Marijtje van DuijnWinter 2006More on worki ng with RMost of you with some exp erie nee in R are already using scri pts, a sort of syn tax files. These can be kept in a simple text editor and you can copy and p aste from them into the R con sole to run a nu mber of comm

2、a nds at the same time. There is a facility within R -through the use of RWin Edt -that you can add to R. Ano ther op ti on is to use text pad (free evaluatio n versi on at ) that you can con figure for R syn tax. See Cori Mar s class no tes about this op ti on(htt p:/www.csde.wash ington .edu/trai

3、ning/courses/csss508/week3.shtml).Con sult the R home page (or one of your fellow stude nts with exp erie nee) if you are in terested in this op ti on.The comma nds used in this sessi on are available as R syn tax file (Sessio n2.R) at the website.Data input and prep aratio nWe use the data used in

4、Sn ijders & Bosker. For a descri pti on see Exa mple 4.1 (p. 46).The data are in the comma separated file SBbookR.csv available under Data on the left side of the class website/vanduijn/560/.Get the data and put them in your worki ng directory. Some compu ters in SAV137

5、have a directory .te mpstat560 that could be used as a worki ng directory.Before we eve n load the data we will attach two p ackages that we will n eed later on. The nlme library is n eeded for the estimati on of (non) lin ear mixed effects models. With functions from the lattice library we will be

6、able to make n ice graphs.library( nl me)library(lattice)Load the datadatasbv-read.table(SBbookR.csv,header=T,se p=,)Attach the dataattach(datasb)You could look at the data with the same comma nd as we used last time datasbYou will find that this is not a very good way to look at the data, the matri

7、x is too large. Also the summary is not very in formative.So it is better to find out about the data, the variable n ames byn ames(datasb)You can find out about the len gth of the datafilele ngth( pup ilNR)Or you can look the first 10 observati ons by datasb1:10,We can get the means per school, by a

8、ggregating over all variables, making the new dataframe schdata schoolsb names(schoolsb)schoolszdatasb$meanscoredatasb$meanIQdatasb$meansesdatasb$IQcdatasb$IQccboxplot(IQschoolNR)You will see that the graph is not clear because of the many (131) schools (not consecutively numbered).For exploration p

9、urposes we therefore take a 10% subsample of all schools. We do this by taking a sample ofunique alscohkonoolsw(n as sampling withoutreplacement), whose numbers are stored in samp25. (Side remark: if you would like to explore all schools, the strategy would be to split the datafile up in smaller por

10、tions.) samp25 samp.25 xyplot(testscoreIQ|schoolNR,data=samp.20,main=Exploring 25 random schools,panel=function(x,y)panel.xyplot(x,y)panel.loess(x,y,span=1)panel.lmline(x,y,lty=2)This produces a graph (beautiful, don t you thinpklo?t)ting testscore against IQfor the 25 selected schools whose number

11、appear in the colored bar above each subgraph (called panel). In each subgraph a scatterplot is created with the xyplot command, a socalled Loess line (locally smoothed regression, here using all of the available data per school-span=1) and the fitted p er-school-regressi on line.If you want no colo

12、r in the graph (for instance if you want to export it, which could also be done by simple copying and pasting to a word document), use the commandtrellis.device(color=F) and redo the Trellis graph.What imp ressi on does the graph give you with respect to the variability of in terce pt and slope?Esti

13、mati ng a multilevel modelFor the estimati on of multilevel models we will use the nlme-library devel oped by Pin heiro & Bates. NLME sta nds for Non Linear Mixed Effects. Mixed Effects refers to models with both fixed and ran dom effects. We will not (yet) use the Non Lin ear modeli ng op ti ons (f

14、or in sta nee logistic or Po iss on) but stay with the lin ear (regressi on) models.The argume nts for the fun ctio n lme() resemble the ones for lm(), used to estimate a regressi on model, but are more elaborate due to the more complex model. It uses the same no tati on to defi ne a model, but also

15、 n eeds a ran dom part Use the hel p fun cti on (?lme also works!) to get a little more in sight into the fun cti on. A more elaborate descri ptio n of the module can be found in Pin heiro & Bates book, but is of course also available at the R website.We start with estimati ng the empty model (table

16、 4.1 in Sn ijders & Bosker).This can be obta ined with the followi ng comma nd. The first part is the socalled fixed part (and could be p receeded by fixed=). The left hand side of the sp ecifies the dependent variable, the right hand side the covariates. Here you only see a 1, which in dicates that

17、 only a con sta nt (=1) is sp ecified, which refers to the in terce pt. The sec ond part could be left out, because we have attached datasb. The third part sp ecifies the ran dom part and looks a bit funny. The right hand side | symbol (called pipe!) indicate over which grouping variable theleftha n

18、dside of the grouping variable is assumed to vary. This means here that the in terce pt varies over schools, and that is just what we want for a ran dom intercept model. The residual (level 1) is default .model.0Rv-lme(testscore1, data=datasb, ran dom=1|schoolNR)summary(model.0R)The out put obta ine

19、d with the summary comma nd n eeds some scruti ny before we can decide whether it has all the eleme nts p rese nt to con struct a table like 4.1. It starts the other way around, by first giving the model fit, apart from the loglikelihood also the AIC and BIC in dices. After rep eati ng their formula

20、, the ran dom effects are give n, not with varia nces but with sta ndard deviati ons (so the square roots). The one bel onging to (In terce pt) is the level 2 (betwee n-school)2varia nee t ; the one bel onging to Residuals, the residual (level 1, with in-school) 2 variance b .The varia nee estimates

21、 can be got directly with the comma nd VarCorr(model.0R)Next the fixed effects are reported, again repeating the formula, here just one, with a t-test, df, and p-value. A s an extra , the dbisotrxipbluottionof the standardized residual error (so with mean 0, and standard deviation 1) is given. It se

22、ems a little skewed to the left. This is not important at all at this moment, since this is not a final model, but if it w ould show funny things that may indicate a problem with the data.If you look even more carefully at the estimates you will see tiny differences in the variance estimates. This i

23、s because the default estimation method in nlme is REML (REsidual Maximum Likelihood). This method produces less biased variance estimates but its deviances (or likelihood values) cannot be used for testing (comparing) different models. Lets therefore also estimate the model withmaximum likelihood.m

24、odel.0model.0$apVarThese are rather different from the ones reported in table 4.1.Here you see that the results are different. This must be due to the different implementation of the ML estimation method. And since these estimates are never used for formal testing, but at the most foreye balling the significance ofthe results, it is not so important.Use the command ?lmeObject to investigate what other functions and attributes are available. For instance fitted values, estimates of the random intercepts, etc. Try out some of these functions if you like.We will now e

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論