Re: [R] How to multiply all dataframe rows by another dataframe's columns
matrix(Y[,2]%o%X[,2],ncol=1) On Wednesday 29 August 2007 03:21 pm, Luke Neraas wrote: Hello, I have two data frames, X and Y, with two columns each and different numbers of rows. # creation of data frame X Loc1.alleles - c(1,5,6,7,8) Loc1.Freq- c(0.35, 0.15, 0.05, 0.10, 0.35) Loc1 - cbind( Loc1.alleles,Loc1.Freq) X- data.frame(Loc1) #creation of data frame Y Loc2.alleles - c(1,4,6,8) Loc2.Freq - c(0.35, 0.35, 0.10, 0.20) Loc2 - cbind(Loc2.alleles, Loc2.Freq) Y - data.frame (Loc2) I would like a flexible way to multiply each element of second column of the X data frame by each element of the second column of the Y data frame. example of what the operation need to do: X[1,2]*Y[,2] ; X[2,2]*Y[,2].X[4,2]*Y[,2] I have worked on a variety of for loops to get this to work without success. The final result should look like a column like this all_X[,2] * all_Y[,2] 0.1225 0.1225 0.0350 0.0700 0.0525 0.0525 0.0150 0.0300 0.0175 0.0175 0.0050 0.0100 0.0350 0.0100 0.0200 0.1225 0.1225 0.0350 0.0700 any help would be greatly appreciated Luke Neraas [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] Looking for a cleaner way to implement a setting certain indices of a matrix to 1 function
Hi Mark, Is this of any help? resMat-function(K=6,lag=2,ncol=3*K){ X-matrix(0,K,ncol) X[,1:(K*lag)]-diag(K) return(X) } Cheers, Anders. On Tuesday 08 May 2007 11:21 am, Leeds, Mark (IED) wrote: I wrote an ugly algorithm to set certain elements of a matrix to 1 without looping and below works and you can see what The output is below the code. K-6 lagnum-2 restrictmat-matrix(0,nrow=K,ncol=K*3) restrictmat[((col(restrictmat) - row(restrictmat) = 0 ) (col(restrictmat)-row(restrictmat)) %% K == 0)]-1 restrictmat[,(lagnum*K+1):ncol(restrictmat)]-0 restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000100 0 0 0 0 0 0 0 0 0 [2,]010000010 0 0 0 0 0 0 0 0 0 [3,]001000001 0 0 0 0 0 0 0 0 0 [4,]000100000 1 0 0 0 0 0 0 0 0 [5,]000010000 0 1 0 0 0 0 0 0 0 [6,]000001000 0 0 1 0 0 0 0 0 0 For lagnum equals 1 , it also works : restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000000 0 0 0 0 0 0 0 0 0 [2,]010000000 0 0 0 0 0 0 0 0 0 [3,]001000000 0 0 0 0 0 0 0 0 0 [4,]000100000 0 0 0 0 0 0 0 0 0 [5,]000010000 0 0 0 0 0 0 0 0 0 [6,]000001000 0 0 0 0 0 0 0 0 0 But I am thinking that there has to be a better way particularly because I'll get an error if I set lagnum to 3. Any improvements or total revampings are appreciated. The number of columns will always be a multiple of the number of rows So K doesn't have to be 6. that was just to show what the commands do. thanks. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ 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] is there a way to let R do smart matrix-vector operation?
Hi Michael, Try: A-as.vector(B) Cheers, Anders. On Monday 06 March 2006 01:10 pm, Michael wrote: Hi all, I want to substract vector B from A's each column... how can R do that smartly without a loop? A=matrix(c(2:7), 2, 3) A [,1] [,2] [,3] [1,]246 [2,]357 B=matrix(c(1, 2), 2, 1) B [,1] [1,]1 [2,]2 A-B Error in A - B : non-conformable arrays [[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 __ 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
Re: [R] Automatic differentiation (was: Re: D(dnorm...)?)
I can confirm that AD Model Builder is used at the Danish Institute for Fisheries Research, and by fisheries people in and all around the Pacific. On a few occasions I have solved a likelihood optimization problem in AD Model Builder, and then wrapped the binary in an R-package, with data read in, graphics and such, for others to use, as the binaries can be freely distributed. I can recommend this approach to anyone with problems where 'optim' is struggling. I would love to see AD in R, but I think it would be difficult to combine many of the things that makes R so wonderful and flexible to work with, like logical indexing, with AD, but I could be wrong. Anders. On Thursday 26 January 2006 05:55 am, Hans Skaug wrote: Dear Alberto, There are fisheries people also in Europe using AD Model Builder (Denmark and England for instance), but you are probably right that it is more widespread in North America. There is also effort going on where people try to make assessment models written in ADMB callable from R. best regards, hans think AD Model Builder is mainly used for fisheries assessment in North America and, it seems, also in Australia. In Europe, R is still the de-facto standard for fisheries assessment. However, I'd like to support Bill Venables' suggestion. I've been resisting to adopt AD model builder, or to start using again that other system not unlike R, mainly because of the licence price and because I really like R as a tool for almost everything. But an AD function would really make a huge difference for my work. There are free tools that can be used to perform AD on C or Fortran code (e.g. http://www.autodiff.org). One of the difficulties to use them with R is the need to translate the R code into C of Fortran code, but probably there are many other problems that I'm not able to see. Alberto -- _ Hans Julius Skaug Department of Mathematics University of Bergen Johannes Brunsgate 12 5008 Bergen Norway __ 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-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
Re: [R] GLMs: Negative Binomial family in R?
Hi, Also consider using the function supplied in the post: https://stat.ethz.ch/pipermail/r-help/2005-March/066752.html for fitting negative binomial mixed effects models. Cheers, Anders. On Tue, 5 Apr 2005, Achim Zeileis wrote: On Tue, 5 Apr 2005 11:20:37 -0600 [EMAIL PROTECTED] wrote: Greetings R Users! I have a data set of count responses for which I have made repeated observations on the experimental units (stream reaches) over two air photo dates, hence the mixed effect. I have been using Dr. Jim Lindsey's GLMM function found in hisrepeated measures package with the poisson family. My problem though is that I don't think the poisson distribution is the right one to discribe my data which is overdispersed; the variance is greater than the mean. I have read that the negative binomial regression models can account for some of the differences among observations by adding in a error term that independent of the the covariates. glm.nb() from package MASS fits negative binomial GLMs. I haven't yet come across a mixed effects model that can use the negative binomial distribution. For known theta, you can plug negative.binomial(theta) into glmmPQL() for example. (Both functions are also available in MASS.) I'm not sure whether there is also code available for unknown theta. If any of you know of such a function - I will certainly look forward to hearing from you! Additionally, if any of you have insight on zero-inflated data, and testing for this, I'd be interested in your comments too. I'll post a summary of your responses to this list. Look at package zicounts for zero-inflated Poisson and NB models. For these models, there is also code available at http://pscl.stanford.edu/content.html which also hosts code for hurdle models. hth, Z Best Regards, Nadele Flynn, M.Sc. candidate. University of Alberta __ 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-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-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
Re: [R] Negative binomial GLMMs in R
Dear List, I have tried the program (Linux version) supplied to fit the negative binomial mixed model. It seems to work really well and converge fast. Since this is apparently a model that is difficult to fit with what is presently in R, and more difficult to fit with other standard tools, it would be nice to have this solution wrapped into a real R-package with documentation and all. I for one would like to encourage the authors to make such a package available (and would be willing to help if requested). I don't know if such a package could be posted on CRAN since it relies on a closed source library for automatic differentiation, but if that is a problem it could at least be made available on a personal web-page. Cheers, Anders. On Wed, 23 Mar 2005 [EMAIL PROTECTED] wrote: Dear R-users, A recent post (Feb 16) to R-help inquired about fitting a glmm with a negative binomial distribution. Professor Ripley responded that this was a difficult problem with the simpler Poisson model already being a difficult case: https://stat.ethz.ch/pipermail/r-help/2005-February/064708.html Since we are developing software for fitting general nonlinear random effects models we thought this might be an interesting challenge. We contacted Professor Ripley who kindly directed us to the epilepsy data in Venables Ripley section 10.4 (4th ed.). While VB did not actually fit a negative binomial to these data they did refer to evidence of overdispersion in the response. Fortunately Booth et al. (2003) did attempt to fit this model with a negative binomial which gave us something to which we could compare our results. Booth et al. fitted two forms of the model a simpler one and a more complicated model. They reported some difficulty fitting the more complicated model. We found that we could reliably fit (MLE) both the complicated and simpler model in 20 seconds or less (although the more complicated turns out to be overparameterized) Using the random effects module of AD Model Builder we have developed a shared library (Windows dll) that can be called from R via the driver function glmm.admb(). The function can be downloaded from http://otter-rsch.com/admbre/examples/nbmm/nbmm.html The two models of Booth et al are fit by the commands: glmm.admb(y~Base*trt+Age+Visit,random=~1,group=subject,data=epil2) glmm.admb(y~Base*trt+Age+Visit,random=~Visit,group=subject,data=epil2) I will be happy to receive feedback on the function glmm.admb(). Best regards, Hans Skaug Reference: Booth J.G.; Casella G.; Friedl H.; Hobert J.P, Negative binomial loglinear mixed models. Statistical Modelling, October 2003, vol. 3, no. 3, pp. 179-191 __ 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-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
Re: [R] R: lags
How about: y-cumsum(c(0,rnorm(100))) On Tue, 10 Feb 2004, allan clark wrote: hi all how does one simulate a random walk process? i.e y(0)=0 y(t)=y(t-1)+ e(t) where e(t) is normal(0,1) say. Regards allan __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] runif and sample with reproducibility
Hi Eric, How about: set.seed(1234567) sample(1:100,10) [1] 57 72 90 3 74 46 9 81 95 78 set.seed(1234567) sample(1:100,10) [1] 57 72 90 3 74 46 9 81 95 78 Cheers, Anders. On Mon, 5 Jan 2004, Eric ESPOSITO wrote: Hello, I already sent such an email before Christmas, but nobody answered, so here is my problem: I would like to sample a population but the result needs to be reproducible, using 'runif' or 'sample' is the good way to do it but I can't manage to make the results reproducible even with the 'set.seed' function. My aim is that th call to 'sample(1:100,10)' gives always the same result, how can I do that? Thanks! Eric Esposito __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] code efficiency, extr. info from list
Try using lapply() For instance like: val-unlist(lapply(test, function(x)x$value)) You can also extend this by having your function return everything you need from the list. Cheers, Anders. On Tue, 21 Oct 2003, Tord Snall wrote: Dear all, I try extracting information from a list with several levels, but I would be happy for recommendation on writing more efficient code: h0- seq(0,100, by = 20); expo- seq(0.1, 0.5, l = 5) grid- expand.grid(h0, expo) test- apply(grid, 1, pcp, point.data = as.points(dat[,c(x,y)]), poly.data = studyarea) test[1] $1 $1$par s2 rho 1.815343e-06 2.358788e-02 $1$value [1] 144.346 $1$counts function gradient 65 NA $1$convergence [1] 0 $1$message NULL I want to put the results together: val- c(test[[1]]$value, test[[2]]$value, test[[3]]$value, test[[4]]$value...) s2- c(test[[1]]$par[1], test[[2]]$par[1], test[[3]]$par[1], test[[4]]$par[1]...) rho- ... funct- grad- . useful.df- as.data.frame(cbind(val, s2), F) However, as you can see dim(grid) [1] 30 2 the call rows val- c(test[[1]]$value, test[[2]]$value, test[[3]]$value, test[[4]]$value...) etc. will be long. I would thus be happy for help with writing this code more efficient (and I know will benefit from this knowing how to do this in the future). Thanks in advance! Sincerely, Tord --- Tord Snäll Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villavägen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: [EMAIL PROTECTED] Check this: http://www.vaxtbio.uu.se/resfold/snall.htm! __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Place a graphic into an R-plot
Hi, You need to install the pixmap package to do that. After that is installed the following lines illustrate how to use it. library(pixmap) x - read.pnm(system.file(pictures/logo.ppm, package = pixmap)[1]) layout(matrix(c(1,1,2,3),2,2)) plot(rnorm(100)) plot(rnorm(100)) plot(x) Cheers, Anders. On Thu, 18 Sep 2003, Morrison, Gordon wrote: I have a graphic image in a file (say a *.jpeg or *.png file) and want to put it into a plot. I have segmented the plot area by means of the layout function and successfully plotted my data in the appropriate segments. I cannot find how to put my graphic image onto the same plot. Searching the archives has shed little light on my challenge. Many thanks in anticipation. Gordon version platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major1 minor7.1 year 2003 month06 day 16 language R ** This is a commercial communication from Commerzbank AG.\ \ T...{{dropped}} __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help