Re: [R] Need help using lattice
Ha David, I do not know. It must be that your AprtoDec object is different from month.abb. Anyhow it should contain the labels for all twelve months! If I copy your command in an interactive session I just get the message that AprtoDec is not defined. I understand that you type the commands interactively in an R session? I usually prefer using an editor to write the program, and execute with a > source("dotplot.R",echo=TRUE) command. I like an editor with syntax highlighting and shows matching brackets. I have attached my dotplot.R to this mail. Gerrit Op 09-02-2021 om 21:14 schreef Parkhurst, David F.: When I enter your line: df <- expand.grid(site=c("een","twee","drie","vier","vijf"), month=factor(3:12,levels=1:12, labels=month.abb, ordered=TRUE)) I get + signs in front of month and label, as expected, and it follows with < When I convert that to use my own variable names to this: df <- expand.grid(site=c("CrCr","NFSC","MFSC","SFSC","LMO","MCE","MUE","MLE"), monames=factor(3:12, levels=1:12, labels=AprToDec, ordered=TRUE)) I get + signs in front of monames and labels, again as expected, but then it gives me another + sign. And if I try to add another ), it keeps giving me + signs. What is happening here? From: Gerrit Draisma Date: Tuesday, February 9, 2021 at 6:14 AM To: Parkhurst, David F. Cc: r-help@r-project.org Subject: Re: [R] Need help using lattice Ha David, Thanks for your reply. For your last question, you have to change month into an ordered factor variable: library(lattice) df <- expand.grid(site=c("een","twee","drie","vier","vijf"), month=factor(3:12,levels=1:12, labels=month.abb, ordered=TRUE)) df$conc <- rnorm(dim(df)[1]) dotplot(month~conc|site,data=df) HTH, Gerrit Op 08-02-2021 om 21:04 schreef Parkhurst, David F.: That worked nicely; thanks again. Here’s what I used for our data: library(lattice) df <- expand.grid(site=c("CrCr","NFSC","MFSC","SFSC","LMO","MCE","MUE","MLE","MoCe"),months=1:9) dotplot(months~conc | site, data=df) The result is attached. Is there a way to replace the month numbers on the y axes with the names Apr-Dec? David library(lattice) # month is defined as a factor with 12 levels, of which only levels 4 to 12 # are present in the data. # Note that month.abb is a predefined constant in R. # Here it provides the labels for all 12 levels. df <- expand.grid(site=c("CrCr","NFSC","MFSC","SFSC","LMO","MCE","MUE","MLE"), month=factor(4:12,levels=1:12, labels=month.abb, ordered=TRUE)) df$conc <- rnorm(dim(df)[1]) # introduce some missing values i <- sample(1:dim(df)[1],6) df$conc[i] <- NA # so there some missing points in the plot dotplot(month~conc|site,data=df) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Need help using lattice
Ha David, Thanks for your reply. For your last question, you have to change month into an ordered factor variable: library(lattice) df <- expand.grid(site=c("een","twee","drie","vier","vijf"), month=factor(3:12,levels=1:12, labels=month.abb, ordered=TRUE)) df$conc <- rnorm(dim(df)[1]) dotplot(month~conc|site,data=df) HTH, Gerrit Op 08-02-2021 om 21:04 schreef Parkhurst, David F.: That worked nicely; thanks again. Here’s what I used for our data: library(lattice) df <- expand.grid(site=c("CrCr","NFSC","MFSC","SFSC","LMO","MCE","MUE","MLE","MoCe"),months=1:9) dotplot(months~conc | site, data=df) The result is attached. Is there a way to replace the month numbers on the y axes with the names Apr-Dec? David __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Need help using lattice
David, Is this what you are looking for? library(lattice) df <- expand.grid(site=c("een","twee","drie","vier","vijf"), month=1:12) df$conc <- rnorm(dim(df)[1]) dotplot(month~conc|site,data=df) HTH Gerrit __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] turning a list of objects into columns
Ha Janet, Does this reshape do what you want: df = read.table(header=TRUE,text=" scen streamflowtrans evap psn 1 0.019234 1.658967 0.002883 0.002391 1 0.019027 1.661192 0.002844 0.003142 2 0.018821 1.695623 0.003192 0.002167 2 0.018619 1.503481 0.002536 0.003059 3 0.018425 0.08 1.880355 0.002592 3 0.018369 0.100551 2.225793 0.006642 ") df$t <- 1:2 # reshape(df,idvar="scen",timevar="t",direction="wide") reshape(df,idvar="t",timevar="scen",direction="wide") HTH, Gerrit > Date: Sat, 29 Jun 2019 12:45:46 -0700 > From: Jeff Newmiller > > Maybe look at reshape2? e.g. https://seananderson.ca/2013/10/19/reshape/ > > There is a very capable base R reshape function, but I find it quite > challenging to apply. > > Note that you normally want to retain one or more columns to identify > each row in your result... but I am not sure I see anything suitable > in your example data. You may have removed it, or it may have been > implied and you may need to create something that will do the job. > > e.g. https://www.studytonight.com/dbms/database-key.php > > On June 29, 2019 11:27:40 AM PDT, Janet Choate wrote: > > Hi, > > I have a data frame that looks something like this (but much longer): > > df > > scen streamflowtrans evap psn > > 1 0.019234 1.658967 0.002883 0.002391 > > 1 0.019027 1.661192 0.002844 0.003142 > > 2 0.018821 1.695623 0.003192 0.002167 > > 2 0.018619 1.503481 0.002536 0.003059 > > 3 0.018425 0.08 1.880355 0.002592 > > 3 0.018369 0.100551 2.225793 0.006642 > > > > i want to end up with something like this (for each variable - > > streamflow, > > trans, evap, psn). using the variable trans here as an example: > > trans1trans2 trans3 > > 1.658967 1.695623 0.08 > > 1.661192 1.503481 0.100551 > > > > so that each variable (streamflow, trans, evap, psn) is in a separate > > column based on the scen #. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Problem with Example with Lattice
Hallo Lorenzo, In addition to Berts advice try > trellis.par.get("superpose.symbol") which shows the default for superposed graphics and set them with the par.settings parameter > dotplot(VADeaths, type = "o", + auto.key = list(lines = TRUE, space = "right"), + main = "Death Rates in Virginia - 1940", + par.settings=list(superpose.symbol=list(pch=1:4)), + xlab = "Rate (per 1000)") Gerrit On 05/28/2017 12:00 PM, r-help-requ...@r-project.org wrote: Re: Problem with Example with Lattice __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Multiple Histograms in R
Prateek, Does lattice do what you want? HTH Gerrit ppdat<-read.table(text="mou_mean,totalmrc_mean,rev_range,mou_range,Churn 23,24,25,27,1 45,46,47,49,1 43,44,45,47,1 45,46,47,49,0 56,57,58,60,0 67,68,69,71,1 67,68,69,71,0 44,45,46,48,1 33,34,35,37,0 90,91,92,94,1 87,88,89,91,1 76,77,78,80,1 33,34,35,37,1 44,45,46,48,1", sep=",",header=TRUE) library(lattice) ppdat <- reshape(ppdat,times=names(ppdat)[1:4], varying=list(names(ppdat)[1:4]), direction="long", timevar="Characteristic", v.name="Value") histogram(Churn~Value|Characteristic,nint=10,data=ppdat) Message: 1 Date: Thu, 20 Apr 2017 16:54:35 +0530 From: prateek pandeTo: Hasan Diwan Cc: r-help mailing list Subject: Re: [R] Multiple Histograms in R Message-ID: Content-Type: text/plain; charset="UTF-8" HI Hasan, Thanks for sharing the solution. Really appreciate it. But i was reading somewhere that we cannot use par with ggplot 2 . we can only use grid extra to have multiple plots in a single view. Is it right? Regards Prateek Message: 2 Date: Thu, 20 Apr 2017 12:26:47 + From: Ulrik Stervbo To: prateek pande , Hasan Diwan Cc: r-help mailing list Subject: Re: [R] Multiple Histograms in R Message-ID: Content-Type: text/plain; charset="UTF-8" Hi Prateek, maybe facet_* with ggplot is what you are looking for HTH Ulrik __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Scaling x axis
Ha Fabio, With lattice' xyplot you can do - library(lattice) x<-as.Date(rnorm(10)*10,origin="2016-1-1") y<-5+rnorm(10) xyplot(y~x,type="h",scales=list(x=list(at=x,rot=90))) - And yes, labels may overlap, even with rotation. Gerrit. Message: 5 Date: Thu, 25 Feb 2016 12:31:00 + From: Fabio MonteiroTo: r-help@r-project.org Subject: [R] Scaling x axis Message-ID:
[R] attribute and main value
Just a curiosity question: In the documentation for the nlm procedure a find this example of defining a function with a gradient attribute: --- f - function(x, a) { res - sum((x-a)^2) attr(res, gradient) - 2*(x-a) res } --- I get the gradient with attr(f(3,2),gradient) but how do I get the function value it self? Gerrit __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] attribute and main value
Thanks Duncan. But my question was how to extract simply the function value from value, without the gradient attribute? I see that things like value2 give the right answer. I was curiosity. I found now that value[1] gives strips the attributes from value: -- value [1] 1 attr(,gradient) [1] 2 value[1] [1] 1 -- Is that the way? Gerrit On 12/29/2014 05:05 PM, Duncan Murdoch wrote: On 29/12/2014 10:32 AM, Gerrit Draisma wrote: Just a curiosity question: In the documentation for the nlm procedure a find this example of defining a function with a gradient attribute: --- f - function(x, a) { res - sum((x-a)^2) attr(res, gradient) - 2*(x-a) res } --- I get the gradient with attr(f(3,2),gradient) but how do I get the function value it self? value - f(3,2) gradient - attr(value, gradient) Duncan Murdoch __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] attribute and main value
Thanks Bill, That is what I was looking for. Gerrit On 12/29/2014 05:53 PM, William Dunlap wrote: as.vector(x) will return x without any attributes and structure(x, attrA=NULL, attrB=NULL) will return x without the named attributes. z - f(1:3, 4) z [1] 14 attr(,gradient) [1] -6 -4 -2 as.vector(z) [1] 14 structure(z, gradient=NULL) [1] 14 as.vector is a generic function, so for certain classes (e.g., factor) it may do more than just strip attributes. Matrix dimensions are attributes and will be removed by as.vector. Bill Dunlap TIBCO Software wdunlap tibco.com http://tibco.com On Mon, Dec 29, 2014 at 8:17 AM, Gerrit Draisma gdrai...@xs4all.nl mailto:gdrai...@xs4all.nl wrote: Thanks Duncan. But my question was how to extract simply the function value from value, without the gradient attribute? I see that things like value2 give the right answer. I was curiosity. I found now that value[1] gives strips the attributes from value: -- value [1] 1 attr(,gradient) [1] 2 value[1] [1] 1 -- Is that the way? Gerrit On 12/29/2014 05:05 PM, Duncan Murdoch wrote: On 29/12/2014 10:32 AM, Gerrit Draisma wrote: Just a curiosity question: In the documentation for the nlm procedure a find this example of defining a function with a gradient attribute: --- f - function(x, a) { res - sum((x-a)^2) attr(res, gradient) - 2*(x-a) res } --- I get the gradient with attr(f(3,2),gradient) but how do I get the function value it self? value - f(3,2) gradient - attr(value, gradient) Duncan Murdoch R-help@r-project.org mailto:R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/__listinfo/r-help 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] lattice question: removing strips
Dear Martin, Would - library(latticeExtra) useOuterStrips(p) - be useful for you? Gerrit. -- Message: 1 Date: Mon, 10 Feb 2014 14:12:45 +0200 (EET) From: Martin Ivanov tra...@abv.bg To: Pascal Oettli kri...@ymail.com Cc: r-help@r-project.org Subject: Re: [R] lattice question: removing strips Message-ID: 999135689.229399.1392034366300.javamail.apa...@mail21.abv.bg Content-Type: text/plain; charset=UTF-8 Dear Pascal, Thank You very much for Your reply. Here is a minimal working example: library(lattice); # this is with both strips plotted: data - data.frame(x=1:5, y=6:10, f1=c(a1, a1, a1, a2, a3), f2=c(b1, b2, b2, b1, b2)); p - xyplot(y ~ x | f1 + f2, data=data) print(p); # and this is my attempt to only plot the strips for the second conditioning variable, i.e. for f2: my.strip - function(which.given, strip.levels, ...) { if(which.given == 1) { return(FALSE); } else { strip.default(which.given=which.given, strip.levels=c(TRUE, FALSE), ...); } } p1 - xyplot(y ~ x | f1 + f2, data=data, strip=my.strip) print(p1); #As You can see, here I do not get the strips for f1 potted, but there is an empty space below the strips for f1, which I cannot get rid of. I only want to show the strips for f2. Directly below them I want to see the panels themselves, instead of an empty space for the missing f1 strips. Any suggestions? Best regards, Martin __ 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] eliminating white space in lattice plot
Thanks Duncan, I will have a look at the print command. Gerrit. op 1/18/2014 2:23 PM Duncan Mackay schreef: I forgot to mention for multiple plots ? print.trellis Duncan -Original Message- From: Gerrit Draisma [mailto:gdrai...@xs4all.nl] Sent: Saturday, 18 January 2014 21:49 To: arun; Duncan Mackay Subject: Re: [R] eliminating white space in lattice plot Ha Arun and Duncan, Thanks for your answers. They helped to solve my problem. I found that the width and height parameters can be specified directly in the pdf() command source file, as = pdf(wideplot.pdf,width=10,height=3) xyplot(y~x,data=X,type=l,aspect=0.25) dev.off() = or more useful for me in the R-code chunk in a Sweave = fig=TRUE,echo=FALSE,height=10,width=7= xyplot(y~x,data=X,type=l,aspect=0.25) @ = Have a good day. Mine is already. Gerrit. op 1/17/2014 5:37 PM arun schreef: Hi, May be this helps: x11(width=10,height=3) xyplot(y~x,data=X,type=l,aspect=0.25) dev.copy2pdf(file=wideplot.pdf) dev.off() A.K. On Friday, January 17, 2014 11:26 AM, Gerrit Draisma gdrai...@xs4all.nl wrote: Dear R-users, How do I get rid of the white space above and under a plot made with lattice xyplot? I searched the documentation but could not find how to do it. Standard lattice plots are square, but sometimes I want a wide but low graph and use aspect parameter to obtain such a graph. But then the plot has a lot of white space above and below the graph. That makes it more difficult to include in a LaTeX document. Thanks, Gerrit Draisma = library(lattice) X-data.frame(x=1:100,y=runif(100)) # pdf(wideplot.pdf) xyplot(y~x,data=X,type=l,aspect=0.25) # dev.off() __ 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-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] eliminating white space in lattice plot
Dear R-users, How do I get rid of the white space above and under a plot made with lattice xyplot? I searched the documentation but could not find how to do it. Standard lattice plots are square, but sometimes I want a wide but low graph and use aspect parameter to obtain such a graph. But then the plot has a lot of white space above and below the graph. That makes it more difficult to include in a LaTeX document. Thanks, Gerrit Draisma = library(lattice) X-data.frame(x=1:100,y=runif(100)) # pdf(wideplot.pdf) xyplot(y~x,data=X,type=l,aspect=0.25) # dev.off() __ 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] Lattice: how to change the canvas size with Sweave
Dear all, I would like to include a wide graph with narrow height in my LaTeX output. Up to now I only get it done using Stangle and including pdf(pietje-001.pdf,width=7,height=4) xyplot(...) dev.off() What is the correct way of setting the canvas size? Thanks. Gerrit Draisma ==pietje.tex \documentclass{article} \title{How to define the size of the plotting canvas?} \author{Gerrit Draisma} \begin{document} \maketitle I would like to make this a wide graph with a small height. How do I do that? = library(lattice) @ \raggedbottom \begin{figure}[!h] \centering echo=FALSE,fig=TRUE= xyplot(runif(11)~(0:10),type=l, aspect=0.4, scales=list(x=list(tick.number=11),y=list(tick.number=3))) @ \caption{The canvas is too big!} \end{figure} \end{document} == __ 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] Lattice: how to change the canvas size with Sweave
Oops, I forget to write the I included the lines pdf()..(dev.off() in the file pietje.R generated by Stangle. And now I see that included pietje.tex in reality was named pietje.rnw with pietje.tex the result from Sweave. Apologies for this confusion Gerrit. op 11/17/2013 12:43 PM Gerrit Draisma schreef: Dear all, I would like to include a wide graph with narrow height in my LaTeX output. Up to now I only get it done using Stangle and including pdf(pietje-001.pdf,width=7,height=4) xyplot(...) dev.off() What is the correct way of setting the canvas size? Thanks. Gerrit Draisma ==pietje.tex \documentclass{article} \title{How to define the size of the plotting canvas?} \author{Gerrit Draisma} \begin{document} \maketitle I would like to make this a wide graph with a small height. How do I do that? = library(lattice) @ \raggedbottom \begin{figure}[!h] \centering echo=FALSE,fig=TRUE= xyplot(runif(11)~(0:10),type=l, aspect=0.4, scales=list(x=list(tick.number=11),y=list(tick.number=3))) @ \caption{The canvas is too big!} \end{figure} \end{document} == __ 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] how to combine apply and which or alternative ways to, do so
Ha Guillaume, Is this what you are looking for? #--- x1-sample(1:10,5,replace=FALSE) x2-sample(1:10,100,replace=TRUE) #This tapply(x1,1:5,FUN=function(i){which(x2==i)}) #or this tapply(x1,x1,FUN=function(i){which(x2==i)}) #--- Gerrit __ 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] integrate / Vectorize question
Dag Rui, Yes, it helped a lot! Thanks and greetings, Gerrit. Op 10/4/2012 12:06 AM, Rui Barradas schreef: Hello, Try Fnum - function(x, beta){ sapply(x, function(x) integrate(f,lower=-x,upper=0,beta=beta)$value) } And all results were the same. Hope this helps, Rui Barradas Em 03-10-2012 21:59, Gerrit Draisma escreveu: Dear R-users, I want to use the function Fnum below in another integrate procedure. How do I write Fnum so that it returns a vector of values? And will the last integrate return the right answer? (I tried Vectorize() in several places, but got all sorts of errors.) Thanks for helping out. Gerrit. This my code = beta=c(3,0.3)*1E-2 mu-5 f-function(x,beta){beta[1]+beta[2]*x} Fnum-function(x,beta){ integrate(f,lower=-x,upper=0,beta=beta)$value } Fsym-function(x,beta){x*(beta[1]-beta[2]/2*x)} y-1:4 Fsym(y,beta) Fnum(y,beta) g-function(y,mu){exp(-y/mu)/mu} integrate((function(y,beta,mu){Fsym(y,beta)*g(y,mu)}),lower=0,upper=2*mu, beta=beta,mu=mu) #This fails integrate((function(y,beta,mu){Fnum(y,beta)*g(y,mu)}),lower=0,upper=2*mu, beta=beta,mu=mu) t-seq(0,2*mu, by=0.2) 0.2*sum(tapply(t,t,function(y,beta,mu){Fnum(y,beta)*g(y,mu)}, beta=beta,mu=mu)) __ 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-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] integrate / Vectorize question
Dear R-users, I want to use the function Fnum below in another integrate procedure. How do I write Fnum so that it returns a vector of values? And will the last integrate return the right answer? (I tried Vectorize() in several places, but got all sorts of errors.) Thanks for helping out. Gerrit. This my code = beta=c(3,0.3)*1E-2 mu-5 f-function(x,beta){beta[1]+beta[2]*x} Fnum-function(x,beta){ integrate(f,lower=-x,upper=0,beta=beta)$value } Fsym-function(x,beta){x*(beta[1]-beta[2]/2*x)} y-1:4 Fsym(y,beta) Fnum(y,beta) g-function(y,mu){exp(-y/mu)/mu} integrate((function(y,beta,mu){Fsym(y,beta)*g(y,mu)}),lower=0,upper=2*mu, beta=beta,mu=mu) #This fails integrate((function(y,beta,mu){Fnum(y,beta)*g(y,mu)}),lower=0,upper=2*mu, beta=beta,mu=mu) t-seq(0,2*mu, by=0.2) 0.2*sum(tapply(t,t,function(y,beta,mu){Fnum(y,beta)*g(y,mu)}, beta=beta,mu=mu)) __ 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] how to include integrate in a function that can be solved with uniroot?
Thanks Michael, Op 11/14/2011 3:30 PM, R. Michael Weylandt schreef: You need to explicitly pass th to your function with the ... argument of integrate. That was a point I was missing! Thanks again, This solved my problems for this time. Gerrit. E- function(th){ integrate(function(x,th) x*g(x, th), 0, Inf, th)$value } Also, it's value, not Value, which might be producing errors of another sort. Michael On Mon, Nov 14, 2011 at 9:16 AM, Gerrit Draismagdrai...@xs4all.nl wrote: Thanks Michael, I see now how to include integrate function in the EV function. And apologies: I now realize that my code was sloppy. I intended to write E- function(th) { + integrate( f = function(x,th){x*g(x,th)}, + 0,Inf)$Value} E(1/10) But that does not work either, Gerrit. Op 11/14/2011 2:50 PM, R. Michael Weylandt schreef: Try this: EV- function(lamb){ fnc- function(x) x * dexp(x, lamb) integrate(fnc, 0, Inf)$value } Your problem is that there's nothing to translate th to lambda in your code for E. Michael On Mon, Nov 14, 2011 at 5:32 AM, Gerrit Draismagdrai...@xs4all.nl wrote: Hallo, I am trying to define expectation as an integral and use uniroot to find the distribution parameter for a given expectation. However I fail to understand how to define properly the functions involved and pass the parameters correctly. Can anyone help me out? Thanks, Gerrit Draisma. This what I tried: === # exponential density g- function(x,lambda){ lambda *exp(-lambda*x) } # expectation with lambda=1/10 integrate(f = function(x,lambda=1/10) {x*g(x,lambda)}, 0,Inf) 10 with absolute error6.7e-05 # *how to write this as a function?* E- function(lambda) { + integrate( f = function(x,th){x*g(x,lambda)}, + 0,Inf)$Value} E(1/10) NULL # *how to include this function in uniroot to find lambda* # *for a given expectation?* mu- 10 uniroot(f-function(th){E(th)-mu},lower=1,upper=100) Error in if (is.na(f.lower)) stop(f.lower = f(lower) is NA) : argument is of length zero __ 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-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] how to include integrate in a function that can be solved with uniroot?
Hallo, I am trying to define expectation as an integral and use uniroot to find the distribution parameter for a given expectation. However I fail to understand how to define properly the functions involved and pass the parameters correctly. Can anyone help me out? Thanks, Gerrit Draisma. This what I tried: === # exponential density g - function(x,lambda){ lambda *exp(-lambda*x) } # expectation with lambda=1/10 integrate(f = function(x,lambda=1/10) {x*g(x,lambda)}, 0,Inf) 10 with absolute error 6.7e-05 # *how to write this as a function?* E - function(lambda) { + integrate( f = function(x,th){x*g(x,lambda)}, + 0,Inf)$Value} E(1/10) NULL # *how to include this function in uniroot to find lambda* # *for a given expectation?* mu - 10 uniroot(f-function(th){E(th)-mu},lower=1,upper=100) Error in if (is.na(f.lower)) stop(f.lower = f(lower) is NA) : argument is of length zero __ 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] understanding output of tapply/by cumsum
Thanks Jim, Ave does what I wanted. It is simpler and probably more efficient than unlisting Sn as I tried. Still I remain puzzled with the structure of the by() or tapply() output and how to access the individual cumsums. Yes the split command is useful for checking the result. Gerrit. Op 12/7/2010 1:43 PM, jim holtman schreef: Maybe 'ave' is what you were looking for: d$cum- ave(d$n, d$a, d$c, FUN = cumsum) d a b cn cum 1 1 1 1 11.1 11.1 2 2 1 1 21.1 21.1 3 3 1 1 31.1 31.1 4 4 1 1 41.1 41.1 5 5 1 1 51.1 51.1 6 1 2 1 12.1 23.2 7 2 2 1 22.1 43.2 8 3 2 1 32.1 63.2 9 4 2 1 42.1 83.2 10 5 2 1 52.1 103.2 11 1 3 1 13.1 36.3 12 2 3 1 23.1 66.3 13 3 3 1 33.1 96.3 14 4 3 1 43.1 126.3 15 5 3 1 53.1 156.3 16 1 1 2 11.2 11.2 17 2 1 2 21.2 21.2 18 3 1 2 31.2 31.2 19 4 1 2 41.2 41.2 20 5 1 2 51.2 51.2 21 1 2 2 12.2 23.4 22 2 2 2 22.2 43.4 23 3 2 2 32.2 63.4 24 4 2 2 42.2 83.4 25 5 2 2 52.2 103.4 26 1 3 2 13.2 36.6 27 2 3 2 23.2 66.6 28 3 3 2 33.2 96.6 29 4 3 2 43.2 126.6 30 5 3 2 53.2 156.6 On Tue, Dec 7, 2010 at 6:39 AM, Gerrit Draismagdrai...@xs4all.nl wrote: Dear R-users, I have a dataset with categories and numbers. I would like to compute and add cumulative numbers to the dataset. I do not understand the structure of by(...) or tapply(...) output enough to handle it. Here a small example -- d-expand.grid(a=1:5,b=1:3,c=1:2) d$n = 10 * d$a + d$b +0.1* d$c Sn-by(d$n,list(d$a,d$c),cumsum) str(Sn) - List of 10 $ : num [1:3] 11.1 23.2 36.3 $ : num [1:3] 21.1 43.2 66.3 $ : num [1:3] 31.1 63.2 96.3 $ : num [1:3] 41.1 83.2 126.3 $ : num [1:3] 51.1 103.2 156.3 $ : num [1:3] 11.2 23.4 36.6 $ : num [1:3] 21.2 43.4 66.6 $ : num [1:3] 31.2 63.4 96.6 $ : num [1:3] 41.2 83.4 126.6 $ : num [1:3] 51.2 103.4 156.6 - attr(*, dim)= int [1:2] 5 2 - attr(*, dimnames)=List of 2 ..$ : chr [1:5] 1 2 3 4 ... ..$ : chr [1:2] 1 2 - attr(*, call)= language by.default(data = d$n, INDICES = list(d$a, d$c), FUN = cumsum) - attr(*, class)= chr by - # these give (a) lists of one numerical vector(a) Sn[5,2] Sn[cbind(d$a,d$c)] # how to access the individual cumsum values? # and assign them to d$Sn? -- Thanks, Gerrit. --- Gerrit Draisma Department of Public Health Erasmus MC, University Medical Center Rotterdam Room AE-235 P.O. Box 2040 3000 CA Rotterdam The Netherlands Phone: +31 10 7043787 Fax: +31 10 7038474 http://mgzlx4.erasmusmc.nl/pwp/?gdraisma __ 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-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] understanding output of tapply/by cumsum
Dear R-users, I have a dataset with categories and numbers. I would like to compute and add cumulative numbers to the dataset. I do not understand the structure of by(...) or tapply(...) output enough to handle it. Here a small example -- d-expand.grid(a=1:5,b=1:3,c=1:2) d$n = 10 * d$a + d$b +0.1* d$c Sn-by(d$n,list(d$a,d$c),cumsum) str(Sn) - List of 10 $ : num [1:3] 11.1 23.2 36.3 $ : num [1:3] 21.1 43.2 66.3 $ : num [1:3] 31.1 63.2 96.3 $ : num [1:3] 41.1 83.2 126.3 $ : num [1:3] 51.1 103.2 156.3 $ : num [1:3] 11.2 23.4 36.6 $ : num [1:3] 21.2 43.4 66.6 $ : num [1:3] 31.2 63.4 96.6 $ : num [1:3] 41.2 83.4 126.6 $ : num [1:3] 51.2 103.4 156.6 - attr(*, dim)= int [1:2] 5 2 - attr(*, dimnames)=List of 2 ..$ : chr [1:5] 1 2 3 4 ... ..$ : chr [1:2] 1 2 - attr(*, call)= language by.default(data = d$n, INDICES = list(d$a, d$c), FUN = cumsum) - attr(*, class)= chr by - # these give (a) lists of one numerical vector(a) Sn[5,2] Sn[cbind(d$a,d$c)] # how to access the individual cumsum values? # and assign them to d$Sn? -- Thanks, Gerrit. --- Gerrit Draisma Department of Public Health Erasmus MC, University Medical Center Rotterdam Room AE-235 P.O. Box 2040 3000 CA Rotterdam The Netherlands Phone: +31 10 7043787 Fax: +31 10 7038474 http://mgzlx4.erasmusmc.nl/pwp/?gdraisma __ 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] combined plot of observed and expected fractions
Dear R-users, I have a dataset of numbers of cases with a certain age and stage. I want to plot the observed stage distribution by age and compare it to an expected or predicted one. As I want to plot these for several populations I want to use lattice plots. Below is example code I made that produces a plot that satisfies me. I prefer this to using stacked bars. But I would like to improve on the code. Specifically: Question 1: How to get cumulative fractions from observed numbers? Question 2: How to divide each number by the total number in each age group? Question 3: How to construct an indicator for combined categories (here: observed/expected x Stage) Question 4: Lattice: could I use data values to locate the text? I would appreciate any comments. Thank you, Gerrit. = # stagexage.r # compare observed and predicted stage by age. library(lattice) # simple data set df-data.frame(Age=rep(1:5,each=3),Stage=1:3) # expectation df$mu-5+(df$Age-1)*df$Stage # simulated data df$N-rpois(15,df$mu) # for lattice xyplot df-reshape(df,direction=long,varying=c(N,mu),timevar=OE,v.name=N) # cumulate numbers over Age group and OE (Observed, Expected) # Q1: how to aply cumsum by age and stage? i-df$Age+5*(df$OE-1) P-unlist(tapply(df$N,i,cumsum)) # compute probabilities # Q2: How to divide by the total number by age and stage? j-3*(0:29%/%3) + 3 P-P/P[j] # combining OE and Stage in a single index # for superposing graphs in one plot # Q3: How to create an index from Stage and number (obs or exp)? ix-(df$Stage-1)*2+df$OE # plot observed and predicted fractions in a area plot j-df$Stage!=3 xyplot(P[j]~Age,groups=ix[j],data=df[j,],type=o, panel=function(x,y,...){ panel.xyplot(x,y,...) # Q4: would it be possible to position labels based # data values? panel.text(x=3,y=c(0.15,0.5,0.85),labels=paste(Stage, 1:3)) }, scales=list(y=list(at=0:4/4)), axs=i,ylim=c(0,1), xlab=Age, ylab=Fraction, par.settings=list(superpose.line=list(col=c(blue,red),lty=1:2), superpose.symbol=list(col=c(blue,red),type=1)), auto.key=list(text=c(Obs,Pred), points=F,lines=T,type=o,divide=1,columns=2) ) = -- Gerrit Draisma Department of Public Health Erasmus MC, University Medical Center Rotterdam Room AE-235 P.O. Box 2040 3000 CA Rotterdam The Netherlands Phone: +31 10 7043787 Fax: +31 10 7038474 http://mgzlx4.erasmusmc.nl/pwp/?gdraisma __ 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] printing a dataframe by categories
I am looking for the following simple question. I have a data frame with names and numbers, divided in categories. I would like to produce a text file with page breaks, listing the names and numbers by category, and totalling the numbers. Example: Name-LETTERS[1:6] Score-rep(5:8,length.out=6) Form-rep(1:2,each=3) x-data.frame(Name,Score,Form) This gives approximately what I need by(x,x$Form,function(x){x[,1:2]}) but I would like a total added to the list. Gerrit. __ 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] latex.table for table with character and numeric columns
Ha Charlie, It helped. It is just what i was looking for. Gerrit. Gerrit Draisma wrote: Hallo, I have a dataset with one or two columns with character data and the rest with numeric data. Using latex.table from the quantreg package produced a table, but I cannot set the decimals. For instance: --- x-data.frame(Name=c(Jan,Piet,Jan), V=c(1,2.991,3)) latex.table(as.matrix(x),file=x,caption=x) latex.table(as.matrix(x),file=x,caption=x,dec=2) Error in round(x, dec) : Non-numeric argument to mathematical function --- Am I not using the right command, or is there a way around? Thanks, Gerrit. Hi Gerrit, I haven't used latex.table from quantreg so I can't comment on this approach. However, I would recommend using the xtable package to format your data.frame into LaTeX: require( xtable ) x-data.frame(Name=c(Jan,Piet,Jan), V=c(1,2.991,3)) # Create a table object from the data.frame latexTable - xtable( x ) # Set the number of digits in each column. Your example has two columns, # but we must specify three digits since the rownames count as a column. digits( latexTable ) - c( 0, 0, 2 ) # Print the table object in order to produce the LaTeX code. print( latexTable ) You should also read the help page for print.xtable() as it discusses many options that can affect the final LaTeX output. Hope this helps! -Charlie __ 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] latex.table for table with character and numeric columns
Hallo, I have a dataset with one or two columns with character data and the rest with numeric data. Using latex.table from the quantreg package produced a table, but I cannot set the decimals. For instance: --- x-data.frame(Name=c(Jan,Piet,Jan), V=c(1,2.991,3)) latex.table(as.matrix(x),file=x,caption=x) latex.table(as.matrix(x),file=x,caption=x,dec=2) Error in round(x, dec) : Non-numeric argument to mathematical function --- Am I not using the right command, or is there a way around? Thanks, Gerrit. __ 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] correct line types in lattice legends
Hallo R-users, I do not understand how to specify the correct line and symbol types in the legends of a lattice xyplot. This is what I tried, but the line types and symbol in the graph are not seen in the legend. Any help is appreciated. Thanks, Gerrit. library(lattice) s-rep(1:3,len=10) x- 1:10 y- x+s+rnorm(10) d-data.frame(s,x,y) xyplot(y~x, groups=s,data=d, type=b, pch=1:3,lty=1:3, auto.key=list(text=c(A,B,C),points=FALSE,lines=TRUE, type=b,lty=1:3,divide=1,pch=1:3,columns=3)) __ 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] Vectorize tapply(...,cumsum) output
I would like to graph cumulative counts and use tapply(...,cumsum) to get the cumulative counts. For instance: library(lattice) t-rep(1:4,each=3)#time i-rep(1:3,times=4) #categories n-rpois(length(t),t) #count xyplot(n ~ t,groups=i, type=o,key=simpleKey(as.character(1:3),x=0,y=1,lines=T)) N-tapply(n,t,cumsum) Now, what is the best way to convert N to a vector that can be plotted? E.g. xyplot(N ~ t,groups=i, type=o,key=simpleKey(as.character(1:3),x=0,y=1,lines=T)) Or is there a better way to get cumulative graphs? Thanks, Gerrit. __ 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.