[R] initalizing and checking validity of S4 classes
Dear Martin and Seth, Thanks a million for explanations and suggestions. I find myself still in the process of learning S4 system. Can you possibly suggest some material that I could use in this process? Anything apart from Programming with data book? I also know the notes written by John Chambers and I also have Fritz Leisch's slides from the first UseR!, all three available on the Web. Do you have any suggestions in that matter? Thanks a lot in advance! Best, Michal Martin Morgan mtmorgan at fhcrc.org writes: Hi Michal -- Add validObject to your initialize method: Actually, a call to valid object is part of the default initialization method which has been masked. A different solution, which might have some other benefits is to delegate back to the default method using callNextMethod. So you could do: setMethod(initialize, someclass, function(.Object, v=numeric(0), l=character(0)) { # strip the vector names cv - v cl - l names(cv) - NULL names(cl) - NULL callNextMethod(.Object=.Object, v=cv, l=cl) } ) Here are two interpretations of this. (1) using 'initialize' means that you are taking control of the initialization process, and hence know when you need to call validObject. Yes. Anytime you specialize a method you must take responsibility for any less specific methods. In this case, the default 'initialize' does object validation. So if you want validation, you either need to do it directly or invoke the default method. (2) Responsibility for object validity is ambiguous -- does it belong with 'new', 'initialize', or a 'constructor' that the programmer might write? This is particularly problematic with R's copy semantics, where creating transiently invalid objects seems to be almost necessary (e.g., callNextMethod() in 'initialize' might initialize the inherited slots of the object, but the object itself is of the derived class and could well be invalid 'invalid' after the base class has finished with initialize). This is a good point. It suggests that, at least, one must initialize all non-inherited slots to valid values _before_ calling the next method. + seth : :: ::: Note that my e-mail address has changed to m.j.bojanowski at uu dot nl ::: Please update you address books accordingly. Thank you! :: : Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht The Netherlands m.j.bojanowski at uu dot nl http://www.fss.uu.nl/soc/bojanowski/ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] initalizing and checking validity of S4 classes
Dear useRs and wizaRds, I am currently developing a set of functions using S4 classes. On the way I encountered the problem exemplified with the code below. For some reason the 'validity' method does not seem to work, i.e. does not check for errors in the specification of the slots of the defined class. Any hints? My understanding of the whole S4 system was that validity checks are made *after* class initialization. Is that correct? Thanks a lot in advance! PS. Session info: R version 2.5.1 (2007-06-27) i386-pc-mingw32 locale: LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Polish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base -b-e-g-i-n--r--c-o-d-e- setClass( someclass, representation(v=numeric, l=character), prototype( v=numeric(0), l=character(0) ) ) setMethod(initialize, someclass, function(.Object, v=numeric(0), l=character(0)) { # strip the vector names cv - v cl - l names(cv) - NULL names(cl) - NULL rval - .Object [EMAIL PROTECTED] - cv [EMAIL PROTECTED] - cl rval } ) # at this point this should be OK o - new(someclass, v=1:2, l=letters[1:3]) o # check validity f - function(object) { rval - NULL if( length([EMAIL PROTECTED]) != length([EMAIL PROTECTED]) ) rval - c( rval, lengths dont match) if( is.null(rval) ) return(TRUE) else return(rval) } # this should return error description f(o) # define the validity method setValidity( someclass, f) # this should return an error new(someclass, v=1:2, l=letters[1:3]) # but it doesn't... -e-n-d--r--c-o-d-e- Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 m.j.bojanowski at uu dot nl http://www.fss.uu.nl/soc/bojanowski/ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] ODP: Updating R version
Is R not loading the packages at all (like they were never installed), or perhaps it is complaining that they were build on the older version of R? You have to be more specific here about what happens etc. For now try running update.packages and see if it fixes the problem. I would also suggest keeping all the additional packages (beyond R base installation) in the separate library tree -- less work every time you update R. See ?library and ?.libPaths hth, Michal : :: ::: Note that my e-mail address has changed to m.j.bojanowski at uu dot nl ::: Please update you address books accordingly. Thank you! :: : Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 m.j.bojanowski at uu dot nl http://www.fss.uu.nl/soc/bojanowski/ -Wiadomo¶æ oryginalna- Od: [EMAIL PROTECTED] w imieniu Raghu Naik Wys³ano: So 2007-06-02 23:13 Do: r-help@stat.math.ethz.ch Temat: [R] Updating R version A quick question. I am trying to understand how I could move the installed packages in my R 2.3 version to the newly installed R 2.5 version, without having to install all the packages again. I copied the files under the old library subdirectory to the new library subdirectory. But still the newer version is not recognizing the packages that were copied over. Thanks. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] tapply
I'm not sure what is the 'pvalue' function (it's not found in base nor stats packages) but this should give you what you want: # some example re - rnorm(100) reg - rep(1:3, length=100) ast - rep(1:2, length=100) tapply( re, list(reg, ast), function(v) shapiro.test(v)$p.value ) # or neater by defining a function p.shapiro - function(v) shapiro.test(v)$p.value tapply( re, list(reg, ast), p.shapiro ) hth, michal Hello, I want to conduct normality test to a series of data and get the p-value for each subset. I am using the following codes, but it does not work. tapply(re, list(reg, ast), pvalue(shapiro.test)) Could anyone give me some advice? Many thanks. __ R-help@stat.math.ethz.ch 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] random effects in lmer
Look here for the answer: https://stat.ethz.ch/pipermail/r-help/2006-May/094765.html : :: ::: Note that my e-mail address has changed to m.j.bojanowski at uu dot nl ::: Please update you address books accordingly. Thank you! :: : Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 m.j.bojanowski at uu dot nl http://www.fss.uu.nl/soc/bojanowski/ -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rina Miehs Sent: Friday, June 01, 2007 12:52 PM To: r-help@stat.math.ethz.ch Subject: [R] random effects in lmer Hello i have a model with two random effects. i have used lmer (package lme4) on the model. To find the predictions of the random effects used ranef(). How can i get p-values on theese predictions?? Thanks Rina [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] Help with optim
Hi, Unfortunately I don't think it is possible to do exactly what you want, but: If the numbers reported by 'optim' to the console are enough for you, then consider using 'capture.output'. Below I used the example from 'optim' help page, because I could not use yours directly. hth, Michal # -b-e-g-i-n---R---c-o-d-e- # this is from the 'optim' example fr - function(x) { ## Rosenbrock Banana function x1 - x[1] x2 - x[2] 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 } # and now optim-ize capturing the output to 'out' and the results to 'o' out - capture.output( o - optim( c(-1.2, 1), fr, method=BFGS, control=c(REPORT=1, trace=1)) ) # 'out' is a character vector storing every line as a separate element out # 'o' is returned by optim o # to get a grip on the values you could use for example 'strsplit' and then # extract neccessary info optimout - function(out) { # split by spaces l - strsplit(out, ) # just return the numbers rval - sapply(l[-length(l)], function(x) x[length(x)] ) as.numeric(rval) } x - optimout(out) x plot(x) # -e-n-d---R---c-o-d-e- -Wiadomo¶æ oryginalna- Od: [EMAIL PROTECTED] w imieniu Anup Nandialath Wys³ano: Wt 2007-05-29 08:33 Do: r-help@stat.math.ethz.ch Temat: [R] Help with optim Dear Friends, I'm using the optim command to maximize a likelihood function. My optim command is as follows estim.out - optim(beta, loglike, X=Xmain, Y=Y, hessian=T, method=BFGS, control=c(fnscale=-1, trace=1, REPORT=1)) Setting the report=1, gives me the likelihood function value (if i'm correct) at each step. The output from running this is as follows initial value 3501.558347 iter 2 value 3247.277071 iter 3 value 3180.679307 iter 4 value 3157.201356 iter 5 value 3156.579887 iter 6 value 3017.715292 iter 7 value 2993.349538 iter 8 value 2987.181782 iter 9 value 2986.672719 iter 10 value 2986.658620 iter 11 value 2986.658266 iter 12 value 2986.658219 iter 13 value 2986.658156 iter 13 value 2986.658156 iter 13 value 2986.658135 final value 2986.658135 converged I just wanted to know if there was any way I could get the value of each iteration into an object. At present it is dumped on the screen. But is there a way to get hold of these values through an object?? Thanks in advance sincerely Anup - The fish are biting. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] How to transform matrices to ANOVA input datasets?
Hi Josh, Consider what follows to convert your data and estimate the models. I am not sure however, what do you want to do after the models are estimated, so my suggestions stop at this point. HTH, Michal # -b-e-g-i-n---R---c-o-d-e- # first i make some data that is similar to yours so i can practice on it m - matrix(rnorm(7*6), ncol=6, nrow=7, dimnames=list(1:7, paste( rep(c(col, trt), each=3), 1:3, sep=)) ) dataHP - list(m, m, m) # the first 7x6 matrix in the list dataHP[[1]] # function that converts a 7x6 matrix 'x' to a data frame just as you want it f - function(x) { data.frame( yield=as.numeric(x), block=rep( c(1:3, 1:3), each=7 ), treat=rep( c(col, trt), each=7*3 ), position=rep(1:7, 6) ) } # the data frame f(dataHP[[1]]) # process the list of matrices by applying 'f' to every component # consequently, 'd' is a list of data frames d - lapply(dataHP, f) # now, to calculate AOV for every data set you could again use 'lapply', e.g.: models - lapply(d, function(x) aov( yield ~ treat*position, data=x) ) # the result is a list of models models[[1]] # -e-n-d---R---c-o-d-e- *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update your address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Wednesday, March 14, 2007 7:12 PM To: r-help@stat.math.ethz.ch Subject: [R] How to transform matrices to ANOVA input datasets? Hello, R experts, I have a list called dataHP which has 30 elements (m1, m2, ..., m30). Each element is a 7x6 matrix holding yield data from two factors experimental design, with treatment in column, position in row. For instance, the element 20 is: dataHP[[20]] col1 col2 col3 trt1 trt2trt3 [1,] 22.0 20.3 29.7 63.3 78.576.4 [2,]102.4 92.3 72.2 199.2201.1218.9 [3,] 18.8 20.8 22.9 106.2148.4147.6 [4,] 14.5 17.2 15.6 120.1115.8124.6 [5,] 31.9 28.3 22.8 157.9192.3160.6 [6,] 98.2147.3102.5 628.8577.0643.0 [7,]174.9217.5 188.66 453.5491.1409.8 My goal is to find which element in the list has significant yield difference among the position. So my first question is how to transform the matrix to ANOVA input dataset which is: yield block treat position 22 1 col 1 102.4 1 col 2 18.81 col 3 14.51 col 4 31.91 col 5 98.21 col 6 174.9 1 col 7 20.32 col 1 92.32 col 2 20.82 col 3 17.22 col 4 28.32 col 5 147.3 2 col 6 217.5 2 col 7 29.73 col 1 72.23 col 2 22.93 col 3 15.63 col 4 22.83 col 5 102.5 3 col 6 188.66 3 col 7 63.31 trt 1 199.2 1 trt 2 106.2 1 trt 3 120.1 1 trt 4 157.9 1 trt 5 628.8 1 trt 6 453.5 1 trt 7 78.52 trt 1 201.1 2 trt 2 148.4 2 trt 3 115.8 2 trt 4 192.3 2 trt 5 577 2 trt 6 491.1 2 trt 7 76.43 trt 1 218.9 3 trt 2 147.6 3 trt 3 124.6 3 trt 4 160.6 3 trt 5 643 3 trt 6 409.8 3 trt 7 So I can contrasts(position) and do ANOVA like this: fit1-aov(yield~treat*position) summary(fit1, split=list(position=1:10), expand.split= T) Finally I can find the significant element in the list if there is any significant contrast among the position. So my second question is how to apply this ANOVA to each element in the list? Your help will be highly appreciated!! Josh __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] Highlight overlapping area between two curves
If PDF is OK, you can use the 'alpha' argument in colors, i.e.: pdf( file.pdf) p - seq(0.2,1.4,0.01) x1 - dnorm(p, 0.70, 0.12) x2 - dnorm(p, 0.90, 0.12) plot(range(p), range(x1,x2), type=n) polygon(p, x1, col = rgb(1,0,0, .5),lwd=4, lty=2) polygon(p, x2, col = rgb(0,0,1, .5),lwd=4) dev.off() hth, Michal *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update your address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Nguyen Dinh Nguyen Sent: Tuesday, March 13, 2007 5:20 AM To: r-help@stat.math.ethz.ch Subject: [R] Highlight overlapping area between two curves Dear R helpers, I have a graph as following; I would like to highlight the overlapping area between the two curves. Do you know how to do this? Thank you in advance for your help. Nguyen ###START x1 - rnorm(1, 0.70,0.12) x2 - rnorm(1, 0.90,0.12) d1 - density(x1) d2 - density(x2) plot(range(d1$x,d2$x), range(d1$y, d2$y), type = n, xlab = X value, ylab = Probability Density ) lines(d1, col = red,lwd=4, lty=2) lines(d2, col = blue,lwd=4) ##END CODE __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] 'substitute' question
I do not understand why you play with 'substitute' instead of something like this: # CAUTION: this will work only for bivariate case # plotmaking function plotModel - function(m) { x - m$model$x y - m$model$y r2 - summary(m)$r.squared plot(x,y) abline(m) text( min(x), max(y), paste(And the R^2 is, round(r2,3)), pos=4) invisible(NULL) } # your data x - rnorm(100) y - x + rnorm(100) lm1 - lm(y~x) # make the plot plotModel(lm1) *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update your address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of remko duursma Sent: Tuesday, March 13, 2007 2:02 PM To: r-help@stat.math.ethz.ch Subject: [R] 'substitute' question # I use this code to label a graph with the R2: # graph x - rnorm(100) y - x + rnorm(100) lm1 - lm(y~x) plot(x,y) # label R2text - substitute(paste(R^2, = ,r2),list(r2=r2)) text(1,-3,R2text, col=red) # i have modified this a bit, so that i have a vector with other labels, each of which # will be labelled on the graph. Example: texts - c(And the R2 is, R2text) x - c(-2,-2) y - c(2,1) for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col=blue) # As you can see the label R2 = 48.7 remains at paste(R^2, = , 48.7) # What to do? Thanks for your help, Remko ..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~- Remko Duursma Post-doctoral researcher Dept. Forest Ecology University of Helsinki, Finland _ With tax season right around the corner, make sure to follow these few simple tips. __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] 'substitute' question
I'm sorry for my first post as I did not properly understood your problem. The reason why you were getting paste(R^2, = , 48.7) instead of the properly formatted R^2=48.7 is that in creating 'texts' in your code you were mixing character string with expressions resulting in 'texts' being a list not a vector. The only modification to make is to index texts with double square brackets as below # the figure x - rnorm(100) y - x + rnorm(100) lm1 - lm(y~x) plot(x,y) # label R2text - substitute(paste(R^2, = ,r2),list(r2=r2)) text(1,-3,R2text, col=red) # i expanded your list of labels to four elements... texts - c(And the R2 is, R2text, third label, fourth label) # texts is a list not an atomic vector class(texts) str(texts) # ... and added more coordinates accordingly x - c(-2,-2, -1, -1) y - c(2,1, 2, 1) # here 'texts[[i]]' instead of 'texts[i]' for(i in 1:length(texts))text(x[i],y[i], texts[[i]],pos=4, col=blue) hth, Michal *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update your address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of remko duursma Sent: Tuesday, March 13, 2007 2:02 PM To: r-help@stat.math.ethz.ch Subject: [R] 'substitute' question # I use this code to label a graph with the R2: # graph x - rnorm(100) y - x + rnorm(100) lm1 - lm(y~x) plot(x,y) # label R2text - substitute(paste(R^2, = ,r2),list(r2=r2)) text(1,-3,R2text, col=red) # i have modified this a bit, so that i have a vector with other labels, each of which # will be labelled on the graph. Example: texts - c(And the R2 is, R2text) x - c(-2,-2) y - c(2,1) for(i in 1:length(texts))text(x[i],y[i],texts[i],pos=4, col=blue) # As you can see the label R2 = 48.7 remains at paste(R^2, = , 48.7) # What to do? Thanks for your help, Remko ..-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~-.-~- Remko Duursma Post-doctoral researcher Dept. Forest Ecology University of Helsinki, Finland _ With tax season right around the corner, make sure to follow these few simple tips. __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] inconsistent behaviour of add1 and drop1 with a weighted linear model
Hmmm, this is the same I got add1(model,.~.+x1+x2) Single term additions Model: y ~ 1 Df Sum of SqRSSAIC none 74.541 24.703 x1 144.377 30.164 14.942 x2 136.619 37.922 17.918 R version 2.4.1 (2006-12-18) i386-pc-mingw32 locale: LC_COLLATE=Polish_Poland.1250;LC_CTYPE=Polish_Poland.1250;LC_MONETARY=Po lish_Poland.1250;LC_NUMERIC=C;LC_TIME=Polish_Poland.1250 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update your address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Tuesday, March 13, 2007 5:35 PM To: Jenny Hodgson Cc: r-help@stat.math.ethz.ch Subject: Re: [R] inconsistent behaviour of add1 and drop1 with a weighted linear model Jenny Hodgson wrote: Dear R Help, I have noticed some inconsistent behaviour of add1 and drop1 with a weighted linear model, which affects the interpretation of the results. I have these data to fit with a linear model, I want to weight them by the relative size of the geographical areas they represent. __ ___ example yx1 x2 weights 1 -4.546477 0.1859556 50.0 0.9466022 2 1.484246 0.4740497 29.88000 1.3252430 3 2.203681 0.8594264 16.9 0.9466022 4 1.943163 0.8713360 42.11765 2.7766997 5 1.886473 0.9006082 19.0 0.9466022 6 1.795393 0.8455183 23.68421 1.1674760 7 1.878077 0.5641396 35.0 0.8203885 8 -4.215484 0.4356858 58.75000 0.4417477 9 1.993339 0.5440061 19.28571 0.8519420 10 1.560869 0.6285066 19.54545 0.8203885 11 2.761535 0.7017720 15.8 0.1262136 12 0.995959 0.4638751 0.0 0.9466022 13 -4.516514 0.2794199 77.85714 0.8834954 sum(example$weights) [1] 13.0 model-lm(y~1,data=example,weights=weights) add1(model,.~.+x1+x2) Single term additions Model: y ~ 1 Df Sum of SqRSSAIC none 94.000 27.719 x1 155.290 38.710 18.185 x2 158.630 35.371 17.012 Which version of R??! I get (2.4.1 on Fedora 6): add1(model,.~.+x1+x2) Single term additions Model: y ~ 1 Df Sum of SqRSSAIC none 74.541 24.703 x1 144.377 30.164 14.942 x2 136.619 37.922 17.918 __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] sink with R-code
Hi, How about 'capture.output'? You could also put the code in a separate file and sink the sourcing it with the echo=TRUE argument, for example (input.r contains the commands). sink(output.txt) source(input.r, echo=TRUE) sink() HTH, Michal *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update you address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Cooley, Scott K Sent: Thursday, March 08, 2007 1:37 AM To: r-help@stat.math.ethz.ch Subject: [R] sink with R-code I have the same question that Eusebio had: Is there a function similar to sink that redirect also R code to a file that is: sink(R001) x - c(2,-6,-4,8,5,4,1,3,4,-9,0,1) A - matrix(x, ncol=3) A A.prima - t(A) A.prima dim(A) dim(A.prima) sink() create a file R001 with contents: -- [,1] [,2] [,3] [1,]254 [2,] -64 -9 [3,] -410 [4,]831 [,1] [,2] [,3] [,4] [1,]2 -6 -48 [2,]5413 [3,]4 -901 [1] 4 3 [1] 3 4 and what I want is a file with: x - c(2,-6,-4,8,5,4,1,3,4,-9,0,1) A - matrix(x, ncol=3) A [,1] [,2] [,3] [1,]254 [2,] -64 -9 [3,] -410 [4,]831 A.prima - t(A) A.prima [,1] [,2] [,3] [,4] [1,]2 -6 -48 [2,]5413 [3,]4 -901 -- Any hint will be appreciated Eusebio Scott K. Cooley Statistical Sciences, K6-08 Battelle--Pacific Northwest Division Pacific Northwest National Laboratory P.O. Box 999 Richland, WA 99352 Phone: 509-375-3604 FAX: 509-375-2604 Email: [EMAIL PROTECTED] [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] Searching and deleting elements of list
Hi, A little bit shorter perhaps: # deletion mydata2 - lapply( mydata, function(x) x[ !(x %in% A) ] ) # insert A again mydata2[[1]] - A mydata2 [[1]] [1] aaa bbb ccc ddd eee [[2]] [1] vvv ooo zzz [[3]] [1] sss jjj ppp [[4]] character(0) Please note that if all elements are deleted (as in 'mydata2[[4]]') you get 'character(0)' instead of 'NULL'. You could fix that by lapply(mydata2, function(x) if(identical(x, character(0))) NULL ) or by (recomennded): f - function(x) # perform replacements etc. { rval - x[ !(x %in% A) ] if ( identical(rval, character(0)) ) return(NULL) else return(rval) } mydata2 - lapply( mydata, f ) # apply mydata2[[1]] - A # insert A mydata2 PS. Powodzenia! :) *** Note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update you address books accordingly. Thank you! _ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of jastar Sent: Thursday, March 08, 2007 12:31 PM To: r-help@stat.math.ethz.ch Subject: [R] Searching and deleting elements of list Hi, I have a problem. Please, look at example and try to help me!! A-c(aaa,bbb,ccc,ddd,eee) B-c(vvv,ooo,aaa,eee,zzz,bbb) C-c(sss,jjj,ppp,ddd) D-c(bbb,ccc) mydata=list(A,B,C,D) I want to find and delete from 'mydata' all elements which occur in A (except A). I mean after operation: mydata[[1]] [1] aaa bbb ccc ddd eee mydata[[2]] [1] vvv ooo zzz mydata[[3]] [1] sss,jjj,ppp mydata[[4]] NULL My list have about 1 subelements (each contains several strings) so using loops is senseless. Thank's for all replies and sorry for my English (I hope you understand what I'm talking about) :-) -- View this message in context: http://www.nabble.com/Searching-and-deleting-elements-of-list-tf3368489. html#a9372270 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] ODP: Plotting a broken line?
Hi Aldi, Yet another way to do what you want. 'd' is your data frame. You cannot easily modify the attributes of the lines however... # just to set up coordinates plot( d$Location, d$otherinfo, type=n) # plot the lines invisible(by(d, d$indicator, function(dd) lines(dd$Location, dd$otherinfo))) * *** Please note that my e-mail address has changed to [EMAIL PROTECTED] *** Please update you address books accordingly. Thank you! * Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski/ -Wiadomo¶æ oryginalna- Od: [EMAIL PROTECTED] w imieniu Aldi Kraja Wys³ano: ¦r 2007-03-07 17:21 Do: r-help@stat.math.ethz.ch Temat: [R] Plotting a broken line? Hi, Is there a smart way in the R graphs to create a line that is broken in intervals based on the indicator given below. following is a small test graph Location,indicator,otherinfo 1.2,1,2.2 2.5,1,2.5 3.7,1,2.3 20.1,2,4.3 22.5,2,5.2 25.0,2,3.4 27.3,2,2.2 35.1,3,3.4 37.0,3,7.2 38.0,3,6.1 40.1,3,5.4 52.9,3,3.3 Right now in the plot the line is continuous, but I would like to have it broken based on the indicator. If the line of the plot reaches the last observation of indicator=1 then the line needs to stop; the next line will start at location 22.5 and continue up top 27.3; the next line goes from 35.1 up to 52.9. x-read.table(file='c:\\aldi\\testgraph.csv',sep=',',header=T) x Location indicator otherinfo 1 1.2 1 2.2 2 2.5 1 2.5 3 3.7 1 2.3 4 20.1 2 4.3 5 22.5 2 5.2 6 25.0 2 3.4 7 27.3 2 2.2 8 35.1 3 3.4 9 37.0 3 7.2 10 38.0 3 6.1 11 40.1 3 5.4 12 52.9 3 3.3 plot(x$Location,x$indicator,type='l',xlim=c(0,max(x$Location)),ylim=c(0,max(x$indicator,x$otherinfo))) points(x$Location,x$otherinfo) TIA, Aldi -- __ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] using non-ASCII strings in R packages
Hello dear useRs and wizaRds, I am currently developing a package that will enable to use administrative map of Poland in R plots. Among other things I wanted to include region names in proper Polish language so that they can be used in creating graphics etc. I am working on Windows and when I build the package it is complaining about non-ASCII characters R code files. I was wondering what would be the best way to properly implement them in a platform-independent way so that they can be used in computations as well as in producing PS, PDF and other graphic output. Unfortunately I have a limited knowledge of encoding schemes etc. Is it OK to include them in Windows-1250 encoding (default for Polish locale, as far as I know)? I believe this problem is frequently confronted for other non-latin1 languages. If it is not the way to go, I would be very grateful for suggestions. Thanks in advance and kind regards, Michal Bojanowski Michal Bojanowski ICS / Department of Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] http://www.fss.uu.nl/soc/bojanowski/ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] problem with hist() for 'times' objects from 'chron' package
Hello dear useRs and wizaRds, I encountered the following problem using the hist() method for the 'times' classes from package 'chron'. You should be able to recreate it using the code: library(chron) # pasted from chron help file (?chron) dts - dates(c(02/27/92, 02/27/92, 01/14/92, 02/28/92, 02/01/92)) class(dts) hist(dts) # which yields: # Error in axis(side, at, labels, tick, line, pos, outer, font, lty, lwd, : #'label' is supplied and not 'at' # In addition: Warning messages: # 1: histo is not a graphical parameter in: plot.window(xlim, ylim, log, asp, ...) # 2: histo is not a graphical parameter in: title(main, sub, xlab, ylab, line, outer, ...) The plot is produced, but there are no axes. As far as it goes for the warnings I looked in the sources and I think they are caused by hist.times() in package 'chron' which is calling the barplot() with the histo=TRUE, whereas neither barplot(), plot.window(), title() nor axis() seem to accept this argument. Am I doing something wrong or there is something not right with the hist.times method? Kind regards, Michal PS: I'm using R 2.3.1 on Windows XP and: sessionInfo() Version 2.3.1 (2006-06-01) i386-pc-mingw32 attached base packages: [1] methods stats graphics grDevices utils datasets [7] base other attached packages: chron 2.3-3 library(help=chron) Package: chron Version: 2.3-3 Date: 2006-05-09 Author:S original by David James [EMAIL PROTECTED], R port by Kurt Hornik [EMAIL PROTECTED]. Maintainer:Kurt Hornik [EMAIL PROTECTED] Description: Chronological objects which can handle dates and times Title: Chronological objects which can handle dates and times Depends: R (= 1.6.0) License: GPL Packaged: Fri May 12 09:31:49 2006; hornik Built: R 2.3.0; i386-pc-mingw32; 2006-05-13 12:21:51; windows ~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~ Michal Bojanowski ICS / Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] rearranging data frame rows
Hi Fede, How about using merge()? For example: n - letters[1:10] d1 - data.frame( n=n, x1=rnorm(10) ) d2 - data.frame( n=sample(n), x2=rnorm(10)) d1 d2 merge(d1,d2) Is this what you had in mind? HTH, Michal Hi All, I have two data frames. The first contains data about a number of individuals, coded in the first column with a name, in an order I find convenient. The second contains different data about the same indivduals, in a different order. Both data frame have the individual names in the first column. I need to reorder the second data frame so the rows are rearranged in the same manner as the fist. How? I cannot turn the individual names in a numeric vairable with as.numeric(data1[,1]), because the two data frames are subset of different data, so the the factor levels are way off between the two. I think I need to actually use the names as a index. Cheers, Fede ~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~ Michal Bojanowski ICS / Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html