[R] Plotting an interaction with error bars
Hello. I need to plot a two-way interaction (5 levels X 3) with error bars. The x.factor will be the five-levels var and the trace.factor will be the three level var. I was able to find functions that draw error bars, but still couldn't find a way to draw an interaction plot that looks like what is common in psychological (and related) journals: the lines (three in my need) are plotted in parallel and to avoid overlapping of the error bars a small jitter is added. I was able to make plotmeans() {from gplots} draw the interaction but with the lines plotted side by side (not parallel), resulting in three panes of one line each. When I tried manually super-imposing (looping a draw of one line for each level in the trace.factor) I got the overall look, but the error bars overlap and are hard to distinguish. Another attempt I made was with ci.plot() {from pda}. Inputting an aov() model resulted in a perfect plot of interaction but which is very limited to handle - I was unable to add a legend, change symbols etc. Does anybody knows if there is a way to manipulate either of the above functions more then what I was able to? Is there another function to draw interaction plots with error bars? Thanks for any help or comment. dror __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting an interaction with error bars
Thank you Jim. brkdn.plot() seems to be just the function I need. Still, I wonder if there are lists of labels of the different measures of central tendency (mct argument) and measures of dispersion (md)? dror - On Wed, Nov 4, 2009 at 7:41 AM, Jim Lemon wrote: > On 11/03/2009 10:15 PM, Dror D Lev wrote: >> >> Hello. >> >> I need to plot a two-way interaction (5 levels X 3) with error bars. >> The x.factor will be the five-levels var and the trace.factor will be >> the three level var. >> >> I was able to find functions that draw error bars, but still couldn't >> find a way to draw an interaction plot that looks like what is common >> in psychological (and related) journals: the lines (three in my need) >> are plotted in parallel and to avoid overlapping of the error bars a >> small jitter is added. >> >> I was able to make plotmeans() {from gplots} draw the interaction but >> with the lines plotted side by side (not parallel), resulting in three >> panes of one line each. When I tried manually super-imposing (looping >> a draw of one line for each level in the trace.factor) I got the >> overall look, but the error bars overlap and are hard to distinguish. >> >> Another attempt I made was with ci.plot() {from pda}. Inputting an >> aov() model resulted in a perfect plot of interaction but which is >> very limited to handle - I was unable to add a legend, change symbols >> etc. >> >> Does anybody knows if there is a way to manipulate either of the above >> functions more then what I was able to? >> Is there another function to draw interaction plots with error bars? >> >> > > Hi Dror, > I think you want to offset the points in a group along the x axis so that > the points and error bars don't overlap. Try introducing an x offset of > maybe 0.1 in your try with plotmeans. To see an example of what this looks > like and how it is done, have a look at brkdn.plot in the plotrix package. > > Jim > > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting an interaction with error bars
Indeed, that seems very convenient. Thanks for your work and for the help. dror - On Wed, Nov 4, 2009 at 12:17 PM, Jim Lemon wrote: > On 11/04/2009 06:36 PM, Dror D Lev wrote: >> >> Thank you Jim. >> >> brkdn.plot() seems to be just the function I need. >> >> Still, I wonder if there are lists of labels of the different measures >> of central tendency (mct argument) and measures of dispersion (md)? >> >> > > Hi Dror, > With brkdn.plot (and several other functions in plotrix) you can use any > existing measure or even roll your own. The only limitation is that the > function has to return a numeric value and must accept an "na.rm" argument. > > Jim > > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting an interaction with error bars
Hello Jim, I have encountered a puzzling pattern of results using brkdn.plot(): > brkdn.plot("Duration","group_val","blck",data=dpp) $mean [,1] [,2] [,3] [,4] [1,] 0.653842 0.5679849 0.477645 0.4250087 [2,] 0.653842 0.5679849 0.477645 0.4250087 [3,] 0.653842 0.5679849 0.477645 0.4250087 [4,] 0.653842 0.5679849 0.477645 0.4250087 [5,] 0.653842 0.5679849 0.477645 0.4250087 (I've omitted the $std.error matrix to save space since the pattern is the same) To verify the pattern of repetitions is not in my data I made another calculation and there is no pattern of repetition: > with(dpp, ftable(by(Duration,list(group_val,blck),mean,na.rm=T))) 1 2 3 4 0.015 0.7493519 0.5125178 0.4538040 0.3498578 0.02 0.5583320 0.6234520 0.5014860 0.5001596 0.028 0.5446862 0.3600077 0.2951988 0.2842502 0.035 0.4396780 0.2335916 0.1992378 0.1661076 0.045 0.2711094 0.1305614 0.0969594 0.1491294 Is there a way to make brkdn.plot() to present this data? Thank you, dror ----- On Wed, Nov 4, 2009 at 12:21 PM, Dror D Lev wrote: > Indeed, that seems very convenient. > Thanks for your work and for the help. > > dror > > - > > On Wed, Nov 4, 2009 at 12:17 PM, Jim Lemon wrote: > > On 11/04/2009 06:36 PM, Dror D Lev wrote: > >> > >> Thank you Jim. > >> > >> brkdn.plot() seems to be just the function I need. > >> > >> Still, I wonder if there are lists of labels of the different measures > >> of central tendency (mct argument) and measures of dispersion (md)? > >> > >> > > > > Hi Dror, > > With brkdn.plot (and several other functions in plotrix) you can use any > > existing measure or even roll your own. The only limitation is that the > > function has to return a numeric value and must accept an "na.rm" > argument. > > > > Jim > > > > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Plotting power function to practice data
Hello, I have practice data of motor action in the format: S | Cond. | Time +-+ 01 | c | 1.23 01 | nc| 0.89 02 | c | 2.15 02 | nc| 1.80 . I want to look at the learning curves graphically. I will appreciate pointers to relevant functions / packages. Thanks in advance, dror [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting power function to practice data
Hi, I tried to be comprehensive but Jim's comment is indeed in place. I have data of a practice experiment where people practice a certain motor task and time-to-completion was recorded. Appropriately, the time measure declines as practice goes on. And, again appropriately, the relation seems to be non-linear. It looks like y=1/x but much less steep. I understand that the general case of such functions is called power-function. So what I'm looking for is something like abline() with a power-function fit (rather then a linear one). Jim is also correct writing that I would like to have separate fits for 'c' and 'nc'. Of course this can be achieved using subset() but, as Dennis wrote, some graphic functions include an option to graph the data by groups. Thanks again for any tip or reference. dror - On Tue, Dec 29, 2009 at 10:56 PM, jim holtman wrote: > It would help if you were more explicit on what you were trying to look > at. I assume that you want two curves ('c' and'nc') on one graphs and you > can do that with the basic plot routines, or you can use the 'lattice' > package, but without knowing what you are looking for, it is hard to tell. > > On Tue, Dec 29, 2009 at 3:33 PM, Dror D Lev wrote: > >> Hello, >> >> I have practice data of motor action in the format: >> >> S | Cond. | Time >> +-+ >> 01 | c | 1.23 >> 01 | nc| 0.89 >> 02 | c | 2.15 >> 02 | nc| 1.80 >> . >> >> I want to look at the learning curves graphically. >> >> I will appreciate pointers to relevant functions / packages. >> >> Thanks in advance, >> dror >> >>[[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem that you are trying to solve? > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting power function to practice data
Thank you Peter. nls() & predict() seems to do the job. dror - On Wed, Dec 30, 2009 at 8:10 PM, Peter Ehlers wrote: > Sounds like you might want to use nls() to fit the data > and then use either curve() or predict() to do the > plotting. > > -Peter Ehlers > > Dror D Lev wrote: > >> Hi, >> >> I tried to be comprehensive but Jim's comment is indeed in place. >> >> I have data of a practice experiment where people practice a certain motor >> task and time-to-completion was recorded. >> Appropriately, the time measure declines as practice goes on. And, again >> appropriately, the relation seems to be non-linear. It looks like y=1/x >> but >> much less steep. >> I understand that the general case of such functions is called >> power-function. >> >> So what I'm looking for is something like abline() with a power-function >> fit >> (rather then a linear one). >> >> Jim is also correct writing that I would like to have separate fits for >> 'c' >> and 'nc'. >> Of course this can be achieved using subset() but, as Dennis wrote, some >> graphic functions include an option to graph the data by groups. >> >> Thanks again for any tip or reference. >> >> dror >> >> - >> >> On Tue, Dec 29, 2009 at 10:56 PM, jim holtman wrote: >> >> It would help if you were more explicit on what you were trying to look >>> at. I assume that you want two curves ('c' and'nc') on one graphs and >>> you >>> can do that with the basic plot routines, or you can use the 'lattice' >>> package, but without knowing what you are looking for, it is hard to >>> tell. >>> >>> On Tue, Dec 29, 2009 at 3:33 PM, Dror D Lev >>> wrote: >>> >>> Hello, >>>> >>>> I have practice data of motor action in the format: >>>> >>>> S | Cond. | Time >>>> +-+ >>>> 01 | c | 1.23 >>>> 01 | nc| 0.89 >>>> 02 | c | 2.15 >>>> 02 | nc| 1.80 >>>> . >>>> >>>> I want to look at the learning curves graphically. >>>> >>>> I will appreciate pointers to relevant functions / packages. >>>> >>>> Thanks in advance, >>>> dror >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> __ >>>> R-help@r-project.org mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html< >>>> http://www.r-project.org/posting-guide.html> >>>> >>>> and provide commented, minimal, self-contained, reproducible code. >>>> >>>> >>> >>> -- >>> Jim Holtman >>> Cincinnati, OH >>> +1 513 646 9390 >>> >>> What is the problem that you are trying to solve? >>> >>> >>[[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> >> > -- > Peter Ehlers > University of Calgary > 403.202.3921 > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Structure of Anova for obtaining sig. corrected for departure from sphericity
Dear helpers, I have a 2x2 mixed design with two "groups" (between-subjects) and two presentation-types (within-subjects). The difference between groups is in the order of manipulations: group.CD having first a block of present.type.C and then a block of present.type.D, each block containing 31 trials. group.DC having first a block of present.type.D and then a block of present.type.C, each block containing 31 trials. I'm mostly interested in the interaction group:present.type - to see if the order of presentation has an influence on performance time (the dependent measure). The fundamental ANOVA model is: >aov(t.total~group*present.type+Error(subj/present.type), data=dat2) Unfortunately, the standard deviations of the different interaction-cells differ markedly (~20, ~9, ~9, ~18) so it seems that a correction for departure from sphericity is appropriate. I'm trying to achieve that using the Anova function from the car package, but fail to understand what should be the model, the idata and idesign parameter (which seem to be required for a repeated-measures analysis design). I'd appreciate any help with getting the right required model and parameters (idata, idesign and icontrasts). Thanks, dror My data set is stored in a data.frame with the following columns: > names(dat2) [1] "t.total" "t.err""trial.num""subj" "present.type" [6] "ord" "group""dat.name" and the only table of results I'm getting is: > summary(Anova(m.tmp)) Sum Sq Df F value Pr(>F) Min. : 17810 Min. : 1.0 Min. :69.01 Min. :1.278e-15 1st Qu.: 41232 1st Qu.:108.8 1st Qu.:69.01 1st Qu.:1.278e-15 Median : 64654 Median :216.5 Median :69.01 Median :1.278e-15 Mean : 64654 Mean :216.5 Mean :69.01 Mean :1.278e-15 3rd Qu.: 88075 3rd Qu.:324.2 3rd Qu.:69.01 3rd Qu.:1.278e-15 Max. :111497 Max. :432.0 Max. :69.01 Max. :1.278e-15 NA's : 1.00 NA's :1.000e+00 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Doing a mixed-ANOVA after accounting for a covariate
Dear r helpers, I would like to look at the interaction between two two-level factors, one between and one within participants, after accounting for any variance due to practice (31 trials in each of two blocks) in the task. It seems to require treating practice as a covariate. All the examples I noticed for handling covariates (i.e. ANCOVA, including the ones in Faraway's "Practical regression and anova using r") use lm(), but this doesn't handle repeated-measures. I thought of a solution in the form of first running a regression on the covariate: > cov.accnt = lm (myMeasure ~ myCovMeasure, data=dat) and then run the aov() on the residuals: > m.aov = aov (cov.accnt$residuals ~ withinVar*betweenVar + Error(subj/withinVar, data=dat) Does it seem to be a valid answer to my problem? Is there an existing function that can do this (perhaps more appropriately)? Thank you for any help, dror [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Doing a mixed-ANOVA after accounting for a covariate
Thank you David, for the reference to Dalgaard's paper in Rnews_2007-2. Unfortunately I don't seem to have the mathematical-statistical sophistication required to adapt the example in Dalgaard's paper for my case. I hope someone can suggest a less-mathematical direction for solution. Thanks again, dror On Sun, Dec 26, 2010 at 3:59 PM, David Winsemius wrote: > > On Dec 26, 2010, at 7:42 AM, Dror D Lev wrote: > > Dear r helpers, >> >> I would like to look at the interaction between two two-level factors, one >> between and one within participants, after accounting for any variance due >> to practice (31 trials in each of two blocks) in the task. >> It seems to require treating practice as a covariate. >> >> All the examples I noticed for handling covariates (i.e. ANCOVA, including >> the ones in Faraway's "Practical regression and anova using r") use lm(), >> but this doesn't handle repeated-measures. >> > > See if Dalgaard's piece in R-News offers better guidance: > > http://www.r-project.org/doc/Rnews/Rnews_2007-2.pdf > > > > >> I thought of a solution in the form of first running a regression on the >> covariate: >> >>> cov.accnt = lm (myMeasure ~ myCovMeasure, data=dat) >>> >> >> and then run the aov() on the residuals: >> >>> m.aov = aov (cov.accnt$residuals ~ withinVar*betweenVar + >>> >> Error(subj/withinVar, data=dat) >> >> Does it seem to be a valid answer to my problem? >> >> Is there an existing function that can do this (perhaps more >> appropriately)? >> >> Thank you for any help, >> dror >> > -- > > David Winsemius, MD > West Hartford, CT > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Doing a mixed-ANOVA after accounting for a covariate
Thank you David and Rich for your reply. I will try to get more concrete with an artificial example that replicates the structure of the data sets I'm analyzing. It will include analysis suggestions based on what I was able to figure out from the demo(MMC.apple). At the end there is my current puzzlement, if the logic of the analyses I suggest is correct: ## For creating an artifical data set: # Trial-number: 31 trials per (pt-)block, repeated for 2 blocks per each of 10 human subjects tn=rep(1:31, times=20) # Presentation type: 2 blocks of 31 trials for each of 10 human subjects pt.1=rep(c('C','D'), each=31) pt=rep(pt.1, times=10) # The 10 subjects can be divided into 2 groups (of 5 each), distinct in presentation order grp.1=rep('DC', times=62*5) grp.2=rep('CD', times=62*5) grp=c(grp.1,grp.2) # Ten subjects, each going through 2 (pt-)blocks of 31 trials each s=rep(1:10, each=62) # For the sake of the example we can randomly create a random vector of results: r=runif(620) dat=data.frame("subj"=factor(s),"group"=factor(grp), "present.type"=factor(pt), "trial.num"=tn, "res.v"=r) ## The analysis dilema: # I would like to look at the interaction between the two factors, # alleviating the variance due to trial repeation within each block. # That is, to have trial.num as a covariate. # "simply" matching my case to the example in demo(MMC.apple), from the HH package, will be: m.simple=aov(res.v ~ present.type + group + trial.num, data=dat, x=TRUE) # and the coding for trial.num in the model=matrix is an integer: m.simple$x[1:10,] # (which means it is treated as a continuous var?) # But, actually, in my case, present.type is repeated for each subj and # trial.num is repeated for each level of present.type. # So I figured the model should be: m.mixed=aov(res.v ~ present.type * group * trial.num + Error(subj/(present.type + (present.type/trial.num))), data=dat, x=TRUE) # but now I cannot get the model matrix: m.mixed$x[1:10,] # == NULL # and when I compare a reduced model, not including the covariate-trial.num: m.mixed.red=aov(res.v ~ present.type * group + Error(subj/present.type), data=dat, x=TRUE) # I get the same results for the factors, which means that including the covariate-var (trial.num) # in this way does not affect the evaluation of the other factors. # So, what I wonder now is why including trial.num does not affect the results for the other factors, # and is there a way to use such a covariate to remove any "masking" it can induce on the effects # of interest. # (To explain a bit what I'm expecting: # intuitively I understand that there is variance in the res.v that is due to the covariate-var # which is not related to, and so can not be explained by, the factors of interest. # So, efectively, it inflates the noise-variance that serve to evaluate the significance # of the variance-of-interest # - I hope that by removing (some of) this covariance-var related noise-variance # I will be able to get effects evaluations that are more "purely" related to the res.v # - increasing the levels of significance where there is actual effect) Thank you for any further help. dror -- On Sun, Dec 26, 2010 at 9:45 PM, RICHARD M. HEIBERGER wrote: > Dror, > > Please look at the > demo(MMC.apple) > in the HH package > > install.packages("HH") ## if you don't already have it. > library(HH) > demo(MMC.apple) > > Please reply to the list if there are further queries. > > Rich > > On Sun, Dec 26, 2010 at 7:42 AM, Dror D Lev wrote: > >> Dear r helpers, >> >> I would like to look at the interaction between two two-level factors, one >> between and one within participants, after accounting for any variance due >> to practice (31 trials in each of two blocks) in the task. >> It seems to require treating practice as a covariate. >> >> All the examples I noticed for handling covariates (i.e. ANCOVA, including >> the ones in Faraway's "Practical regression and anova using r") use lm(), >> but this doesn't handle repeated-measures. >> >> I thought of a solution in the form of first running a regression on the >> covariate: >> > cov.accnt = lm (myMeasure ~ myCovMeasure, data=dat) >> >> and then run the aov() on the residuals: >> > m.aov = aov (cov.accnt$residuals ~ withinVar*betweenVar + >> Error(subj/withinVar, data=dat) >> >> Does it seem to be a valid answer to my problem? >> >> Is there an existing function that can do this (perhaps more >> appropriately)? >> >> Thank you for any help, >> dror >> >>[[alternative HTML version deleted]] >> >> _