Re: [R] Nested Fixed Effects - basic questions
You will have to talk to your local statistician/econometrician. Given that you say you will have three observations per student, your description of the data is incomplete. Therefore, it is impossible to tell what the right approach would be. Assumed that all observations are independent, you would fit fix3 as one dummy/factor variable (not fix1 or fix2), and then you would test linear hypotheses for sums of certain coefficients against sums of other coefficients in your case. Nested effects are what you can specify as random effects, afaik; you don't fit them in fixed-effects analyses (rather you would take the outlined linear-hypothesis approach). As far as I can tell from your post, your understanding of the subject matter is limited. As I said, you will have to get yourself an overview of the topic and search for materials that explain a.) nested effects and b.) the application of nested effects in R (e.g. with the nlme and lme4 libraries). So go ahead and put in the effort. Daniel ps: on a more general note, the R-help list is to help with the implementation in R rather than statistical/econometric questions, especially if these problems are extensive. - cuncta stricte discussurus - _ Von: Jojo Ziggy [mailto:jojo.zi...@yahoo.com] Gesendet: Saturday, September 05, 2009 2:14 PM An: Daniel Malter Betreff: Re: AW: [R] Nested Fixed Effects - basic questions "Nesting would imply something like students nested in class nested in school, where each student is only member of one class and each class only member of one school." Yes, I think this is actually what I have going on - I guess I did not represent that correctly in the data table. So... I gather my data shown properly as nested effects should actually look more like: fix1fix2fix3response 00016.596 00016.564 00122.665 00122.801 01216.000 01215.930 01321.739 01321.628 12416.260 12416.128 12522.969 12523.245 13614.687 13614.635 13722.954 13723.345 In this case, how do I tell R that the effects are nested, as opposed to unnested? And how do I specify that they are fixed and not random effects? Will it somehow know automatically? I will in the end have 3 observations for each "student" - so 24 datapoints in this example. Certainly nothing close to 160, however. Thanks, jojo _ From: Daniel Malter To: Jojo Ziggy ; r-help@r-project.org Sent: Friday, September 4, 2009 4:37:14 PM Subject: AW: [R] Nested Fixed Effects - basic questions In R and experimental or mixed-model terminology, your lm model specifies fixed effects. As long as each data row represents a unique subject, you are fine with lm. If not, you have to account for the repeated measurement of subjects and will need other methods (potentially involving random effects). In your model, you perform a dummy variable OLS (ordinary least squares) regression. Mixed-effects models that allow for a combination of fixed and random effects or random-effects-only analyses are most prominently done with Doug Bates's nlme or lme4 libraries (though, there are more libraries that allow for mixed-effects modeling). Google for some manuals. Further, your effects are not nested. If each row stands for a different unit of observation (e.g., subject), and if subjects are randomized into treatments fix1, fix2, and fix3, then you have a completely randomized factorial design (CRF). Nesting would imply something like students nested in class nested in school, where each student is only member of one class and each class only member of one school. Then your fix columns should look like (with 8 students nested in 4 classes nested in 2 schools): fix1 fix2 fix3 111 211 321 421 532 632 742 842 Thus, your effects are really not nested (at least not for what you show us as the data). What you can do to figure out whether not only fix1 and fix2 have an independent effect, but also whether fix1 and fix2 interact in their effect on your response, you can include interaction effects. However, if the data you provided is your entire dataset, you will likely overfit the model and inflate the standard errors if you include all possible interactions (eats up 4 degrees of freedom) along with the direct effects and intercept (also 4 degrees of freedom), given that your provided data has only 16 observations. Example: #Simulate data fix1=rep(0:1,each=8) fix2=rep(c(0,0,1,1),4) fix3=rep(0:1,8) e=rnorm(16) #Dependent variable y=-1*fix1+2*fix2+1*fix3-0.75*fix1*fix2+0.9*fix1*fix3-2*fix2*fix3+1.5*fix1*fi x2*fix3+e #Run regression and show output reg0=lm(y~(fix1+fix2+fix3)^3) #all interactions up to three-way summary(reg0) #note that this is not very insightful with so few observation
Re: [R] Nested Fixed Effects - basic questions
In R and experimental or mixed-model terminology, your lm model specifies fixed effects. As long as each data row represents a unique subject, you are fine with lm. If not, you have to account for the repeated measurement of subjects and will need other methods (potentially involving random effects). In your model, you perform a dummy variable OLS (ordinary least squares) regression. Mixed-effects models that allow for a combination of fixed and random effects or random-effects-only analyses are most prominently done with Doug Bates's nlme or lme4 libraries (though, there are more libraries that allow for mixed-effects modeling). Google for some manuals. Further, your effects are not nested. If each row stands for a different unit of observation (e.g., subject), and if subjects are randomized into treatments fix1, fix2, and fix3, then you have a completely randomized factorial design (CRF). Nesting would imply something like students nested in class nested in school, where each student is only member of one class and each class only member of one school. Then your fix columns should look like (with 8 students nested in 4 classes nested in 2 schools): fix1 fix2 fix3 111 211 321 421 532 632 742 842 Thus, your effects are really not nested (at least not for what you show us as the data). What you can do to figure out whether not only fix1 and fix2 have an independent effect, but also whether fix1 and fix2 interact in their effect on your response, you can include interaction effects. However, if the data you provided is your entire dataset, you will likely overfit the model and inflate the standard errors if you include all possible interactions (eats up 4 degrees of freedom) along with the direct effects and intercept (also 4 degrees of freedom), given that your provided data has only 16 observations. Example: #Simulate data fix1=rep(0:1,each=8) fix2=rep(c(0,0,1,1),4) fix3=rep(0:1,8) e=rnorm(16) #Dependent variable y=-1*fix1+2*fix2+1*fix3-0.75*fix1*fix2+0.9*fix1*fix3-2*fix2*fix3+1.5*fix1*fi x2*fix3+e #Run regression and show output reg0=lm(y~(fix1+fix2+fix3)^3) #all interactions up to three-way summary(reg0) #note that this is not very insightful with so few observations #Same as above, just with a 10-times larger simulated dataset fix1=rep(0:1,each=80) fix2=rep(c(0,0,1,1),40) fix3=rep(0:1,80) e=rnorm(160) y=-1*fix1+2*fix2+1*fix3-0.75*fix1*fix2+0.9*fix1*fix3-2*fix2*fix3+1.5*fix1*fi x2*fix3+e reg1=lm(y~(fix1+fix2+fix3)^3) summary(reg1) #160 observations works quite well already #coef estimates are within the margin of error of the true coefficients The second example shows that the approach to use OLS to model your data is fine if your error distribution (the distribution of e in the simulated data) is normal. Daniel - cuncta stricte discussurus - -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Jojo Ziggy Gesendet: Friday, September 04, 2009 1:17 PM An: r-help@r-project.org Betreff: [R] Nested Fixed Effects - basic questions Hi R people, I have a very basic question to ask - I'm sorry if it's been asked before, but I searched the archives and could not find an answer. All the examples I found were much more complicated/nuanced versions of the problem - my question is much more simple. I have data with multiple, nested fixed effects (as I understand it, fixed effects are specified by the experimental design while random effects are measured) and one continuous response variable. All the fixed effects are catagorical. e.g. fix1fix2fix3response 00016.260 00016.128 00122.969 00123.245 01014.687 01014.635 01122.954 01123.345 10019.866 10019.589 10122.748 10122.817 11017.861 11017.872 11122.925 11123.138 I was thinking I could use a linear model to determine whether any of the nested fixed effects or their interactions effect the response, but I could not determine how to specify whether effects were fixed or random, and how to specify nesting. For example: lm(response~ fix1+fix2+fix3) The above, as I understand it, simply asks whether the effects fix1 through fix4 have an effect on the response. However, in reality my experimental design has multiple levels of nesting: fix1(fix2(fix3(fix4))) So, how do I do this? To specify nesting, do I need to use another type of model such as lmer or glm? I also don't know whether the above example is specifying whether the effects are fixed or random - how do I do this? Thanks very much, Jojo [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listin