[R] optim with constraints
Hi, dear R users I am a newbie in R and I wantto use the method of meximum likelihood to fit a Weibull distribution to my survival data. I use "optim" as follows: optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian = TRUE) My question is: how do I setup the constraints so that the two parametrs of Weibull to be pisotive? Or should I use other function like"nlm"? Many thanks! Any comments are greatly appreciated! Steven __ 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] optim with constraints
Hi, Prof Nash Thanks for your comment! I modified my code to be (added an extra parametr): optim(c(1.14,0.25,0.06), weibull.like, mydata=mydata, method="L-BFGS- B", hessian = TRUE, lower = c(0, 0, 0), upper = c(Inf, Inf, 1)) But I had the following error: Error in optim(c(1.14, 0.25, 0.06), weibull.like, mydata = mydata, method = "L-BFGS-B", : non-finite finite-difference value [2] What does that mean? Much appreciate your help! Steven On Dec 7, 1:15 am, "Prof. John C Nash" wrote: > As per ?optim > > > Usage: > > > optim(par, fn, gr = NULL, ..., > > method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"), > > lower = -Inf, upper = Inf, > > control = list(), hessian = FALSE) > > Note that the optimx() function on > R-forgehttp://r-forge.r-project.org/R/?group_id=395 > is a wrapper that allows some other methods too. > > JN > > > > > > > Message: 72 > > Date: Sun, 6 Dec 2009 02:53:55 -0800 (PST) > > From: Steven > > Subject: [R] optim with constraints > > To: r-h...@r-project.org > > Message-ID: > > <5e293933-91f2-48f7-98e5-bc87905c5...@x25g2000prf.googlegroups.com> > > Content-Type: text/plain; charset=ISO-8859-1 > > > Hi, dear R users > > > I am a newbie in R and I wantto use the method of meximum likelihood > > to fit a Weibull distribution to my survival data. I use "optim" as > > follows: > > > optim(c(1, 0.25),weibull.like,mydata=mydata,method="L-BFGS-B",hessian > > = TRUE) > > > My question is: how do I setup the constraints so that the two > > parametrs of Weibull to be pisotive? Or should I use other function > > like"nlm"? > > > Many thanks! Any comments are greatly appreciated! > > > Steven > > __ > r-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.- Hide > quoted text - > > - Show quoted text - __ 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] hist{graphics}
# Can someone help with this simple frequency histogram problem (n = 15)? # I use four class limits: [90,95], [95,100], [100,105], [105,110]. # These coincide with the limits obtain by pretty {base}. # Proper frequencies would be: (1,5,6,3). # But hist{graphics} gives me a histogram showing frequencies (1,8,3,3), # with or without argument break = ... # Replicable codes below. Thanks. set.seed(123) x<-rnorm(15,mean=100,sd=5); x<-as.integer(x) x<-sort(x) x breaks<-seq(90,110,by=5); breaks pretty(x,n=5) # pretty {base} x.cut<-cut(x,breaks,right=F) ; x.cut freq<-table(x.cut); cbind(freq) hist(x,breaks=breaks) # hist {graphics} hist(x) __ 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] hist{graphics}
Never mind. Thanks. I found that adding parameter right=F to the call fixes it. On 2019/7/12 下午 05:10, Steven wrote: # Can someone help with this simple frequency histogram problem (n = 15)? # I use four class limits: [90,95], [95,100], [100,105], [105,110]. # These coincide with the limits obtain by pretty {base}. # Proper frequencies would be: (1,5,6,3). # But hist{graphics} gives me a histogram showing frequencies (1,8,3,3), # with or without argument break = ... # Replicable codes below. Thanks. set.seed(123) x<-rnorm(15,mean=100,sd=5); x<-as.integer(x) x<-sort(x) x breaks<-seq(90,110,by=5); breaks pretty(x,n=5) # pretty {base} x.cut<-cut(x,breaks,right=F) ; x.cut freq<-table(x.cut); cbind(freq) hist(x,breaks=breaks) # hist {graphics} hist(x) __ 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] Printing vector
Is there a convenient way to print a vector into rows of a specified column length? What I need is to print in the old FORTRAN format, viz., format(10F8.2) which would print, for instance, a vector of 25 into two rows of 10 plus an incomplete row of 5. I managed to write a procedure for that task, as shown below (except that I prefer simply blanks rather than the NA). I am too embarrassed to even show the procedure. In short, I like to print in the above FORTRAN format. Thank you. [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.66 0.26 0.82 0.73 0.13 0.05 0.56 0.67 0.74 0.87 [2,] 0.91 0.25 0.40 0.39 0.50 0.89 0.07 0.84 0.14 0.75 [3,] 0.38 0.08 0.86 0.97 0.56 NA NA NA NA NA [[alternative HTML version deleted]] __ 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] Printing vector
Dear All: Below is what I meant. Procedure print0 allows me to print a vector of length 53 in four rows of 10 plus 1 row of 3 (Ido not like the NA). This is silly. I am hoping that there is a candid way to print the matrix. Thank you. Steven Yen === n<-53; x<-runif(n); # x<-round(x,2) print0<-function(x,c=10,digits=2){ # ** # Print vector in rows of a specified length # ** n<-length(x) r<-n/c; if(n%%c>0) r<-as.integer(r)+1 y<-rep(NA,r*c) y[1:n]<-x y<-matrix(y,r,c,byrow=T) y<-round(y,digits=digits) y } print0(x,c=10,digits=3) # result # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,] 0.576 0.291 0.600 0.515 0.135 0.335 0.296 0.911 0.454 0.696 # [2,] 0.699 0.728 0.442 0.469 0.996 0.539 0.772 0.768 0.652 0.882 # [3,] 0.614 0.228 0.748 0.071 0.788 0.428 0.885 0.722 0.432 0.881 # [4,] 0.422 0.148 0.459 0.870 0.044 0.421 0.282 0.337 0.751 0.579 # [5,] 0.468 0.659 0.446 0.199 0.388 0.576 0.829 0.186 0.823 0.960 # [6,] 0.880 0.944 0.709 NA NA NA NA NA NA NA Steven 於 2019/7/20 下午 02:00 寫道: > > Is there a convenient way to print a vector into rows of a specified > column length? What I need is to print in the old FORTRAN format, viz., > > format(10F8.2) > > which would print, for instance, a vector of 25 into two rows of 10 > plus an incomplete row of 5. I managed to write a procedure for that > task, as shown below (except that I prefer simply blanks rather than > the NA). I am too embarrassed to even show the procedure. In short, I > like to print in the above FORTRAN format. Thank you. > > > > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.66 0.26 0.82 > 0.73 0.13 0.05 0.56 0.67 0.74 0.87 [2,] 0.91 0.25 0.40 0.39 0.50 0.89 > 0.07 0.84 0.14 0.75 [3,] 0.38 0.08 0.86 0.97 0.56 NA NA NA NA NA [[alternative HTML version deleted]] __ 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] Printing vector
Thank you, Gentlemen. That serves my need. Bill's routine is great. Also, Rui: Is there a way to get rid of the filled "NA" and use space instead. Using fill = "" does not help either; it causes all numbers to be embraced with quotations. Finally, I have no idea why Rui's message did not reach me at all (not even in the junk mail box). But, obviously Bill had received it. Hmm. Steven Yen William Dunlap 於 2019/7/23 上午 12:33 寫道: > The following mimics Fortran printing with format > F.. > > print1 <- function (x, perLine = 10, fWidth = 8, fPrecision = 2, > fortranStars = TRUE) > { > format <- paste0("%", fWidth, ".", fPrecision, "f") > oldWidth <- getOption("width") > on.exit(options(width = oldWidth)) > options(width = perLine * fWidth) > fx <- sprintf(format, x) > if (fortranStars) { > fx[nchar(fx) > fWidth] <- strrep("*", fWidth) > } > cat(fx, sep = "", fill = TRUE) > invisible(x) > } > > Compare > > print1((-1.7)^(1:24)) > -1.70 2.89 -4.91 8.35 -14.20 24.14 -41.03 69.76 -118.59 > 201.60 > -342.72 582.62 -990.46 1683.78-2862.42 > 4866.12-8272.4014063.0840642.31 > > with the output from the Fortran > % cat a.f > double precision x(24); > integer i > do 10 i=1,24 > x(i) = (-1.7d0) ** i > 10 continue > write(6, "(10f8.2)") x > stop > end > % gfortran a.f > % ./a.out > -1.70 2.89 -4.91 8.35 -14.20 24.14 -41.03 69.76 -118.59 > 201.60 > -342.72 582.62 -990.46 1683.78-2862.42 > 4866.12-8272.4014063.0840642.31 > > > > Compare > Bill Dunlap > TIBCO Software > wdunlap tibco.com <http://tibco.com> > > > On Mon, Jul 22, 2019 at 12:19 AM Rui Barradas <mailto:ruipbarra...@sapo.pt>> wrote: > > Simpler, no loops: > > > print0b <- function(x, len = 10, digits = 2, fill = ""){ > n <- length(x) > x <- round(x, digits = digits) > m <- n %/% len > remainder <- n %% len > A <- matrix(x[seq_len(len*m)], ncol = len) > if(remainder > 0){ > A <- rbind(A, c(x[(len*m + 1):n], rep(fill, len*(m + 1) - n))) > } > A > } > > > Hope this helps, > > Rui Barradas > > Às 07:47 de 22/07/19, Rui Barradas escreveu: > > Hello, > > > > Maybe something like the following is what you want. > > I have added an extra argument 'fill' to allow to choose what to > print > > in the end. It's default value is "" making the entire matrix > elements > > characters but it can be NA or 0. > > > > print0 <- function(x, len = 10, digits = 2, fill = ""){ > > n <- length(x) > > x <- round(x, digits = digits) > > passes <- n %/% len > > remainder <- n %% len > > A <- matrix(fill, nrow = passes + (remainder > 0), ncol = len) > > for(i in seq_len(passes)){ > > A[i, ] <- x[(len*(i - 1) + 1):(len*i)] > > } > > A[nrow(A), 1:remainder] <- x[(len*passes + 1):n] > > A > > } > > > > print0(rnorm(23), 10) > > print0(rnorm(23), 10, fill = 0) > > > > > > Hope this helps, > > > > Rui Barradas > > > > Às 21:34 de 20/07/19, Steven escreveu: > >> Dear All: > >> > >> Below is what I meant. Procedure print0 allows me to print a > vector of > >> length 53 in four rows of 10 plus 1 row of 3 (Ido not like the > NA). This > >> is silly. I am hoping that there is a candid way to print the > matrix. > >> Thank you. > >> > >> Steven Yen > >> > >> === > >> n<-53; x<-runif(n); # x<-round(x,2) > >> > >> print0<-function(x,c=10,digits=2){ > >> # ** > >> # Print vector in rows of a specified length > >> # ** > >> n<-length(x) > >> r<-n/c; if(n%%c>0) r<-as.integer(r)+1 > >> y<-rep(NA,r*c) > >> y[1:n]<-x > >> y<-matrix(y,r,c,byrow=T) > >> y<-round(y,digits=digits) >
Re: [R] Printing vector
Very nice indeed. Thank you gentlemen. Steven Michael Friendly 於 2019/7/24 上午 01:23 寫道: Nice to see William Dunlap take the trouble to mimic the classic Fortran behavior of printing for numbers that don't fit in the given width :) -Michael On 7/22/19 6:33 p.m., William Dunlap via R-help wrote: The following mimics Fortran printing with format F.. print1 <- function (x, perLine = 10, fWidth = 8, fPrecision = 2, fortranStars = TRUE) { format <- paste0("%", fWidth, ".", fPrecision, "f") oldWidth <- getOption("width") on.exit(options(width = oldWidth)) options(width = perLine * fWidth) fx <- sprintf(format, x) if (fortranStars) { fx[nchar(fx) > fWidth] <- strrep("*", fWidth) } cat(fx, sep = "", fill = TRUE) invisible(x) } Compare print1((-1.7)^(1:24)) -1.70 2.89 -4.91 8.35 -14.20 24.14 -41.03 69.76 -118.59 201.60 -342.72 582.62 -990.46 1683.78-2862.42 4866.12-8272.4014063.0840642.31 with the output from the Fortran % cat a.f double precision x(24); integer i do 10 i=1,24 x(i) = (-1.7d0) ** i 10 continue write(6, "(10f8.2)") x stop end % gfortran a.f % ./a.out -1.70 2.89 -4.91 8.35 -14.20 24.14 -41.03 69.76 -118.59 201.60 -342.72 582.62 -990.46 1683.78-2862.42 4866.12-8272.4014063.0840642.31 Compare Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jul 22, 2019 at 12:19 AM Rui Barradas wrote: Simpler, no loops: print0b <- function(x, len = 10, digits = 2, fill = ""){ n <- length(x) x <- round(x, digits = digits) m <- n %/% len remainder <- n %% len A <- matrix(x[seq_len(len*m)], ncol = len) if(remainder > 0){ A <- rbind(A, c(x[(len*m + 1):n], rep(fill, len*(m + 1) - n))) } A } Hope this helps, Rui Barradas Às 07:47 de 22/07/19, Rui Barradas escreveu: Hello, Maybe something like the following is what you want. I have added an extra argument 'fill' to allow to choose what to print in the end. It's default value is "" making the entire matrix elements characters but it can be NA or 0. print0 <- function(x, len = 10, digits = 2, fill = ""){ n <- length(x) x <- round(x, digits = digits) passes <- n %/% len remainder <- n %% len A <- matrix(fill, nrow = passes + (remainder > 0), ncol = len) for(i in seq_len(passes)){ A[i, ] <- x[(len*(i - 1) + 1):(len*i)] } A[nrow(A), 1:remainder] <- x[(len*passes + 1):n] A } print0(rnorm(23), 10) print0(rnorm(23), 10, fill = 0) Hope this helps, Rui Barradas Às 21:34 de 20/07/19, Steven escreveu: Dear All: Below is what I meant. Procedure print0 allows me to print a vector of length 53 in four rows of 10 plus 1 row of 3 (Ido not like the NA). This is silly. I am hoping that there is a candid way to print the matrix. Thank you. Steven Yen === n<-53; x<-runif(n); # x<-round(x,2) print0<-function(x,c=10,digits=2){ # ** # Print vector in rows of a specified length # ** n<-length(x) r<-n/c; if(n%%c>0) r<-as.integer(r)+1 y<-rep(NA,r*c) y[1:n]<-x y<-matrix(y,r,c,byrow=T) y<-round(y,digits=digits) y } print0(x,c=10,digits=3) # result # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,] 0.576 0.291 0.600 0.515 0.135 0.335 0.296 0.911 0.454 0.696 # [2,] 0.699 0.728 0.442 0.469 0.996 0.539 0.772 0.768 0.652 0.882 # [3,] 0.614 0.228 0.748 0.071 0.788 0.428 0.885 0.722 0.432 0.881 # [4,] 0.422 0.148 0.459 0.870 0.044 0.421 0.282 0.337 0.751 0.579 # [5,] 0.468 0.659 0.446 0.199 0.388 0.576 0.829 0.186 0.823 0.960 # [6,] 0.880 0.944 0.709 NA NA NA NA NA NA NA Steven 於 2019/7/20 下午 02:00 寫道: Is there a convenient way to print a vector into rows of a specified column length? What I need is to print in the old FORTRAN format, viz., format(10F8.2) which would print, for instance, a vector of 25 into two rows of 10 plus an incomplete row of 5. I managed to write a procedure for that task, as shown below (except that I prefer simply blanks rather than the NA). I am too embarrassed to even show the procedure. In short, I like to print in the above FORTRAN format. Thank you. [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.66 0.26 0.82 0.73 0.13 0.05 0.56 0.67 0.74 0.87 [2,] 0.91 0.25 0.40 0.39 0.50 0.89 0.07 0.84 0.14 0.75 [3,] 0.38 0.08 0.86 0.97 0.56 NA NA NA NA NA [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/m
Re: [R] Printing vector
Thank you Lei. I incorporate Bill Dunlap's idea of flagging with FORTRAN stars when field width is short. It works great and serves what I need. Thank you all. I love R! Steven Linus Chen 於 2019/7/23 下午 05:46 寫道: Dear Steven, The function "write()" has a parameter "columns". And sprint() can do do some formatting in C style. x <- rnorm(100) s <- sprintf( fmt="%8.2f" ,x ) write(s, file="", ncolumns=7L) Cheers, Lei On Mon, 22 Jul 2019 at 07:37, Steven wrote: Is there a convenient way to print a vector into rows of a specified column length? What I need is to print in the old FORTRAN format, viz., format(10F8.2) which would print, for instance, a vector of 25 into two rows of 10 plus an incomplete row of 5. I managed to write a procedure for that task, as shown below (except that I prefer simply blanks rather than the NA). I am too embarrassed to even show the procedure. In short, I like to print in the above FORTRAN format. Thank you. [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.66 0.26 0.82 0.73 0.13 0.05 0.56 0.67 0.74 0.87 [2,] 0.91 0.25 0.40 0.39 0.50 0.89 0.07 0.84 0.14 0.75 [3,] 0.38 0.08 0.86 0.97 0.56 NA NA NA NA NA [[alternative HTML version deleted]] __ 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-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] Rtools required
Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Steven Yen WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ [[alternative HTML version deleted]] __ 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] Rtools required
Thanks John. Where is file .Renviron located? It must be a hidden file. I cannot find it. On 2020/4/28 下午 08:29, Fox, John wrote: Dear Steven, Did you follow the instruction on the Rtools webpage to add PATH="${RTOOLS40_HOME}\usr\bin;${PATH}" to your .Renviron file? I hope this helps, John - John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: http::/socserv.mcmaster.ca/jfox On Apr 28, 2020, at 4:38 AM, Steven wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Steven Yen WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ [[alternative HTML version deleted]] __ 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-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] Rtools required
Hello John, Perhaps you can help me. I am an idiot. I visited the Rtools web page and learn to run the following lines in R: Still I am getting the same warning message. > writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") > Sys.which("make") make "C:\\rtools40\\usr\\bin\\make.exe" On 2020/4/28 下午 08:29, Fox, John wrote: Dear Steven, Did you follow the instruction on the Rtools webpage to add PATH="${RTOOLS40_HOME}\usr\bin;${PATH}" to your .Renviron file? I hope this helps, John - John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: http::/socserv.mcmaster.ca/jfox On Apr 28, 2020, at 4:38 AM, Steven wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Steven Yen WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ [[alternative HTML version deleted]] __ 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-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] Rtools required
Good point, but I am bothered by the non-discrimatory warning message to all package installation attempts. My students install mostly binary files, and I just hate to have them install Rtools. For one, they have not had to do so until after RStudio-1.1.463. The fact that using this older RStudio can avoid the annoying warning message speaks volume. On 2020/4/30 上午 10:02, Bert Gunter wrote: Wouldn't packages that have to be built from source on installation require Rtools? Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Apr 29, 2020 at 6:48 PM Steven wrote: Thanks to all - very helpful. I search from c:\ and now find file .Renviron located in C:\Users\USER\Documents. That does it. I would like to pose an additional question, since it would also fall under the same subject line. This is an RStudio question but since I am using the free version the RStudio folks would not help me. My students simply need to install packages and are not building any packages so they have no reason to install Rtools. When they install (not build) packages (from CRAN or file archive) they received the same warning message saying "Rtools required to build a package". This is nonsense of course as they are, as I said, simply installing. I tell them to user an older RStudio version, specifically 1.1.463 that's free of that warning. Any idea? Steven Yen On 2020/4/30 上午 06:30, Fox, John wrote: Dear Steven, It's possible that Windows will hide .Renviron, but it's generally a good idea, in my opinion, in Folder Options > View to click "Show hidden files" and uncheck "hide extensions". Then .Renviron should show up (once you've created it). Best, John -Original Message- From: Bert Gunter Sent: Wednesday, April 29, 2020 5:50 PM To: Steven Cc: Fox, John ; R-help Mailing List Subject: Re: [R] Rtools required Type ?.Renviron ?R.home ?"environment variables" at the R prompt to get what I think should be the info you need (or at least useful info). Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Apr 29, 2020 at 2:37 PM Steven wrote: Thanks John. Where is file .Renviron located? It must be a hidden file. I cannot find it. On 2020/4/28 下午 08:29, Fox, John wrote: Dear Steven, Did you follow the instruction on the Rtools webpage to add PATH="${RTOOLS40_HOME}\usr\bin;${PATH}" to your .Renviron file? I hope this helps, John - John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: http::/socserv.mcmaster.ca/jfox On Apr 28, 2020, at 4:38 AM, Steven wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Steven Yen WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ [[alternative HTML version deleted]] __ 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-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-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] Rtools required
Thanks to all - very helpful. I search from c:\ and now find file .Renviron located in C:\Users\USER\Documents. That does it. I would like to pose an additional question, since it would also fall under the same subject line. This is an RStudio question but since I am using the free version the RStudio folks would not help me. My students simply need to install packages and are not building any packages so they have no reason to install Rtools. When they install (not build) packages (from CRAN or file archive) they received the same warning message saying "Rtools required to build a package". This is nonsense of course as they are, as I said, simply installing. I tell them to user an older RStudio version, specifically 1.1.463 that's free of that warning. Any idea? Steven Yen On 2020/4/30 上午 06:30, Fox, John wrote: Dear Steven, It's possible that Windows will hide .Renviron, but it's generally a good idea, in my opinion, in Folder Options > View to click "Show hidden files" and uncheck "hide extensions". Then .Renviron should show up (once you've created it). Best, John -Original Message- From: Bert Gunter Sent: Wednesday, April 29, 2020 5:50 PM To: Steven Cc: Fox, John ; R-help Mailing List Subject: Re: [R] Rtools required Type ?.Renviron ?R.home ?"environment variables" at the R prompt to get what I think should be the info you need (or at least useful info). Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Apr 29, 2020 at 2:37 PM Steven wrote: Thanks John. Where is file .Renviron located? It must be a hidden file. I cannot find it. On 2020/4/28 下午 08:29, Fox, John wrote: Dear Steven, Did you follow the instruction on the Rtools webpage to add PATH="${RTOOLS40_HOME}\usr\bin;${PATH}" to your .Renviron file? I hope this helps, John - John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: http::/socserv.mcmaster.ca/jfox On Apr 28, 2020, at 4:38 AM, Steven wrote: Dear All I updated to R-4.0.0. and also installed the latest Rtools 4.0 (to now the new default folder c:\rtools40). While compiling a package (binary) I received the follow marning message saying Rtools is required. Any clues? Thanks. Steven Yen WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ [[alternative HTML version deleted]] __ 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-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-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] Rtools required
Thank you all. In sum, installing packages from RStudio 1.2.5042 without Rtools present, I received warnings saying RTools required but installation nevertheless was successful. Installing from R-4.0.0 directly, I have no problem. This is obviously an RStudio problem but unfortunately they are not willing to help free version users. I feel so sorry having to post it here. === 1. IN RStudio I reun the following to install from CRAC and from local a file, respectively: Tools -> Install Packages -> (Choose Repository CRAN from pull-down menu) Tools -> Install Packages -> (Packages, Archice, Files (.zip; .tar.gz) from pull-down menu) In both cases, I received warning messages saying Rtools is required... but nevertheless the installation is successful. 2. Installing from R-4.0.0 directly, installation were successful without warning. --- Install log listed below. > install.packages("A:/R/yenlib1_1.1.0.zip", repos = NULL, type = "win.binary") WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ package ‘yenlib1’ successfully unpacked and MD5 sums checked > install.packages("aod") WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding: https://cran.rstudio.com/bin/windows/Rtools/ 嘗試 URL 'https://cran.rstudio.com/bin/windows/contrib/4.0/aod_1.3.1.zip' Content type 'application/zip' length 382219 bytes (373 KB) downloaded 373 KB package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\Rtmpsf04GM\downloaded_packages <><><> Install from R4.0.0 directly: Worked fine <><> Packages -> Install Packages (Worked fine after selecting a Mirror) > utils:::menuInstallPkgs() --- Please select a CRAN mirror for use in this session --- 嘗試 URL 'https://mirror.las.iastate.edu/CRAN/bin/windows/contrib/4.0/aod_1.3.1.zip' Content type 'application/zip' length 382219 bytes (373 KB) downloaded 373 KB package ‘aod’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\USER\AppData\Local\Temp\RtmpauyB5Q\downloaded_packages Packages -> Install Packages from local file > utils:::menuInstallLocal() package ‘yenlib1’ successfully unpacked and MD5 sums checked > On 2020/4/30 下午 04:38, Jeroen Ooms wrote: On Wed, Apr 29, 2020 at 11:37 PM Steven wrote: Hello John, Perhaps you can help me. I am an idiot. I visited the Rtools web page and learn to run the following lines in R: Still I am getting the same warning message. > writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron") > Sys.which("make") make "C:\\rtools40\\usr\\bin\\make.exe" That looks OK. Did you restart rstudio? How exactly are getting this error? Are you using install.packages() in R? Or using the pkgbuild package? Also are you running the latest version of rstudio? I think old versions may have had difficulty finding rtools40. __ 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] one-way ANOVA model, with one factor, an unbalanced design and unequal variances
Hi, i'm looking for an R function to fit a one-way ANOVA with one factor containing 10 levels. The factor levels have different numbers of observations (varying between 20 to 40). For most of the dependent variables i'm testing there are unequal variances among the factor levels. I see the function oneway.test: oneway.test(variable ~ factor, data=dataset) which by default does not assume equal variances. I also see the basic ANOVA function (aov), which, I think, is OK for unbalanced designs. I get a different F ratio, however, when using these two functions on the same data, which seems to indicate that the aov function assumes equal variances. My question: is there an R function that performs a one-way ANOVA while not assuming a balanced design *or* equal variances? Does the oneway.test function, for example, assume a balanced design? Thanks, Steven -- View this message in context: http://r.789695.n4.nabble.com/one-way-ANOVA-model-with-one-factor-an-unbalanced-design-and-unequal-variances-tp3466586p3466586.html Sent from the R help mailing list archive at Nabble.com. __ 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] Merging RData files
I ran two separate hours-long projects. Results of each were saved to two separate .RData files. Content of each includes, among others, the following: me se t p sig pc21.age 0.640 0.219 2.918 0.004 *** pc21.agesq 0.000 0.000 NaN NaN pc21.inc 0.903 0.103 8.752 0.000 *** pc21.incsq 0.000 0.000 NaN NaN pc21.sei10 0.451 0.145 3.122 0.002 *** pc21.sblkprot -4.334 3.387 1.280 0.201 ... Question: How can I combine/consolidate the two .RData files into one? Thank you. [[alternative HTML version deleted]] __ 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] Merging RData files
Hi all, This is great. Again, here is what I need. I run two separate jobs (a.R and b.R) with results (say regression outputs) going to a.RData and b.RData. I like to put all results in one place (where I can retrieve them in one place, ab.RData). The following codes do it (I am not sure if line 2 is needed but I am happy). Thank you all. load("a.RData") save.image("ab.RData") load("b.RData") save.image("ab.RData") On 1/16/2018 7:08 PM, PIKAL Petr wrote: > Huh. > > I may by completely wrong but you cannot do such "merging". .RData files are > AFAIK places where all objects from given session are stored. > > However you could load each .RData file and save/export result (one object). > > BTW, what do you mean exactly by "combine/consolidate"? > > And finally, post your questions in plain text not html, otherwise they can > be mangled. > > Cheers > Petr > >> -Original Message- >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Steven Yen >> Sent: Tuesday, January 16, 2018 9:44 AM >> To: r-help@r-project.org >> Subject: [R] Merging RData files >> >> I ran two separate hours-long projects. Results of each were saved to two >> separate .RData files. >> Content of each includes, among others, the following: >> >> mese t p sig pc21.age0.640 0.219 >> 2.918 0.004 *** >> pc21.agesq 0.000 0.000NaN NaN pc21.inc0.903 0.103 8.752 >> 0.000 >> *** pc21.incsq 0.000 0.000NaN NaN >> pc21.sei10 0.451 0.145 3.122 0.002 *** pc21.sblkprot -4.334 3.387 >> 1.280 >> 0.201 ... >> >> Question: How can I combine/consolidate the two .RData files into one? >> Thank you. >> >> >> >> >> >>[[alternative HTML version deleted]] >> >> __ >> 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. > > Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou > určeny pouze jeho adresátům. > Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně > jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze > svého systému. > Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email > jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. > Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či > zpožděním přenosu e-mailu. > > V případě, že je tento e-mail součástí obchodního jednání: > - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, > a to z jakéhokoliv důvodu i bez uvedení důvodu. > - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; > Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany > příjemce s dodatkem či odchylkou. > - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným > dosažením shody na všech jejích náležitostech. > - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost > žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně > pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu > případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je > adresátovi či osobě jím zastoupené známá. > > This e-mail and any documents attached to it may be confidential and are > intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its sender. > Delete the contents of this e-mail with all attachments and its copies from > your system. > If you are not the intended recipient of this e-mail, you are not authorized > to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage caused > by modifications of the e-mail or by delay with transfer of the email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a > contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to immediately > accept such offer; The sender of this e-mail (offer) excludes any acceptance > of the offer on the part of the recipient containing any amendment or > variation. > - the sen
Re: [R] Merging RData files
Understood. In my case, a.RData and b.RData contain identical variables/data, plus simulation outputs from separate runs. The codes deliver what I need. Good to know the three lines work. Thank you. On 1/16/2018 8:06 PM, Duncan Murdoch wrote: > On 16/01/2018 6:33 AM, Steven Yen wrote: >> Hi all, >> This is great. Again, here is what I need. I run two separate jobs (a.R >> and b.R) with results (say regression outputs) going to a.RData and >> b.RData. I like to put all results in one place (where I can retrieve >> them in one place, ab.RData). The following codes do it (I am not sure >> if line 2 is needed but I am happy). Thank you all. >> >> load("a.RData") >> save.image("ab.RData") >> load("b.RData") >> save.image("ab.RData") > > That's the same as > > load("a.RData") > load("b.RData") > save.image("ab.RData") > > because the second saved image overwrites the first one. It'll be > okay if all the variable names are different in a.RData and b.RData, > but will lose values from a.RData if any of them have the same names > as objects in b.RData. See the link I posted earlier to avoid this. > > Duncan Murdoch > >> >> On 1/16/2018 7:08 PM, PIKAL Petr wrote: >>> Huh. >>> >>> I may by completely wrong but you cannot do such "merging". .RData >>> files are AFAIK places where all objects from given session are stored. >>> >>> However you could load each .RData file and save/export result (one >>> object). >>> >>> BTW, what do you mean exactly by "combine/consolidate"? >>> >>> And finally, post your questions in plain text not html, otherwise >>> they can be mangled. >>> >>> Cheers >>> Petr >>> >>>> -Original Message- >>>> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of >>>> Steven Yen >>>> Sent: Tuesday, January 16, 2018 9:44 AM >>>> To: r-help@r-project.org >>>> Subject: [R] Merging RData files >>>> >>>> I ran two separate hours-long projects. Results of each were saved >>>> to two >>>> separate .RData files. >>>> Content of each includes, among others, the following: >>>> >>>> me se t p sig pc21.age >>>> 0.640 0.219 2.918 0.004 *** >>>> pc21.agesq 0.000 0.000 NaN NaN pc21.inc 0.903 0.103 >>>> 8.752 0.000 >>>> *** pc21.incsq 0.000 0.000 NaN NaN >>>> pc21.sei10 0.451 0.145 3.122 0.002 *** pc21.sblkprot -4.334 >>>> 3.387 1.280 >>>> 0.201 ... >>>> >>>> Question: How can I combine/consolidate the two .RData files into one? >>>> Thank you. >>>> >>>> >>>> >>>> >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> __ >>>> 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. >>> >>> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a >>> jsou určeny pouze jeho adresátům. >>> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě >>> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a >>> jeho kopie vymažte ze svého systému. >>> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento >>> email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. >>> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou >>> modifikacemi či zpožděním přenosu e-mailu. >>> >>> V případě, že je tento e-mail součástí obchodního jednání: >>> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření >>> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. >>> - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně >>> přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí >>> nabídky ze strany příjemce s dodatkem či odchylkou. >>> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve >>> výslovným dosažením shody na všech jejích náležitostech. >>> - odesílatel
[R] Problem saving .RData file with save.image
I am running R-3.0.3 on RStudio 1.1.183. I have recently gotten the following error message while saving an .RData file with the save.image command. I have not had this problem until recently. Help appreciated. === Error in save.image("bope1a.RData") : image could not be renamed and is left in bope1a.RDataTmp [[alternative HTML version deleted]] __ 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] analysis of covariance and constrained parameters
Consider an analysis of covariance involving age and cohort. The goal is to assess whether the influence of cohort depends upon the age. The simplest case involves data as follows value Age Cohort x1 1 3 x2 1 4 x3 1 5 x4 2 3 x5 2 4 x6 2 5 etc. Age is a factor. The numeric response variable is value and Cohort is a numeric predictor. So, (pseudo-code) commands to estimate the age=specific relationship between value and Cohort could be glm(value ~ Age/Cohort - 1, family =.., data = .) glm(value ~ Age/(Cohort + I(Cohort^2)) - 1, family =.., data = .). The latter commands would provide estimates of the age-specific intercept, linear, and quadratic coefficients, as in value_Age1 <- intercept_Age1 + linear_Age1*Cohort + quad_Age1*Cohort^2 value_Age2 <- intercept_Age2 + linear_Age2*Cohort + quad_Age2*Cohort^2 This is standard. One would choose among the above models via analysis of variance or AIC. Now assume that I have external knowledge that tells me that there is NO influence of Cohort on value for Age1 and that there could be up to a quadratic influence for Age2. Accordingly, I would like to fit a model which estimates these relationships: value_Age1 <- intercept_Age1 (+ 0*Cohort + 0*Cohort^2) (which is, of course, value_Age1 <- intercept_Age1) value_Age2 <- intercept_Age2 + linear_Age2*Cohort + quad_Age2*Cohort^2 What is the glm syntax to fit this model? It is a model in which we have constraints that (two) coefficients for one level of the factor must have a particular value (0) and there is no such constraint for the second level of the factor. Please note that I understand that glm(value ~ Age/(Cohort + I(Cohort^2)) - 1, family =.., data = .). generates point estimates of the linear and quadratic coefficients for Age1 (as above) and one could inspect them to determine whether they are statistically equivalent to 0. However, I want to incorporate the knowledge that these coefficients MUST BE 0 into my hypothesis testing. Knowing that these coefficients are 0 could influence the results of anova and AIC comparisons since it reduces the number of degrees of freedom associated with model. Many thanks for suggestions in advance! -- Steven Orzack Fresh Pond Research Institute 173 Harvey Street Cambridge, MA 02140 617 864-4307 www.freshpond.org __ 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] Documenting R package with Rd file
I have trouble documenting an R package. In my .Rd file (sixth line below), I have uhat<-m%*%y but when the package is built (successfully), the matrix multiplication part does not show up in the documentation. The line become (missing %*% y) uhat<-m === \examples{ x<-c(1,2,3,4,5) y<-c(1,1,2,2,4) x<-cbind(1,x) m<-mmat(x) uhat<-m%*%y dstat(uhat) } -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Problem building binary data into library file
I build binary data files into a library by placing the .rda files at the proper \data sub-folder before building the library with the following procedure: 1. File -> Open project 2. Build -> Build binary packages. This has worked up to 3 .rda files. Now, I add another .rda file to the folder. I received a very strange, seemingly unrelated warning message that says: Warning: object '.Random.seed' is created by more than one data call The weird thing about this is, repeating the procedure, I sometime can access the data file from the library (i.e., by data(filename)), other time I cannot access the data file. The data file is big but not so big (571 KB). Any clues? Why am I getting that warning message? == Below is a log with more details. ==> Rcmd.exe INSTALL --build --preclean yenlib3 * installing to library 'C:/Users/syen01/Documents/R/win-library/3.4' * installing *source* package 'yenlib3' ... ** R ** data *** moving datasets to lazyload DB Warning: object '.Random.seed' is created by more than one data call ** preparing package for lazy loading ** help *** installing help indices converting help for package 'yenlib3' finding HTML links ... done aidshtml all.variables html (truncated) ate.boprobitE html ** building package indices ** testing if installed package can be loaded *** arch - i386 *** arch - x64 * MD5 sums packaged installation of 'yenlib3' as yenlib3_1.1.0.zip * DONE (yenlib3) In R CMD INSTALL Binary package written to Y:/ [[alternative HTML version deleted]] __ 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] Loop Function to Create Multiple Scatterplots
Hello, I am trying to create multiple scatter plot graphs. I have 1 independent variable (Age - weeks post conception) and 18 dependent variables ("Gene n" Expression) in one csv file. Is there a way to set up a looped function to produce 18 individual scatterplots? At the moment, I am writing the plot() function out 18 times to make the 18 graphs. My code is below and csv file is attached. *Code* wd <- setwd("~/Dropbox/Steve/SM Research Projects/Allen Brain Bank Project/Allen Brain Bank Inflammatory Markers Project Matlab:R/Other/2018 Tests") list.files(wd) mydata <- read.csv("Glutamate.Genes.V1.csv") mydata plot(mydata$Age..weeks.post.conception., mydata$GluA1..GRIA1..Expression) plot(mydata$Age..weeks.post.conception., mydata$GluA2..GluR2.GRIA2..Expression), etc Thank you for your time and help. Regards, Steven __ 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] Package installation
I have had trouble installing packages (e.g., car, aod) in some computers (such as computers in the student lab) but no problem in my own laptop. Installation typically goes through, but after I got out and back in R (and RStudios), the error message says "packages xxx not available". That is, earlier installation of the packages did not stay (despite the successful installation message). I went back as far as R3.0.3 and there is no problem. Does this tell anyone what may be going on? Thanks. -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Content Management System Built on R
Hi everyone, I'm still new to R, and I like that's it's so compact, you can do so much in just a few lines of code. I wondered if there is any Content Management System built on R. I have created websites for my clients, and I prefer to set it up in a CMS instead and give them control to edit their own content. So far the CMS I like the best is DotNetNuke, recently they call it DNN. I also tried Joomla, but I didn't like that is not very easy to find the content page to edit. In DNN, you just navigate to the page, press Edit, and you get in to edit mode (instead of searching through all your articles, like in Joomla). It would be nice to have a similar platform and I could use R in the back end. I searched on Google and I found a CMS called "RSuite CMS", but on their website they say is based on MarkLogic, and I haven't heard mentioning anything about R. Not sure where the "R" in the name "RSuite" comes from. I also remember seeing another CMS where you could drag modules/widgets on the page around and place them anywhere you want. It was very flexible. But I can't remember the name. But it wasn't related to R. Let me know if you know any user friendly CMS built on R. Thanks, Steven [[alternative HTML version deleted]] __ 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] Matrix multiplication
I need to have all elements of a matrix multiplied by a weight before being post-multiplied by itself, as shown in the forst block of codes below. I can also multiply the matrix by the square root of the weight and then take the outer product. Actually, what I need is this. Denote each row of the matrix by a row vector as xi and each element of the weighting vector as wi. Then, I need the sum of wi * t(xi) %*% xi over i. Any suggestion for a compact approach would be appreciated. set.seed(76543211) w<-1:10; w a<-matrix(rpois(20,2),nrow=10); a b<-a a<-w*a t(a)%*%b set.seed(76543211) a<-matrix(rpois(20,2),nrow=10); a a<-sqrt(w)*a; a t(a)%*%a On 1/4/2017 5:41 PM, Steven Yen wrote: > I need help with gls{nlme}. > Specifically, I am estimating an equation with AR(1) using > maximum-likelihood. I am not understanding the correlationoption > below. Help appreciated. > > === > library(nlme) > eq1<-log(chnimp)~log(chempi)+log(gas)+log(rtwex)+befile6+ > affile6+afdec6 > reg1<-gls(eq1,data=mydata,correlation=corAR1(),method="ML",verbose=T) > [[alternative HTML version deleted]] __ 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] Matrix multiplication
OK Thanks. Your response made me think. Here (the last line) is what I need: set.seed(76543211) w<-1:10; w a<-matrix(rpois(20,2),nrow=10); a t(w*a)%*%a On 6/8/2017 12:09 PM, Jeff Newmiller wrote: > Is this a question? You seem to have three possible calculations, have > already implemented two of them (?) and it is unclear (to me) what you think > the right answer for any of them is supposed to be. > -- Sent from my phone. Please excuse my brevity. On June 7, 2017 > 8:50:55 PM PDT, Steven Yen wrote: >> I need to have all elements of a matrix multiplied by a weight before >> being post-multiplied by itself, as shown in the forst block of codes >> below. I can also multiply the matrix by the square root of the weight >> and then take the outer product. >> >> Actually, what I need is this. Denote each row of the matrix by a row >> vector as xi and each element of the weighting vector as wi. Then, I >> need the sum of wi * t(xi) %*% xi over i. >> >> Any suggestion for a compact approach would be appreciated. >> >> set.seed(76543211) >> w<-1:10; w >> a<-matrix(rpois(20,2),nrow=10); a >> b<-a >> a<-w*a >> t(a)%*%b >> >> set.seed(76543211) >> a<-matrix(rpois(20,2),nrow=10); a >> a<-sqrt(w)*a; a >> t(a)%*%a [[alternative HTML version deleted]] __ 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] Grep command
What is a good way to grep multiple strings (say in a vector)? In the following, I grep ants, cats, and fox separately and concatenate them, is there a way to grep the trio in one action? Thanks. all<-c("ants","birds","cats","dogs","elks","fox"); all [1] "ants" "birds" "cats" "dogs" "elks" "fox" some<-c("ants","cats","fox"); some [1] "ants" "cats" "fox" j<-c( grep(some[1],all,value=F), grep(some[2],all,value=F), grep(some[3],all,value=F)); j; all[j] [1] 1 3 6 [1] "ants" "cats" "fox" [[alternative HTML version deleted]] __ 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] Element-by-element multiplication
Dear R users: > # p is a vector if length 10 > # a is a vector if length 3 > # I like to create a matrix with > # the first column being p multiplied by a[1] > # the second column being p multiplied by a[2] > # the third column being p multiplied by a[3] > # The following would do that: > > a<-c(10,100,1000); a [1] 10 100 1000 > p<-matrix(1:10,nrow=10); p [,1] [1,]1 [2,]2 [3,]3 [4,]4 [5,]5 [6,]6 [7,]7 [8,]8 [9,]9 [10,] 10 > cbind(a[1]*p,a[2]*p,a[3]*p) [,1] [,2] [,3] [1,] 10 100 1000 [2,] 20 200 2000 [3,] 30 300 3000 [4,] 40 400 4000 [5,] 50 500 5000 [6,] 60 600 6000 [7,] 70 700 7000 [8,] 80 800 8000 [9,] 90 900 9000 [10,] 100 1000 1 > > # Gauss does it easily with an element-by-element > # multiplicationa.*p > # How can I do this in R? > [[alternative HTML version deleted]] __ 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] Element-by-element operation (adding)
Hi all, need help below. Thank you. > # Matrix v is 5 x 3 > # Vector b is of length 3 > # I like to add b[1] to all element in v[,1] > # I like to add b[2] to all element in v[,2] > # I like to add b[3] to all element in v[,3] > # as follows > v<-matrix(0,nrow=5,ncol=3); v [,1] [,2] [,3] [1,]000 [2,]000 [3,]000 [4,]000 [5,]000 > b<-c(0.1,0.2,0.3) > cbind( + (b[1]+v[,1]), + (b[2]+v[,2]), + (b[3]+v[,3])) [,1] [,2] [,3] [1,] 0.1 0.2 0.3 [2,] 0.1 0.2 0.3 [3,] 0.1 0.2 0.3 [4,] 0.1 0.2 0.3 [5,] 0.1 0.2 0.3 > # I am obviously not using sapply correctly: > as.data.frame(sapply(b,"+",v)) V1 V2 V3 1 0.1 0.2 0.3 2 0.1 0.2 0.3 3 0.1 0.2 0.3 4 0.1 0.2 0.3 5 0.1 0.2 0.3 6 0.1 0.2 0.3 7 0.1 0.2 0.3 8 0.1 0.2 0.3 9 0.1 0.2 0.3 10 0.1 0.2 0.3 11 0.1 0.2 0.3 12 0.1 0.2 0.3 13 0.1 0.2 0.3 14 0.1 0.2 0.3 15 0.1 0.2 0.3 [[alternative HTML version deleted]] __ 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] read.fortran format
Dear fellow R users: I am reading a data (ascii) file with fortran fixed format, containing multiple records. R does not recognize fortran's record break (a slash). I tried to do the following but it does not work. Help appreciated. 60 FORMAT(1X,F6.0,5F8.6/1X,5F8.4,F10.6/1X,2F6.0,3E15.9,F8.0,F5.2,F5.3 * /1X,F7.0,2E15.9,F9.4,F5.3) mydata<-read.fortran("G:/Journals/Disk1/12_restat_95/estimate/GROUPD.DAT", c("1X","F6.0","5F8.6"/"1X","5F8.4","F10.6" /"1X","2F6.0","3E15.9","F8.0","F5.2","F5.3" /"1X","F7.0","2E15.9","F9.4","F5.3"), col.names=c("year","w1","w2","w3","w4","w5","w6","v1","v2","v3", "v4","v5","v6","z","chyes","chno","ec","vc","cvc", "pop","ahs","fah","tnh","eq","vq","ups","zm1 ")) [[alternative HTML version deleted]] __ 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] read.fortran format
Thanks John. That helped, but I got a mixed of good thing and bad thing. Good is R does not like the scientific number format "3E15.9" but I was able to read with alphanumerical format "3A15" (and convert to numerical). Bad is R does not like the numbers .1234, .2345 without the zeros before the decimal points. My data look like: 1950. .614350 .026834 .087227 .006821 .180001 .084766 The first variable was read correctly, followed by six 0's. As the instructions say, this fortran format is approximation at best and in this case, a poort approximation. On 5/27/2016 2:21 PM, John McKown wrote: > On Fri, May 27, 2016 at 12:56 PM, Steven Yen <mailto:sye...@gmail.com>>wrote: > > Dear fellow R users: > I am reading a data (ascii) file with fortran fixed format, containing > multiple records. R does not recognize fortran's record break (a > slash). > I tried to do the following but it does not work. Help appreciated. > > 60 > FORMAT(1X,F6.0,5F8.6/1X,5F8.4,F10.6/1X,2F6.0,3E15.9,F8.0,F5.2,F5.3 > * /1X,F7.0,2E15.9,F9.4,F5.3) > > mydata<-read.fortran("G:/Journals/Disk1/12_restat_95/estimate/GROUPD.DAT", > > c("1X","F6.0","5F8.6"/"1X","5F8.4","F10.6" > /"1X","2F6.0","3E15.9","F8.0","F5.2","F5.3" > /"1X","F7.0","2E15.9","F9.4","F5.3"), > > col.names=c("year","w1","w2","w3","w4","w5","w6","v1","v2","v3", > "v4","v5","v6","z","chyes","chno","ec","vc","cvc", > "pop","ahs","fah","tnh","eq","vq","ups","zm1 ")) > > > Did you see this from ?read.fortran > > > > For a single-line record, ‘format’ should be a character vector. >For a multiline record it should be a list with a character vector >for each line. > > > > I think (not sure) you need: > > mydata<-read.frotran("G:/Journals/Disk1/12_restat_95/estimate/GROUPD.DAT", > list(c("1X","F6.0","5F8.6"),c("1X","5F8.4","F10.6"),c("1X","2F6.0","3E15.9","F8.0","F5.2","F5.3"),c("1X","F7.0","2E15.9","F9.4","F5.3")). > > col.names=c("year","w1","w2","w3","w4","w5","w6","v1","v2","v3", > "v4","v5","v6","z","chyes","chno","ec","vc","cvc", > "pop","ahs","fah","tnh","eq","vq","ups","zm1 ")) > > > > > > -- > The unfacts, did we have them, are too imprecisely few to warrant our > certitude. > > Maranatha! <>< > John McKown [[alternative HTML version deleted]] __ 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] read.fortran format
That's great, John. Your mother told you when you were born? How am I supposed to know? Thank you both. The following format statement did it!! I just change F5.3 to F5, 5F8.4 to 5F8. I also change 2E15.9 to 2A9, and then use the following as.numeric to convert the alphanumerical to numerical. Thank you!!! mydata<-read.fortran("GROUPC.DAT", list(c("1X","F6","5F8"), c("1X","5F8","F10"), c("1X","2F6","3A15","F8","F5","F5"), c("1X","F7","2A15","F9","F5")), col.names=c("year","w1","w2","w3","w4","w5","v1","v2","v3", "v4","v5","m","chyes","chno","ec","vc","cvc", "pop","ahs","fah","tnh","eq","vq","ups","zm1")) mydata$ec <-as.numeric(mydata$ec) On 5/27/2016 6:33 PM, William Dunlap wrote: > It has been a while since I used Fortran formatted input, but the > following, > without dots in the format, works: > > > txt <- "1950. .614350 .026834 .087227 .006821 .180001 4.56E-2" > > print(read.fortran(textConnection(txt), c("f5", "6f8")), digits=10) > V1 V2 V3 V4 V5 V6 V7 > 1 1950 0.61435 0.026834 0.087227 0.006821 0.180001 0.0456 > > > If I recall correctly, a dot in the format pushes the decimal point: > > > print(read.fortran(textConnection(txt), c("f5", "6f8.3")), > digits=10) > V1 V2 V3 V4V5 V6 V7 > 1 1950 0.00061435 2.6834e-05 8.7227e-05 6.821e-06 0.000180001 4.56e-05 > > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com <http://tibco.com> > > On Fri, May 27, 2016 at 3:15 PM, Steven Yen <mailto:sye...@gmail.com>> wrote: > > Thanks John. That helped, but I got a mixed of good thing and bad > thing. > Good is R does not like the scientific number format "3E15.9" but > I was > able to read with alphanumerical format "3A15" (and convert to > numerical). Bad is R does not like the numbers .1234, .2345 > without the > zeros before the decimal points. My data look like: > >1950. .614350 .026834 .087227 .006821 .180001 .084766 > > The first variable was read correctly, followed by six 0's. > > As the instructions say, this fortran format is approximation at best > and in this case, a poort approximation. > > On 5/27/2016 2:21 PM, John McKown wrote: > > On Fri, May 27, 2016 at 12:56 PM, Steven Yen <mailto:sye...@gmail.com> > > <mailto:sye...@gmail.com <mailto:sye...@gmail.com>>>wrote: > > > > Dear fellow R users: > > I am reading a data (ascii) file with fortran fixed format, > containing > > multiple records. R does not recognize fortran's record break (a > > slash). > > I tried to do the following but it does not work. Help > appreciated. > > > > 60 > > FORMAT(1X,F6.0,5F8.6/1X,5F8.4,F10.6/1X,2F6.0,3E15.9,F8.0,F5.2,F5.3 > > * /1X,F7.0,2E15.9,F9.4,F5.3) > > > > > > mydata<-read.fortran("G:/Journals/Disk1/12_restat_95/estimate/GROUPD.DAT", > > > > c("1X","F6.0","5F8.6"/"1X","5F8.4","F10.6" > > /"1X","2F6.0","3E15.9","F8.0","F5.2","F5.3" > > /"1X","F7.0","2E15.9","F9.4","F5.3"), > > > > col.names=c("year","w1","w2","w3","w4","w5","w6","v1","v2","v3", > > "v4","v5","v6","z","chyes","chno","ec","vc","cvc", > > "pop","ahs","fah","tnh","eq","vq","ups","zm1 ")) > > > > > > Did you see this from ?read.fortran > > > > > > > > For a single-line record, ‘format’ should be a character vector. > >For a multiline record it should be a list with a character
[R] Reading csv file with missing value
I read a csv file (with read.csv) containing missing values (as shown below). Is there a convenient way to set these NA into zeros? Better yet, is there an option to assign zeros to these blank cells in reading the csv file? Thank you! NA -1 NA NA NA 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA -1 NA NA NA NA NA [[alternative HTML version deleted]] __ 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] t-test for regression estimate
test option for linearHypothesis in library(car) include "Chisq" and "F". I prefer a simple t-test so that I can retrieve the standard error. Any options other than linearHypothesis to test the linear hypothesis (with 1 restriction/degree of freedom)? > summary(ols1) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.200130.09199 -2.176 0.0298 * age 0.040540.01721 2.355 0.0187 * suburb 0.019110.05838 0.327 0.7435 smcity -0.299690.19175 -1.563 0.1184 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > linearHypothesis(ols1,"suburb") Linear hypothesis test Hypothesis: suburb = 0 Model 1: restricted model Model 2: polideo ~ age + suburb + smcity Res.DfRSS Df Sum of Sq F Pr(>F) 1888 650.10 2887 650.02 1 0.078534 0.1072 0.7435 [[alternative HTML version deleted]] __ 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] t-test for regression estimate
Thanks John. Reason is I am doing linear transformations of many coefficients (e.g., bi / scalar). Of course I can uncover the t-statistic from the F statistic and then the standard error. Simply scaling the estimated coefficients I can also transform the standard errors. I have since found deltaMethod from library "car" useful. Its just that, if linearHypothesis had provide the standard errors and t-statistics then the operation would have been easier, with a one-line command for each coefficient. Thank you again. On 6/28/2016 6:28 PM, Fox, John wrote: > Dear Steven, > > The reason that linearHypothesis() computes a Wald F or chisquare test rather > than a t or z test is that the (numerator) df for the linear hypothesis need > not be 1. > > In your case (as has been pointed out) you can get the coefficient standard > error directly from the model summary. > > More generally, with some work, you could solve for the the SE for a 1 df > linear hypothesis in terms of the value of the linear function of > coefficients and the F or chisquare. That said, I'm not sure why you want to > do this. > > I hope this helps, > John > > - > John Fox, Professor > McMaster University > Hamilton, Ontario > Canada L8S 4M4 > Web: socserv.mcmaster.ca/jfox > > >> -Original Message- >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Steven Yen >> Sent: June 28, 2016 9:27 AM >> To: R-help >> Subject: [R] t-test for regression estimate >> >> test option for linearHypothesis in library(car) include "Chisq" and "F". I >> prefer >> a simple t-test so that I can retrieve the standard error. >> Any options other than linearHypothesis to test the linear hypothesis (with 1 >> restriction/degree of freedom)? >> >> > summary(ols1) >> >> Coefficients: >> Estimate Std. Error t value Pr(>|t|) >> (Intercept) -0.200130.09199 -2.176 0.0298 * >> age 0.040540.01721 2.355 0.0187 * >> suburb 0.019110.05838 0.327 0.7435 >> smcity -0.299690.19175 -1.563 0.1184 >> --- >> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 >> >> > linearHypothesis(ols1,"suburb") >> Linear hypothesis test >> >> Hypothesis: >> suburb = 0 >> >> Model 1: restricted model >> Model 2: polideo ~ age + suburb + smcity >> >> Res.DfRSS Df Sum of Sq F Pr(>F) >> 1888 650.10 >> 2887 650.02 1 0.078534 0.1072 0.7435 >> >> >> [[alternative HTML version deleted]] >> >> __ >> 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. [[alternative HTML version deleted]] __ 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] t-test for regression estimate
Thanks John. Yes, by using verbose=T, I get the value of the hypothesis. But tell me again, how would I get the variance (standard error)? On 6/29/2016 11:56 AM, Fox, John wrote: > Dear Steven, > > OK -- that makes sense, and there was also a previous request for > linearHypothesis() to return the value of the hypothesis and its covariance > matrix. In your case, where there's only 1 numerator df, that would be the > value and estimated sampling variance of the hypothesis. > > I've now implemented that, using (at least provisionally) attributes in the > development version of the car package on R-Forge, which you should be able > to install via install.packages("car", repos="http://R-Forge.R-project.org";). > Then see ?linearHypothesis for more information. > > Best, > John > >> -Original Message- >> From: Steven Yen [mailto:sye...@gmail.com] >> Sent: June 28, 2016 3:44 PM >> To: Fox, John >> Cc: R-help >> Subject: Re: [R] t-test for regression estimate >> >> Thanks John. Reason is I am doing linear transformations of many coefficients >> (e.g., bi / scalar). Of course I can uncover the t-statistic from the F >> statistic and >> then the standard error. Simply scaling the estimated coefficients I can also >> transform the standard errors. I have since found deltaMethod from library >> "car" useful. Its just that, if linearHypothesis had provide the standard >> errors >> and t-statistics then the operation would have been easier, with a one-line >> command for each coefficient. Thank you again. >> >> >> On 6/28/2016 6:28 PM, Fox, John wrote: >> >> >> Dear Steven, >> >> The reason that linearHypothesis() computes a Wald F or chisquare >> test rather than a t or z test is that the (numerator) df for the linear >> hypothesis >> need not be 1. >> >> In your case (as has been pointed out) you can get the coefficient >> standard error directly from the model summary. >> >> More generally, with some work, you could solve for the the SE for a 1 >> df linear hypothesis in terms of the value of the linear function of >> coefficients >> and the F or chisquare. That said, I'm not sure why you want to do this. >> >> I hope this helps, >> John >> >> - >> John Fox, Professor >> McMaster University >> Hamilton, Ontario >> Canada L8S 4M4 >> Web: socserv.mcmaster.ca/jfox >> >> >> >> -Original Message- >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf >> Of Steven Yen >> Sent: June 28, 2016 9:27 AM >> To: R-help <mailto:r-help@r- >> project.org> >> Subject: [R] t-test for regression estimate >> >> test option for linearHypothesis in library(car) include "Chisq" >> and "F". I prefer >> a simple t-test so that I can retrieve the standard error. >> Any options other than linearHypothesis to test the linear >> hypothesis (with 1 >> restriction/degree of freedom)? >> >> > summary(ols1) >> >> Coefficients: >> Estimate Std. Error t value Pr(>|t|) >> (Intercept) -0.200130.09199 -2.176 0.0298 * >> age 0.040540.01721 2.355 0.0187 * >> suburb 0.019110.05838 0.327 0.7435 >> smcity -0.299690.19175 -1.563 0.1184 >> --- >> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 >> >> > linearHypothesis(ols1,"suburb") >> Linear hypothesis test >> >> Hypothesis: >> suburb = 0 >> >> Model 1: restricted model >> Model 2: polideo ~ age + suburb + smcity >> >> Res.DfRSS Df Sum of Sq F Pr(>F) >> 1888 650.10 >> 2887 650.02 1 0.078534 0.1072 0.7435 >> >> >> [[alternative HTML version deleted]] >> >> __ >> 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 >> 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@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] t-test for regression estimate
Also, Is there a way to get the second command (hypothesis defined with externally scalars) below to work? Thanks. linearHypothesis(U,"0.5*eq1_DQ+0.3*eq2_DQ",verbose=T) w1<-0.5; w2<-0.3 linearHypothesis(U,"w1*eq1_DQ+w2*eq2_DQ",verbose=T) # does not work On 6/29/2016 12:38 PM, Steven Yen wrote: > Thanks John. Yes, by using verbose=T, I get the value of the > hypothesis. But tell me again, how would I get the variance (standard > error)? > > On 6/29/2016 11:56 AM, Fox, John wrote: >> Dear Steven, >> >> OK -- that makes sense, and there was also a previous request for >> linearHypothesis() to return the value of the hypothesis and its covariance >> matrix. In your case, where there's only 1 numerator df, that would be the >> value and estimated sampling variance of the hypothesis. >> >> I've now implemented that, using (at least provisionally) attributes in the >> development version of the car package on R-Forge, which you should be able >> to install via install.packages("car", >> repos="http://R-Forge.R-project.org";). Then see ?linearHypothesis for more >> information. >> >> Best, >> John >> >>> -Original Message- >>> From: Steven Yen [mailto:sye...@gmail.com] >>> Sent: June 28, 2016 3:44 PM >>> To: Fox, John >>> Cc: R-help >>> Subject: Re: [R] t-test for regression estimate >>> >>> Thanks John. Reason is I am doing linear transformations of many >>> coefficients >>> (e.g., bi / scalar). Of course I can uncover the t-statistic from the F >>> statistic and >>> then the standard error. Simply scaling the estimated coefficients I can >>> also >>> transform the standard errors. I have since found deltaMethod from library >>> "car" useful. Its just that, if linearHypothesis had provide the standard >>> errors >>> and t-statistics then the operation would have been easier, with a one-line >>> command for each coefficient. Thank you again. >>> >>> >>> On 6/28/2016 6:28 PM, Fox, John wrote: >>> >>> >>> Dear Steven, >>> >>> The reason that linearHypothesis() computes a Wald F or chisquare >>> test rather than a t or z test is that the (numerator) df for the linear >>> hypothesis >>> need not be 1. >>> >>> In your case (as has been pointed out) you can get the coefficient >>> standard error directly from the model summary. >>> >>> More generally, with some work, you could solve for the the SE for a 1 >>> df linear hypothesis in terms of the value of the linear function of >>> coefficients >>> and the F or chisquare. That said, I'm not sure why you want to do this. >>> >>> I hope this helps, >>> John >>> >>> - >>> John Fox, Professor >>> McMaster University >>> Hamilton, Ontario >>> Canada L8S 4M4 >>> Web: socserv.mcmaster.ca/jfox >>> >>> >>> >>> -Original Message- >>> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf >>> Of Steven Yen >>> Sent: June 28, 2016 9:27 AM >>> To: R-help <mailto:r-help@r- project.org> >>> Subject: [R] t-test for regression estimate >>> >>> test option for linearHypothesis in library(car) include "Chisq" >>> and "F". I prefer >>> a simple t-test so that I can retrieve the standard error. >>> Any options other than linearHypothesis to test the linear >>> hypothesis (with 1 >>> restriction/degree of freedom)? >>> >>> > summary(ols1) >>> >>> Coefficients: >>> Estimate Std. Error t value Pr(>|t|) >>> (Intercept) -0.200130.09199 -2.176 0.0298 * >>> age 0.040540.01721 2.355 0.0187 * >>> suburb 0.019110.05838 0.327 0.7435 >>> smcity -0.299690.19175 -1.563 0.1184 >>> --- >>> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 >>> >>> > linearHypothesis(ols1,"suburb") >>> Linear hypothesis test >>> >>> Hypothesis: >>>
[R] Column product
A is a 5 x 3 matrix and a is a 3-vector. I like to exponentiate A[,1] to a[1], A[,2] to a[2], and A[,3] to a[3], and obtain the product of the resulting columns, as in line 3. I also accomplish this with lines 4 and 5. I like to have rowProducts(B) but there is not so I came up with something ugly in line 5--exponentiating the row sums of log. Is there a more elegant way than than line 5 or, better yet, lines 4 and 5 together? Thanks. A<-matrix(1:15,nrow=5,byrow=F); A a<-c(1,2,3) (A[,1]^a[1])*(A[,2]^a[2])*(A[,3]^a[3]) B<-t(t(A)^a); B exp(rowSums(log(B))) Result: > A<-matrix(1:15,nrow=5,byrow=F); A [,1] [,2] [,3] [1,]16 11 [2,]27 12 [3,]38 13 [4,]49 14 [5,]5 10 15 > a<-c(1,2,3) > (A[,1]^a[1])*(A[,2]^a[2])*(A[,3]^a[3]) [1] 47916 169344 421824 889056 1687500 > B<-t(t(A)^a); B [,1] [,2] [,3] [1,]1 36 1331 [2,]2 49 1728 [3,]3 64 2197 [4,]4 81 2744 [5,]5 100 3375 > exp(rowSums(log(B))) [1] 47916 169344 421824 889056 1687500 > [[alternative HTML version deleted]] __ 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] Build command in library(devtools)
I recently updated my R and RStudio to the latest version and now the binary option in the "build" command in devtools stops working. I went around and used the binary=F option which worked by I get the .tar.gz file instead of the .zip file which I prefer. Does anyone understand the following error message: status 127 running 'zip' failed === setwd("A:/R/yenlib/"); library(devtools) #build("yenlib",binary=T) # Thisfailed with an error message build("yenlib",binary=F) # This works > build("yenlib",binary=T) "C:/PROGRA~1/R/R-33~1.1/bin/x64/R" --no-site-file \ --no-environ --no-save --no-restore --quiet CMD INSTALL \ "A:\R\yenlib\yenlib" --build * installing to library 'C:/Users/syen01/AppData/Local/Temp/Rtmp8A7KEw/temp_libpath4074149a528e' * installing *source* package 'yenlib' ... ** R ** data ** preparing package for lazy loading ** help *** installing help indices ** building package indices ** testing if installed package can be loaded *** arch - i386 *** arch - x64 * MD5 sums Warning: running command '"zip" -r9Xq "A:/R/yenlib/yenlib_16.3.zip" yenlib' had status 127 running 'zip' failed * DONE (yenlib) [1] "A:/R/yenlib/yenlib_16.3.zip" > [[alternative HTML version deleted]] __ 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] Build command in library(devtools)
Thanks. I found the reason was Rtools does not run under the new version of R. I had to go back to as early as R 3.0.2 (September 2013) to make Rtools work. Any idea for a go-around? Thanks. On 7/19/2016 4:38 PM, John McKown wrote: > On Tue, Jul 19, 2016 at 3:15 PM, Steven Yen <mailto:sye...@gmail.com>>wrote: > > I recently updated my R and RStudio to the latest version and now the > binary option in the "build" command in devtools stops working. > > I went around and used the binary=F option which worked by I get the > .tar.gz file instead of the .zip file which I prefer. > > Does anyone understand the following error message: > > status 127 > running 'zip' failed > > > I'm not totally sure, but I think that means that R cannot find the > "zip" program in order to run it. > > > === > setwd("A:/R/yenlib/"); library(devtools) > #build("yenlib",binary=T) # Thisfailed with an error message > build("yenlib",binary=F) # This works > > > build("yenlib",binary=T) > "C:/PROGRA~1/R/R-33~1.1/bin/x64/R" --no-site-file \ >--no-environ --no-save --no-restore --quiet CMD INSTALL \ >"A:\R\yenlib\yenlib" --build > > * installing to library > 'C:/Users/syen01/AppData/Local/Temp/Rtmp8A7KEw/temp_libpath4074149a528e' > * installing *source* package 'yenlib' ... > ** R > ** data > ** preparing package for lazy loading > ** help > *** installing help indices > ** building package indices > ** testing if installed package can be loaded > *** arch - i386 > *** arch - x64 > * MD5 sums > Warning: running command '"zip" -r9Xq "A:/R/yenlib/yenlib_16.3.zip" > yenlib' had status 127 > running 'zip' failed > * DONE (yenlib) > [1] "A:/R/yenlib/yenlib_16.3.zip" > > > > > [[alternative HTML version deleted]] > > __ > 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 > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > > > -- > "Worry was nothing more than paying interest on a loan that a man may > never borrow" > > From: "Quest for the White Wind" by Alan Black > > Maranatha! <>< > John McKown [[alternative HTML version deleted]] __ 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] Build command in library(devtools)
Here is what I found. I had to go back to as early as R 3.0.3 (March, 2014) along with Rtools30.exe that works with that version of R, in order for devtools to work right. With other/later version of R, I end up building a package with library(devtools); build("yenlib",binary=F) with no error message but the package does not run correctly; or with library(devtools); build("yenlib",binary=T) which deliver an error that says zip command failed (bevtools calls Rtools when binary=T). Updated versions are good, but what's the use if they do not work for a situation like this. Any help/insight would be appreciated. On 7/20/2016 10:08 AM, Steven Yen wrote: > On 7/19/2016 4:38 PM, John McKown wrote: >> On Tue, Jul 19, 2016 at 3:15 PM, Steven Yen > <mailto:sye...@gmail.com>>wrote: >> >> I recently updated my R and RStudio to the latest version and >> now the >> binary option in the "build" command in devtools stops working. >> >> I went around and used the binary=F option which worked by I get >> the >> .tar.gz file instead of the .zip file which I prefer. >> >> Does anyone understand the following error message: >> >> status 127 >> running 'zip' failed >> >> >> I'm not totally sure, but I think that means that R cannot find the >> "zip" program in order to run it. [[alternative HTML version deleted]] __ 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] PDF extraction with tm package
Hi R users, I’m having some issues trying to extract texts from PDF file using tm package. Here are the steps that were carried out: 1. Downloaded and installed the following programs: - Xpdf (Copied the ‘bin32’, ‘bin64’, ‘doc’ folders into ‘C:\Program Files\Xpdf’ directory; also added C:\Program Files\Xpdf\bin64\pdfinfo.exe & C:\Program Files\Xpdf\bin64\pdftotext.exe in existing PATH - Tesseract - Imagemagick 2. Used the following scripts and the corresponding error messages: # Directory where PDF files are stored >cname <- getwd() >Corpus(DirSource(cname), readerControl=list(reader = readPDF)) Error in system2("pdftotext", c(control$text, shQuote(x), "-"), stdout = TRUE) : '"pdftotext"' not found In addition: Warning message: running command '"pdfinfo" "C:\Users\R_Files\XXX.pdf"' had status 127 >file.exists(Sys.which(c("pdfinfo","pdftpotext"))) [1] FALSE FALSE It seems like R can’t find pdfinfo & pdftotext exe files, but not sure as to why this would be the case despite xpdf files being copied into ‘C:\Program Files’ (Im using Windows 7 64bits) I’m aware that ‘pdf_text’ function from pdftools package can extract texts from PDF file and outputs into a string. But I was after something which is able to convert PDF (ie transaction data) into a dataframe without regular expression. Is tm package capable of doing this conversion? Are there any other alternatives to these methods? Your expertise in resolving this problem would be highly appreciated. Steve [[alternative HTML version deleted]] __ 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] Converting string to data frame
Hi R users, I would like to convert a string into a data frame by creating a separator (ie pipe) between each potential fields (then using *read.table* function). ie. Here is the dummy input data for illustration (4 x 5) Date Type Description InOutNet 1/1/2016 ShareShare margin 1234 600 100 500 1/1/2016 Bond Govnt LTM 0 3/1/2016 JPY RTOP123 500 500 5/1/2016 Cash Margin C123 50 -500 The following *dat* object was read from PDF file and stored as a string (which requires to be converted to data frame) > *dat* [1] “1/01/2016” Share Share margin 1234 600 100 500” [2] “1/01/2016” Bond Govt LTM 0” [3] “3/01/2016” JPY appre RTOP124 500 500” [4] “5/01/2016” Cash Margin call 50 -50” > *class(dat)* [1] “character” Are there any effective ways (ie functions) to insert a pipe as a separator between the fields (including the empty field) like the following desired outcome? [1] “1/01/2016” | Share|Share margin 1234 |600 | 100 | 500” [2] “1/01/2016” | Bond |Govt LTM |||0” [3] “3/01/2016” | |JPY appre RTOP124 |500 ||500” [4] “5/01/2016” | Cash |Margin call ||50 |-50” I was trying *gsub *function to insert a pipe between the fields and everything appeared to be fine except when the pipe wasn’t inserted as intended for the empty fields (ie as there are 5 fields, there should be 5 pipes, but this isn’t the case for records with empty field). Any suggestion would be much appreciated. Thanks. Steven [[alternative HTML version deleted]] __ 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] poLCA - Latent Class Analysis - How long should analysis take?
This is the script i am working from. library(poLCA) f <- cbind(bq70,bq72_1,bq72_2,bq72_3,bq72_4,bq72_5,bq72_6,bq72_7,bq73a_1,bq73a_2,bq73a_3,bq73a_4)~ zq88+zq89+dm_zq101_2+dm_zq101_3+dm_zq101_4+dm_zq101_5+dm_zq101_6+dm_zq101_7+dm_zq101_8+dm_zq101_9 for(i in 2:14){max_II<--100 min_bic<-10 for(j in 1:1024){ res<-poLCA(f,BESDATA,nclass=i,maxiter =1000,tol = 1e-5,na.rm=FALSE,probs.start=NULL,nrep=1,verbose=TRUE,calc.se=TRUE) if(res bichttps://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] Read text file
How do I read a block of space-delimited numbers into a column vector using the read.table command? Thank you. -- Steven Yen __ 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] Call to a function
Dear Members I have a data frame as generated below. I like to be able to call a function both with a vector and a vector (mydata$v1) in that data frame (v1). The first call works, but the second does not. Can someone help me with the second call? Thanks!! --- mydata<-data.frame(matrix(1:20,ncol=2)) colnames(mydata) <-c("v1","v2") summary(mydata) aaa<-function(data,w=w){ if(is.vector(w)){ out<-mean(w) } else { out<-mean(data[wt]) } return(out) } aaa(mydata,mydata$v1) aaa(mydata,"v1") # want this call to work --- Printout with error message mydata<-data.frame(matrix(1:20,ncol=2)) colnames(mydata) <-c("v1","v2") summary(mydata) v1 v2 Min. : 1.00 Min. :11.00 1st Qu.: 3.25 1st Qu.:13.25 Median : 5.50 Median :15.50 Mean : 5.50 Mean :15.50 3rd Qu.: 7.75 3rd Qu.:17.75 Max. :10.00 Max. :20.00 aaa<-function(data,w=w){ + if(is.vector(w)){ + out<-mean(w) + } else { + out<-mean(data[wt]) + } + return(out) + } aaa(mydata,mydata$v1) [1] 5.5 aaa(mydata,"v1") [1] NA Warning message: In mean.default(w) : argument is not numeric or logical: returning NA __ 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] Call to a function
Thanks! From this I learn the much needed class statement if (class(wt)=="character") wt <- x[, wt] which serves my need in a bigger project. Steven Yen On 6/23/2015 6:20 PM, boB Rudis wrote: You can do something like: aaa <- function(data, w=w) { if (class(w) %in% c("integer", "numeric", "double")) { out <- mean(w) } else { out <- mean(data[, w]) } return(out) } (there are some typos in your function you may want to double check, too) On Tue, Jun 23, 2015 at 5:39 PM, Steven Yen wrote: mydata<-data.frame(matrix(1:20,ncol=2)) colnames(mydata) <-c("v1","v2") summary(mydata) aaa<-function(data,w=w){ if(is.vector(w)){ out<-mean(w) } else { out<-mean(data[wt]) } return(out) } aaa(mydata,mydata$v1) aaa(mydata,"v1") # want this call to work -- Steven Yen My e-mail alert: https://youtu.be/9UwEAruhyhY?list=PLpwR3gb9OGHP1BzgVuO9iIDdogVOijCtO __ 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] 'class(.) == **' [was 'Call to a function']
Thanks to all for the help. I have learned much about "inherit" and "class". I like to know about one additional option, and that is to use a calling parameter without the quotation marks, similar to the linear regression syntax: lm(data=mydata,weights=wt) Below is a simple set of codes to calculate weighted means with generated data in data frame "mydata". As annotated below, I like the following call to work (without the quotations): wmean(mydata,wt=weight) Thank you! mydata<-matrix(1:20,ncol=2) mydata<-cbind(mydata,runif(10,0,1)) colnames(mydata)<-c("y","x","weight") mydata<-as.data.frame(mydata) wmean <- function(data,wt){ if (inherits(wt,what="character")) wt<-data[,wt] wt<-wt/mean(wt) Mean<-NULL for (i in 1:ncol(data)){ Mean[i] <- sum(data[,i]*wt)/sum(wt) } list("Mean: ",Mean) } wmean(mydata,wt="weight") # This works wmean(mydata,wt=weight) # <= Like this to work reg<-lm(data=mydata,weights=weight) # ? lm On 6/24/2015 3:20 AM, Martin Maechler wrote: Steve Taylor on Wed, 24 Jun 2015 00:56:26 + writes: > Note that objects can have more than one class, in which case your == and %in% might not work as expected. > Better to use inherits(). > cheers, > Steve Yes indeed, as Steve said, really do! The use of (class(.) == "") it is error prone and against the philosophy of classes (S3 or S4 or ..) in R : Classes can "extend" other classes or "inherit" from them; S3 examples in "base R" are - glm() objects which are "glm" but also inherit from "lm" - multivariate time-series are "mts" and "ts" - The time-date objects POSIXt , POSIXct, POSIXlt ==> do work with inherits(, , ) We've seen this use of class(.) == ".."(or '!=" or %in% ...) in too many places; though it may work fine in your test cases, it is wrong to be used in generality e.g. inside a function you provide for more general use, and is best replaced with the use of inherits() / is() everywhere "out of principle". Martin Maechler ETH Zurich -- Steven Yen My e-mail alert: https://youtu.be/9UwEAruhyhY?list=PLpwR3gb9OGHP1BzgVuO9iIDdogVOijCtO __ 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] 'class(.) == **' [was 'Call to a function']
Thanks Davis. But actually, the line is legitimate: if (inherits(wt,what="character")) wt<-data[,wt] because, coming down with wt being characters, the part wt<-data[,wt] then picks up variables data$wt. The call wmean(mydata,wt="weight") actually goes OK. I was hoping to figure out a way to fix the wmean routine some how so that I can call with wmean(mydata,wt=weight) Good to know there is a better way to initialize the vector Mean and and a better list command. Thank you! On 6/26/2015 2:39 AM, David Winsemius wrote: On Jun 25, 2015, at 7:48 PM, Steven Yen wrote: Thanks to all for the help. I have learned much about "inherit" and "class". I like to know about one additional option, and that is to use a calling parameter without the quotation marks, similar to the linear regression syntax: lm(data=mydata,weights=wt) Below is a simple set of codes to calculate weighted means with generated data in data frame "mydata". As annotated below, I like the following call to work (without the quotations): wmean(mydata,wt=weight) Let's start with the call. If you are to execute this, then names `mydata` and `weight` each must have a value. Thank you! mydata<-matrix(1:20,ncol=2) OK. There is a value having been assigned to `mydata` mydata<-cbind(mydata,runif(10,0,1)) And now augmented. colnames(mydata)<-c("y","x","weight") And a names attribute added for its columns. mydata<-as.data.frame(mydata) wmean <- function(data,wt){ if (inherits(wt,what="character")) wt<-data[,wt] wt<-wt/mean(wt) Here's the problem. If `wt` was of mode "character", then you cannot divide it by a number, since the RHS will be evaluated first. You really should read the error messages! Perhaps you meant: wt <- data[, wt]/mean(data[ , wt] But if you did, then it's rather confusing (but possible) to assign the value to the same name as the column of the matrix. Mean<-NULL Why do that? If you remove it from the workspace then you cannot assign a value using indexed assignment as you apparently intend to do. Should have been Mean <- numeric( ncol(data) ) for (i in 1:ncol(data)){ Mean[i] <- sum(data[,i]*wt)/sum(wt) There is a bit of a confusion here. `wt` started out as a character value. I guess you could do this. } list("Mean: ",Mean) Wrong syntax for lists. Suspect you want list(Mean=Mean) } wmean(mydata,wt="weight") # This works wmean(mydata,wt=weight) # <= Like this to work So were you planning to execute this first? weight="weight" #? -- Steven Yen My e-mail alert: https://youtu.be/9UwEAruhyhY?list=PLpwR3gb9OGHP1BzgVuO9iIDdogVOijCtO __ 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] Judging if a matrix contains any NA
How do I judge if a matrix contain any NA or otherwise non-missing, non-numerical? In the following, I would like to deliver ONE logical of TRUE or FALSE, rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > a<-matrix(1:16,nrow=4) > diag(a)<-NA > a [,1] [,2] [,3] [,4] [1,] NA59 13 [2,]2 NA 10 14 [3,]37 NA 15 [4,]48 12 NA > is.na(a) [,1] [,2] [,3] [,4] [1,] TRUE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE TRUE FALSE [4,] FALSE FALSE FALSE TRUE __ 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] Element-by-element division
I need help with element-by-element division. Below, matrices a and c are both 5 x 2 and element-by-element division works as (I) expected. What if matrix is 1 by 2: to divide first column of a by b[1] and second column of a by b[2]. I had to go around (two ways) to make it work. In Gauss, these can be dine by a./b and a./c. Any such simple way in R? Thank! > a<-matrix(1:10,nrow=5); a [,1] [,2] [1,]16 [2,]27 [3,]38 [4,]49 [5,]5 10 > b<-matrix(c(0.5,0.25),nrow=1); b [,1] [,2] [1,] 0.5 0.25 > c<-matrix(rep(c(0.5,0.25),5),nrow=5,byrow=T); c [,1] [,2] [1,] 0.5 0.25 [2,] 0.5 0.25 [3,] 0.5 0.25 [4,] 0.5 0.25 [5,] 0.5 0.25 > one<-a/c; one [,1] [,2] [1,]2 24 [2,]4 28 [3,]6 32 [4,]8 36 [5,] 10 40 > two<-a/b Error in a/b : non-conformable arrays > two<-cbind(a[,1]/b[1],a[,2]/b[2]); two [,1] [,2] [1,]2 24 [2,]4 28 [3,]6 32 [4,]8 36 [5,] 10 40 > b2<-matrix(rep(b,5),nrow=5,byrow=T); b2 [,1] [,2] [1,] 0.5 0.25 [2,] 0.5 0.25 [3,] 0.5 0.25 [4,] 0.5 0.25 [5,] 0.5 0.25> a/b2 [,1] [,2] [1,]2 24 [2,]4 28 [3,]6 32 [4,]8 36 [5,] 10 40 [[alternative HTML version deleted]] __ 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] x11() hangs in 3.2.1
I'm on RHEL 6.6, R version 3.2.1 Patched (2015-07-30 r68761) -- "World-Famous Astronaut". Issuing the x11() command hangs R and does not complete. A window is partially drawn then freezes. Does anyone know of a solution? Thanks, Steve -- Steven J. BackusComputer Systems Manager University of Utah E-Mail: steven.bac...@utah.edu Genetic EpidemiologyAlternate: bac...@math.utah.edu 391 Chipeta Way -- Suite D Office: 801.587.9308 Salt Lake City, UT 84108-1266 http://www.math.utah.edu/~backus __ 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] x11() hangs in 3.2.1
marc_schwa...@me.com writes: > First, just an FYI, that this would be better posted to R-SIG-Fedora: > https://stat.ethz.ch/mailman/listinfo/r-sig-fedora Thanks, I'll give it a try. > Can you run: > capabilities() > in a terminal session and see what it shows for X11: Yes, here is the output: jpeg pngtiff tcltk X11aqua TRUETRUETRUE FALSETRUE FALSE http/ftp sockets libxmlfifo cledit iconv TRUETRUETRUETRUETRUETRUE NLS profmem cairo ICU long.double libcurl TRUE FALSETRUE FALSETRUE FALSE > Did you install R via local compilation? Yes, local compilation. > Presuming local compilation, I would check your configure and build > logs for warnings/errors. It is possible that you are missing an > X11 header or lib someplace. The config.log file has lots of errors but nothing obviously related to this problem. Thanks for your help, Steve -- Steven J. BackusComputer Systems Manager University of Utah E-Mail: steven.bac...@utah.edu Genetic EpidemiologyAlternate: bac...@math.utah.edu 391 Chipeta Way -- Suite D Office: 801.587.9308 Salt Lake City, UT 84108-1266 http://www.math.utah.edu/~backus __ 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] Splitting lines in R script
I have a line containing summation of four components. # This works OK: p<-pbivnorm(bb,dd,tau)+pbivnorm(aa,cc,tau)- -pbivnorm(aa,dd,tau)-pbivnorm(bb,cc,tau) # This produces unpredicted results without warning: p<-pbivnorm(bb,dd,tau)+pbivnorm(aa,cc,tau) -pbivnorm(aa,dd,tau)-pbivnorm(bb,cc,tau) Is there a general rule of thumb for line breaks? Thanks you. [[alternative HTML version deleted]] __ 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] Checking if a logical variable exists
My obj does not always come with a logical variable defined. So I do my.foo <- function(obj,df,digits=5){ if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher ... } This works when "Fisher" is defined in/passed from obj. When it is not, I get error: Error in (!is.na("obj$spec$Fisher")) & Fisher : operations are possible only for numeric, logical or complex types I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks. __ 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] Checking if a logical variable exists
Thanks. This worked!! :) Fisher <- ifelse(!("Fisher" %in% names(obj$spec)), FALSE, obj$spec$Fisher) Steven At 08:43 AM 12/14/2014, Ben Tupper wrote: Hi, Does this work for you? It simply tests if the name Fisher is found among the names of the elements of spec. obj = list(spec = list) Fisher <- ifelse(!("Fisher" %in% names(obj$spec)), FALSE, obj$spec$Fisher) Cheers, Ben On Dec 14, 2014, at 8:07 AM, Steven Yen wrote: > My obj does not always come with a logical variable defined. So I do > > my.foo <- function(obj,df,digits=5){ > if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher > ... > } > > This works when "Fisher" is defined in/passed from obj. When it is not, I get error: > > Error in (!is.na("obj$spec$Fisher")) & Fisher : > operations are possible only for numeric, logical or complex types > > I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks. > > __ > 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. Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org __ 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] Automating regression
How do I specify the type of regression in calling a procedure/ In the following I call the procedure to do a probit regression. Of course, I can change "probit" into "lm" in procedure "myreg" to do a linear regression. My question is, how do I automate this (choice of lm or probit) in calling "myreg", with a proper input (e.g., model=lm)? Thank you. --- eq1<-d~sex+age+children b<-myreg(eq1,data=mydata); summary(b) myreg<-function(formula,data){ data<-model.frame(formula,data) reg<-probit(formula,data=data) return(reg) } __ 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] Matrix element-by-element multiplication
I like to multiple the first and second column of a 10 x 3 matrix by 100. The following did not work. I need this in an operation with a much larger scale. Any help? aa<-matrix(1:30,nrow=10,ncol=3); aa bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb dim(aa) dim(bb) aa*bb Results: > aa<-matrix(1:30,nrow=10,ncol=3); aa [,1] [,2] [,3] [1,]1 11 21 [2,]2 12 22 [3,]3 13 23 [4,]4 14 24 [5,]5 15 25 [6,]6 16 26 [7,]7 17 27 [8,]8 18 28 [9,]9 19 29 [10,] 10 20 30 > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb [,1] [,2] [,3] [1,] 100 1001 > dim(aa) [1] 10 3 > dim(bb) [1] 1 3 > aa*bb Error in aa * bb : non-conformable arrays > __ 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] Matrix element-by-element multiplication
Thank you both. Both John and Peter's suggestions work great!! At 06:17 PM 1/7/2015, John McKown wrote: >On Wed, Jan 7, 2015 at 5:05 PM, Steven Yen ><<mailto:sye...@gmail.com>sye...@gmail.com> wrote: >I like to multiple the first and second column >of a 10 x 3 matrix by 100. The following did not >work. I need this in an operation with a much larger scale. Any help? > >aa<-matrix(1:30,nrow=10,ncol=3); aa >bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb >dim(aa) >dim(bb) >aa*bb > >Results: > > > aa<-matrix(1:30,nrow=10,ncol=3); aa >� � � [,1] [,2] [,3] >� [1,]� � 1� � 11� � 21 >� [2,]� � 2� � 12� � 22 >� [3,]� � 3� � 13� � 23 >� [4,]� � 4� � 14� � 24 >� [5,]� � 5� � 15� � 25 >� [6,]� � 6� � 16� � 26 >� [7,]� � 7� � 17� � 27 >� [8,]� � 8� � 18� � 28 >� [9,]� � 9� � 19� � 29 >[10,]� � 10� � 20� � 30 > > bb<-matrix(c(100,100,1),nrow=1,ncol=3); bb >� � � [,1] [,2] [,3] >[1,]� 100� 100� � 1 > > dim(aa) >[1] 10� 3 > > dim(bb) >[1] 1 3 > > aa*bb >Error in aa * bb : non-conformable arrays > > > > > >Assuming that this is exactly what you want to do, then� > >aa[,1:2]<-aa[,1:2]*100; > >transcript: > > > aa<-matrix(1:30,nrow=10,ncol=3); > > aa > [,1] [,2] [,3] > [1,]1 11 21 > [2,]2 12 22 > [3,]3 13 23 > [4,]4 14 24 > [5,]5 15 25 > [6,]6 16 26 > [7,]7 17 27 > [8,]8 18 28 > [9,]9 19 29 >[10,] 10 20 30 > > aa[,1:2]<-aa[,1:2]*100 > > aa > [,1] [,2] [,3] > [1,] 100 1100 21 > [2,] 200 1200 22 > [3,] 300 1300 23 > [4,] 400 1400 24 > [5,] 500 1500 25 > [6,] 600 1600 26 > [7,] 700 1700 27 > [8,] 800 1800 28 > [9,] 900 1900 29 >[10,] 1000 2000 30 > > > > >� > >-- > >While a transcendent vocabulary is laudable, one >must be eternally careful so that the calculated >objective of communication does not become >ensconced in obscurity.� In other words, eschew obfuscation. > >111,111,111 x 111,111,111 = 12,345,678,987,654,321 > >Maranatha! <>< >John McKown [[alternative HTML version deleted]] __ 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] Package corpcor: Putting symmetric matrix entries in vector
Dear I use sm2vec from package corpcor to puts the lower triagonal entries of a symmetric matrix (matrix A) into a vector. However, sm2vec goes downward (columnwise, vector B), but I would like it to go across (rowwise). So I define a vector to re-map the vector (vector C). This works. But is there a short-cut (simpler way)? Thank you. > A<-cor(e); A [,1] [,2][,3][,4] [,5][,6] [1,] 1. 0.5240809 0.47996616 0.11200672 -0.1751103 -0.09276455 [2,] 0.52408090 1.000 0.54135982 -0.15985028 -0.2627738 -0.14184545 [3,] 0.47996616 0.5413598 1. -0.06823105 -0.2046897 -0.23815967 [4,] 0.11200672 -0.1598503 -0.06823105 1. 0.2211311 0.08977677 [5,] -0.17511026 -0.2627738 -0.20468966 0.22113112 1.000 0.23567235 [6,] -0.09276455 -0.1418455 -0.23815967 0.08977677 0.2356724 1. > B<-sm2vec(A); B [1] 0.52408090 0.47996616 0.11200672 -0.17511026 -0.09276455 [6] 0.54135982 -0.15985028 -0.26277383 -0.14184545 -0.06823105 [11] -0.20468966 -0.23815967 0.22113112 0.08977677 0.23567235 > jj<-c(1,2,6,3,7,10,4,8,11,13,5,9,12,14,15) > C<-B[jj]; C [1] 0.52408090 0.47996616 0.54135982 0.11200672 -0.15985028 [6] -0.06823105 -0.17511026 -0.26277383 -0.20468966 0.22113112 [11] -0.09276455 -0.14184545 -0.23815967 0.08977677 0.23567235 __ 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] Package corpcor: Putting symmetric matrix entries in vector
Great! Thanks. Thanks to all who tried to help. as.vector(r[upper.tri(r)]) does it: > e<-as.matrix(cbind(u1,u2,u3,v1,v2,v3)) > r<-cor(e); r [,1] [,2][,3][,4] [,5][,6] [1,] 1. 0.5240809 0.47996616 0.11200672 -0.1751103 -0.09276455 [2,] 0.52408090 1.000 0.54135982 -0.15985028 -0.2627738 -0.14184545 [3,] 0.47996616 0.5413598 1. -0.06823105 -0.2046897 -0.23815967 [4,] 0.11200672 -0.1598503 -0.06823105 1. 0.2211311 0.08977677 [5,] -0.17511026 -0.2627738 -0.20468966 0.22113112 1.000 0.23567235 [6,] -0.09276455 -0.1418455 -0.23815967 0.08977677 0.2356724 1. > as.vector(r[upper.tri(r)]) [1] 0.52408090 0.47996616 0.54135982 0.11200672 -0.15985028 -0.06823105 [7] -0.17511026 -0.26277383 -0.20468966 0.22113112 -0.09276455 -0.14184545 [13] -0.23815967 0.08977677 0.23567235 At 06:56 PM 1/30/2015, Peter Langfelder wrote: If you have a symmetric matrix, you can work with the upper triangle instead of the lower one, and you get what you want by simply using as.vector(A[upper.tri(A)]) Example: > a = matrix(rnorm(16), 4, 4) > A = a + t(a) > A [,1] [,2] [,3][,4] [1,] 0.3341294 0.5460334 -0.4388050 1.09415343 [2,] 0.5460334 0.1595501 0.3907721 0.24021833 [3,] -0.4388050 0.3907721 -0.4024922 -1.62140865 [4,] 1.0941534 0.2402183 -1.6214086 0.03987924 > as.vector(A[upper.tri(A)]) [1] 0.5460334 -0.4388050 0.3907721 1.0941534 0.2402183 -1.6214086 No need to play with potentially error-prone index vectors; upper.tri does that for you. Hope this helps, Peter On Fri, Jan 30, 2015 at 3:03 PM, Steven Yen wrote: > Dear > I use sm2vec from package corpcor to puts the lower triagonal entries of a > symmetric matrix (matrix A) into a vector. However, sm2vec goes downward > (columnwise, vector B), but I would like it to go across (rowwise). So I > define a vector to re-map the vector (vector C). This works. But is there a > short-cut (simpler way)? Thank you. > >> A<-cor(e); A > [,1] [,2][,3][,4] [,5][,6] > [1,] 1. 0.5240809 0.47996616 0.11200672 -0.1751103 -0.09276455 > [2,] 0.52408090 1.000 0.54135982 -0.15985028 -0.2627738 -0.14184545 > [3,] 0.47996616 0.5413598 1. -0.06823105 -0.2046897 -0.23815967 > [4,] 0.11200672 -0.1598503 -0.06823105 1. 0.2211311 0.08977677 > [5,] -0.17511026 -0.2627738 -0.20468966 0.22113112 1.000 0.23567235 > [6,] -0.09276455 -0.1418455 -0.23815967 0.08977677 0.2356724 1. >> B<-sm2vec(A); B > [1] 0.52408090 0.47996616 0.11200672 -0.17511026 -0.09276455 > [6] 0.54135982 -0.15985028 -0.26277383 -0.14184545 -0.06823105 > [11] -0.20468966 -0.23815967 0.22113112 0.08977677 0.23567235 >> jj<-c(1,2,6,3,7,10,4,8,11,13,5,9,12,14,15) >> C<-B[jj]; C > [1] 0.52408090 0.47996616 0.54135982 0.11200672 -0.15985028 > [6] -0.06823105 -0.17511026 -0.26277383 -0.20468966 0.22113112 > [11] -0.09276455 -0.14184545 -0.23815967 0.08977677 0.23567235 > > __ > 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-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] Date extract Year
My date column is the excel numeric date format. For instance, in excel, that first date is 11 September 2012. On Mar 8, 2015, at 10:41 AM, Frederic Ntirenganya wrote: > Hi Steve, > > Can you please explain to us your date column? thanks. > > Regards, > Fredo. > On Mar 8, 2015 7:32 PM, "Steve Archambault" wrote: > Hi all, > > I am trying in vain to create a new object "Year" in my data frame from > existing Date data. I have tried many different approaches, but can't seem > to get it to work. Here is an example of some code I tried. > > date1<- as.Date(wells$Date,"%m/%d/%Y") > wells$year<-as.numeric(format(date1, "%Y")) > > I am starting with data that looks like this. > > ID Date DepthtoWater_bgs test test2 > 1 BC-0004 41163 260.603 1 > 2 BC-0004 41255 261.654 2 > 3 BC-0003 41345 166.585 3 > 4 BC-0002 41351 317.856 4 > 5 BC-0004 41355 262.157 5 > 6 BC-0003 41438 167.558 6 > 7 BC-0004 41438 265.459 7 > 8 BC-0002 41443 317.25 10 8 > 9 BC-0002 41521 321.25 11 9 > 10 BC-0003 41522 168.65 1210 > 11 BC-0004 41522 266.15 1311 > 12 BC-0003 41627 168.95 1412 > 13 BC-0004 41627 265.25 1513 > 14 BC-0002 41634 312.31 1614 > 15 BC-0003 41703 169.25 1715 > 16 BC-0004 41703 265.05 1816 > 17 BC-0002 41710 313.01 1917 > 18 BC-0003 41795 168.85 2018 > 19 BC-0004 41795 266.95 2119 > 20 BC-0002 41801 330.41 2220 > 21 BC-0003 41905 169.75 2321 > 22 BC-0004 41905 267.75 2422 > 23 BC-0002 41906 321.01 2523 > > Any help would be greatly appreciated! > > > -Steve > > __ > 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. [[alternative HTML version deleted]] __ 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] Panel Data--filling in missing dates in a span only
Hi folks, I have this panel data (below), with observations missing in each of the panels. I want to fill in years for the missing data, but only those years within the span of the existing data. For instance, BC-0002 needs on year, 1995. I do not want any years after the last observation. structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("BC-0002", "BC-0003", "BC-0004"), class = "factor"), Date = c(1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1996L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1996L, 1995L, 1996L, 1997L, 1998L, 2000L, 1994L, 1993L, 1999L, 1998L), DepthtoWater_bgs = c(317.85, 317.25, 321.25, 312.31, 313.01, 330.41, 321.01, 166.58, 167.55, 168.65, 168.95, 169.25, 168.85, 169.75, 260.6, 261.65, 262.15, 265.45, 266.15, 265.25, 265.05, 266.95, 267.75)), .Names = c("ID", "Date", "DepthtoWater_bgs" ), class = "data.frame", row.names = c(NA, -23L)) I have been using this code to expand the entire panels, but it is not what exactly what I want. fexp <- expand.grid(ID=unique(wells$ID), Date=unique(wells$Date)) merge(fexp, wells, all=TRUE) Any help would be much appreciated! Thanks, Steve [[alternative HTML version deleted]] __ 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] print and rbind within a for loop
Greets, I'm trying to iteratively find solutions to a problem given a range of options. Code is as follows: sim.app.wald<-function(B=0.1,min=10,max=50,alpha=0.05){ result<-c(fails=0,n=0) for(n in min:max){ x<-seq(1,n-1,1) fhat<-x/n se<-sqrt((fhat*(1-fhat))/n) ci.wald<-fhat+qnorm(1-(alpha/2))*se if(sum(ci.wald[ci.wald<=B])){ # rbind(cbind(fails=x[ci.wald<=B],n=n),result) print(cbind(X=x[ci.wald<=B],n)) } # return(result) } } If you run it like this, you can readily see what I intend to get out; which is the correct result. If you uncomment the two commented lines, nothing useful obtains. What am I missing here? This seeming codependence of print() and return() and the failure of rbind() to do anything is quite puzzling. Best Regards, Steven [[alternative HTML version deleted]] __ 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] print and rbind within a for loop
Hi Dave, Thank you, but that didn't seem to do it. This code produces the same result: sim.app.wald<-function(B=0.1,min=10,max=50,alpha=0.05){ result<-c(fails=0,n=0) for(n in min:max){ x<-seq(1,n-1,1) fhat<-x/n se<-sqrt((fhat*(1-fhat))/n) ci.wald<-fhat+qnorm(1-(alpha/2))*se if(sum(ci.wald[ci.wald<=B])){ result<-rbind(cbind(fails=x[ci.wald<=B],n=n),result) print(cbind(X=x[ci.wald<=B],n)) } return(result) } } Once again, commenting out the return() statement shows the correct output via the print() statement. Best Regards, Steven On Mar 25, 2015, at 12:51 PM, David R Forrest wrote: > From looking at it it looks like nothing is done with the results of rbind(), > and the return() then exits the loop and routine with the unmodified result. > > Dave > > From: R-help [r-help-boun...@r-project.org] on behalf of Steven LeBlanc > [ores...@gmail.com] > Sent: Wednesday, March 25, 2015 3:43 PM > To: r-help@R-project.org > Subject: [R] print and rbind within a for loop > > Greets, > > I'm trying to iteratively find solutions to a problem given a range of > options. Code is as follows: > > sim.app.wald<-function(B=0.1,min=10,max=50,alpha=0.05){ >result<-c(fails=0,n=0) >for(n in min:max){ >x<-seq(1,n-1,1) >fhat<-x/n >se<-sqrt((fhat*(1-fhat))/n) >ci.wald<-fhat+qnorm(1-(alpha/2))*se >if(sum(ci.wald[ci.wald<=B])){ > # rbind(cbind(fails=x[ci.wald<=B],n=n),result) >print(cbind(X=x[ci.wald<=B],n)) >} > # return(result) >} > } > > If you run it like this, you can readily see what I intend to get out; which > is the correct result. If you uncomment the two commented lines, nothing > useful obtains. > > What am I missing here? This seeming codependence of print() and return() and > the failure of rbind() to do anything is quite puzzling. > > Best Regards, > Steven >[[alternative HTML version deleted]] > > __ > 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. [[alternative HTML version deleted]] __ 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] print and rbind within a for loop
Hi Dave, Bill, Thank you. That was it! Best Regards, Steven On Mar 25, 2015, at 1:11 PM, David R Forrest wrote: > I'm away from a computer that can check syntax, but make sure the return is > outside of the for loop. To my eye it looks inside. [[alternative HTML version deleted]] __ 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] Problem with Rtools version 3.5.0.4
I am trying to build an R package with Rtools version 3.5.0.4 along with R-3.5.1 using the following sequence of commands: File -> Open Project -> Build -> Build Binary Package I received the following error message: zip I/O error: No such file or directory zip error: Temporary file failure (Y:/ziPu2G1b) running 'zip' failed I then removed Rtools version 3.5.0.4 and installed Rtools version 3.4. It worked. What am I missing? Or, shall I wait till the next version of Rtools? Full log file below. ==> Rcmd.exe INSTALL --build --preclean yenlib3 * installing to library 'C:/Users/syen01/Documents/R/win-library/3.5' * installing *source* package 'yenlib3' ... ** R ** data *** moving datasets to lazyload DB ** byte-compile and prepare package for lazy loading ** help *** installing help indices converting help for package 'yenlib3' finding HTML links ... done aids html all.variables html ate.boprobit html (list truncated) ate.boprobitE0 html zxcombined html ** building package indices ** testing if installed package can be loaded *** arch - i386 *** arch - x64 * MD5 sums zip I/O error: No such file or directory zip error: Temporary file failure (Y:/ziPu2G1b) running 'zip' failed * DONE (yenlib3) In R CMD INSTALL Binary package written to Y:/ -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Polygon
Can someone help me with polygon. The following codes are self-runnable and mark a shaded area under the standard normal curve in the x-range (-3,-1). Is there a way to also mark the area in (1,3), at the same time. That is, I want shaded areas in both tails. Thank you... === # Create data for the area to shade cord.x <- c(-3,seq(-3,-1,0.01),-1) cord.y <- c(0,dnorm(seq(-3,-1,0.01)),0) # Make a curve curve(dnorm(x,0,1), xlim=c(-3,3), main='Standard Normal') # Add the shaded area. polygon(cord.x,cord.y,col='skyblue') -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Polygon
Great. Thanks! It did wonders. 1. Is there a way to suppress the obvious tick stops (-3,-2,-1,0,1,2,3) and mark only the ticks -1.96 and 1.96. 2. Better yet, to draw vertical lines at x = -1.96 and x = 1.96. Thanks. Steven On 10/3/2018 12:51 AM, Rui Barradas wrote: > Hello, > > Continue with > > > polygon(-rev(cord.x), rev(cord.y), col = 'skyblue') > > > Hope this helps, > > Rui Barradas > > Às 17:25 de 02/10/2018, Steven Yen escreveu: >> Can someone help me with polygon. The following codes are self-runnable >> and mark a shaded area under the standard normal curve in the x-range >> (-3,-1). >> Is there a way to also mark the area in (1,3), at the same time. >> That is, I want shaded areas in both tails. Thank you... >> >> === >> # Create data for the area to shade >> cord.x <- c(-3,seq(-3,-1,0.01),-1) >> cord.y <- c(0,dnorm(seq(-3,-1,0.01)),0) >> >> # Make a curve >> curve(dnorm(x,0,1), xlim=c(-3,3), main='Standard Normal') >> >> # Add the shaded area. >> polygon(cord.x,cord.y,col='skyblue') >> > -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Polygon
Thanks!!! It did wonders. Steven On 10/3/2018 9:39 AM, David Winsemius wrote: >> On Oct 2, 2018, at 5:50 PM, Steven Yen wrote: >> >> Great. Thanks! It did wonders. >> 1. Is there a way to suppress the obvious tick stops (-3,-2,-1,0,1,2,3) >> and mark only the ticks -1.96 and 1.96. >> 2. Better yet, to draw vertical lines at x = -1.96 and x = 1.96. >> Thanks. > Read ?plot.default > > Then add xaxt="n" to the arguments to curve, ... and afterwards: > > axis( 1, at=c(-1.96, 1.96),lab=c(-1.96, 1.96) ) > abline( v= c(-1.96, 1.96) ) > > -- >> Steven >> >> On 10/3/2018 12:51 AM, Rui Barradas wrote: >>> Hello, >>> >>> Continue with >>> >>> >>> polygon(-rev(cord.x), rev(cord.y), col = 'skyblue') >>> >>> >>> Hope this helps, >>> >>> Rui Barradas >>> >>> Às 17:25 de 02/10/2018, Steven Yen escreveu: >>>> Can someone help me with polygon. The following codes are self-runnable >>>> and mark a shaded area under the standard normal curve in the x-range >>>> (-3,-1). >>>> Is there a way to also mark the area in (1,3), at the same time. >>>> That is, I want shaded areas in both tails. Thank you... >>>> >>>> === >>>> # Create data for the area to shade >>>> cord.x <- c(-3,seq(-3,-1,0.01),-1) >>>> cord.y <- c(0,dnorm(seq(-3,-1,0.01)),0) >>>> >>>> # Make a curve >>>> curve(dnorm(x,0,1), xlim=c(-3,3), main='Standard Normal') >>>> >>>> # Add the shaded area. >>>> polygon(cord.x,cord.y,col='skyblue') >>>> >> -- >> st...@ntu.edu.tw (S.T. Yen) >> >> >> [[alternative HTML version deleted]] >> >> __ >> 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. > David Winsemius > Alameda, CA, USA > > 'Any technology distinguishable from magic is insufficiently advanced.' > -Gehm's Corollary to Clarke's Third Law > > > > > > -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Graphing output
Is it possible to release the file destination after sending it to a pdf file? Below, line 3 send the graph to a pdf file. I like to release the devise so that I can see result produced by line for on the console (screen). Thanks. x<-1:10 pdf("test1.pdf") # pdf {grDevices} boxplot(x) # This goes to pdf file boxplot(x) # Like this to go to console -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Polygon
David, Rui, and All: Greetings. 1. I need a helping hand with the polygon statement below so that I can have the area under the curve highlighted, between (z1,z2). 2. Is it possible to label the X-axis with in two scale, in the current z-scale and another, say x = (z+5)*2? Thank you. z1<- -1 z2<- 2 curve(dnorm(x,0,1),xlim=c(-4,4),main="Standard Normal",xaxt="n",frame=F,xlab="z") jj<-seq(z1,z2,0.01) cord.x<-c(jj) cord.y<-c(dnorm(jj)) #polygon(cord.x,cord.y,col="skyblue") axis(1,at=c(-5,z1,0,z2,5),lab=c(-5,z1,0,z2,5)) abline(v=c(z1,z2)) -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Using apply
I need help with "apply". Below, I have no problem getting the column sums. 1. How do I get the sum of squares? 2. In general, where do I look up these functions? Thanks. x<-matrix(1:10,nrow=5); x sum <- apply(x,2,sum); sum [[alternative HTML version deleted]] __ 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] Printing with cat in a procedure
How do I print a matrix running a procedure? In the code below, I print with the cat command and get a vector (from A and C). A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) B<-diag(4) try5<-function(A,B){ C<-A+B cat("\nA =",A,"\nC = ",C) structure(list(A=A,B=B,C=C)) } v<-try5(A,B) v$C -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Printing with cat in a procedure
Thank you all - print works wonders. On 12/22/2018 10:36 PM, Eric Berger wrote: > Hi Steven, > Here's one way, using print > > try5<-function(A,B){ > C<-A+B > #cat("\nA =",A,"\nC = ",C) > cat("\nA = ") > print(A) > cat("\nC = ") > print(C) > structure(list(A=A,B=B,C=C)) > } > > HTH, > Eric > > > On Sat, Dec 22, 2018 at 4:32 PM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > How do I print a matrix running a procedure? In the code below, I > print > with the cat command and get a vector (from A and C). > > A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) > B<-diag(4) > > try5<-function(A,B){ > C<-A+B > cat("\nA =",A,"\nC = ",C) > structure(list(A=A,B=B,C=C)) > } > > v<-try5(A,B) > v$C > > -- > st...@ntu.edu.tw <mailto:st...@ntu.edu.tw> (S.T. Yen) > > > [[alternative HTML version deleted]] > > __ > 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 > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Random seed
I have known from the old days to set a random seed of a LARGE ODD NUMBER. Now I read instructions of set.seed and it requires ANY INTEGER. Any idea? Or, does it matter. Thanks. -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Retrievable results in a procedure
I would like to suppressed printing of retrievable results in a procedure and to print only when retrieved. In line 10 below I call procedure "try" and get matrices A,B,C all printed upon a call to the procedure. I get around this unwanted printing by calling with v<-try(A,B) as in line 11. Any way to suppress printing of the retrievable results listed in the structure command? Thank you, and Merry Christmas to all. A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) B<-diag(4) try<-function(A,B){ C<-A+B cat("\nC:\n"); print(C) structure(list(A=A,B=B,C=C)) } try(A,B)# line 10 v<-try(A,B) # line 11 -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Retrievable results in a procedure
Thanks Sarah. Below, replacing "structure" with "invisible" does wonders--that serves my need. What I want is quite simple - I call a procedure and it does two things: (1) display results for all; (2) save retrievable results for use in further analysis, e.g., in knitr. Earlier, with "structure" (or with results<-list(...)) it spits out the main results, with components repeated (printed) in a painfully long list. Yet, as I said, calling with foo<-try(...) prints the main results with the list suppressed. I am just looking for option to NOT have to call with foo<- always. There must be more ways to do this, but I am happy with invisible. Thanks again. On 12/25/2018 11:10 PM, Sarah Goslee wrote: > I'm a bit confused about what you actually want, but I think > invisible() might be the answer. > > Note that there's already a base function try() so that's not a great > name for test functions. > > Sarah > > On Tue, Dec 25, 2018 at 8:47 AM Steven Yen <mailto:st...@ntu.edu.tw>> wrote: > > I would like to suppressed printing of retrievable results in a > procedure and to print only when retrieved. > > In line 10 below I call procedure "try" and get matrices A,B,C all > printed upon a call to the procedure. I get around this unwanted > printing by calling with v<-try(A,B) as in line 11. > > Any way to suppress printing of the retrievable results listed in the > structure command? Thank you, and Merry Christmas to all. > > > A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) > B<-diag(4) > > try<-function(A,B){ > C<-A+B > cat("\nC:\n"); print(C) > structure(list(A=A,B=B,C=C)) > } > > try(A,B)# line 10 > v<-try(A,B) # line 11 > > -- > st...@ntu.edu.tw <mailto:st...@ntu.edu.tw> (S.T. Yen) > > > [[alternative HTML version deleted]] > > __ > 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 > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > -- > Sarah Goslee (she/her) > http://www.sarahgoslee.com -- st...@ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]] __ 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] Installing package into...
I install package using either the command line or Tools -> Install packages... (in RStudio) and get a non-fatal message saying... Installing package into ‘C:/Users/xuhaer/Documents/R/win-library/3.5’ (as ‘lib’ is unspecified) I know it is not a fatal message. But, is there a way to do a cleaner installation without getting such message? Thanks. -- s...@hqu.edu.cn [[alternative HTML version deleted]] __ 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] Printing vectrix
I like to print a vector, wrapped by rows of 10. Below the first command below works for 20 numbers. The second command is ugly. How can I print the 25 numbers into 2 rows of ten plus a helf row of 5? Thanks. > x<-1:20; matrix(x,nrow=2,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 11 12 13 14 15 16 17 18 19 20 > x<-1:25; matrix(x,nrow=2,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 1 2 3 4 5 6 7 8 9 10 11 12 13 [2,] 14 15 16 17 18 19 20 21 22 23 24 25 1 Warning message: In matrix(x, nrow = 2, byrow = T) : data length [25] is not a sub-multiple or multiple of the number of rows [2] > -- s...@hqu.edu.cn [[alternative HTML version deleted]] __ 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] Using apply() with functions I wrote
Hello! Does apply() not work with customized functions? Here is a simple example: AddSeven<-function(n){n+7} AddSeven(3) [1] 10 M<-matrix(nrow=2,ncol=2,data=c(1,2,3,4),byrow=TRUE) M [,1] [,2] [1,]12 [2,]34 apply(x=M,margin=c(1,2),fun=AddSeven) Error in match.fun(FUN) : argument "FUN" is missing, with no default Thanks for your help! -Steve Pfeiffer [[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] Using apply() with functions I wrote
Ugghh... Sorry for bothering you all!! (Yes, I analyzed the data for my ecology M.S. project with R, but I wasted countless hours committing silly mistakes like this.) On Thu, Jul 24, 2014 at 4:11 PM, Bert Gunter wrote: > ummm R is case sensitive! "fun" != "FUN" > > (Have you gone through any R tutorials yet? If not, please do so > before posting further). > > Cheers, > Bert > > Bert Gunter > Genentech Nonclinical Biostatistics > (650) 467-7374 > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > Clifford Stoll > > > > > On Thu, Jul 24, 2014 at 10:07 AM, Pfeiffer, Steven > wrote: > > Hello! > > > > Does apply() not work with customized functions? Here is a simple > example: > > > > AddSeven<-function(n){n+7} > > AddSeven(3) > >[1] 10 > > M<-matrix(nrow=2,ncol=2,data=c(1,2,3,4),byrow=TRUE) > > M > > [,1] [,2] > >[1,]12 > >[2,]34 > > apply(x=M,margin=c(1,2),fun=AddSeven) > >Error in match.fun(FUN) : argument "FUN" is missing, with no > default > > > > Thanks for your help! > > -Steve Pfeiffer > > > > [[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. > [[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] Overwriting a procedure
Is there a way to over-write a procedure (subroutine)? I include a default procedure fixx in a list of procedures which are compiled into a package. By default, the procedure deliver the data matrix x. fixx <- function(x){ result <- list(x=x) return(result) } In some applications, I have transformations (such as squared terms) in some column(s) in x. So I include the following procedure in the mail (calling) program, hoping to over-write the default procedure under the same name in the package (which is the way other languages works, e.g., Gauss): fixx <- function(x){ x[,6]<-x[,5]^2/10 result <- list(x=x) return(result) } This does not seem to work. The procedure in the main (calling) program seems to get ignored. Any idea? Thanks. __ 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] Overwriting a procedure
Thank you all. Various ideas led to a simple solution. I include an argument in the calling function, i.e., v.transform, default being FALSE. Function fixzx is removed entirely from the package, which is OK by default. The function is called only when v.transform=TRUE: if (v.transform){ v<-fixzx(z1,x1); z1<-v$z; x1<-v$x } Then, when I do need to transform variable(s), I include function fixx(x) in the main program (which I had meant to do anyway as transformations are problem specific): fixzx <- function(z,x){ z[,4]=z[,3]^2/10; x[,4]=z[,4]; x[,18]<-x[,17]^2/10 result <- list(z=z,x=x) return(result) } This works out nicely. The fact that a function fixx is not needed when v.transform=FALSE is very convenient. Other programming languages, e.g., Gauss, do not allow the function to be missing when v.transform=FALSE. Thank you all. At 04:41 PM 9/2/2014, Greg Snow wrote: A perhaps better approach would be to have the functions that currently call fixx accept an argument of a function to use. It could default to fixx, but if the caller passed in a new function it would use that function instead. If you really want to overwrite a function inside of a package namespace then look at the assignInNamespace function in the utils package (but note the warning in the description on the help page). On Tue, Sep 2, 2014 at 12:45 PM, Steven Yen wrote: > Is there a way to over-write a procedure (subroutine)? > > I include a default procedure fixx in a list of procedures which are > compiled into a package. By default, the procedure deliver the data matrix > x. > > fixx <- function(x){ > result <- list(x=x) > return(result) > } > > In some applications, I have transformations (such as squared terms) in some > column(s) in x. So I include the following procedure in the mail (calling) > program, hoping to over-write the default procedure under the same name in > the package (which is the way other languages works, e.g., Gauss): > > fixx <- function(x){ > x[,6]<-x[,5]^2/10 > result <- list(x=x) > return(result) > } > > This does not seem to work. The procedure in the main (calling) program > seems to get ignored. Any idea? Thanks. > > __ > 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. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com __ 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] Writing .csv file
I use the following command to write data to a .csv file: write.csv(yxz,file="foo.csv") And I get the following in the file, with one column appended to the file: "","fsp","fsec","cincome", "1",0,3,2.25,... "2",0,1,2.75,... "3",1,1,0.625,... Question: is there a way to avoid the first column? Thanks. __ 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] Writing .csv file
Wonderful. It worked like charms and I love it! Thank you Don. Steven At 05:06 PM 9/24/2014, Don McKenzie wrote: I believe you need to specify row.names = FALSE See the help for write.table() On Sep 24, 2014, at 2:00 PM, Steven Yen wrote: > I use the following command to write data to a .csv file: > >write.csv(yxz,file="foo.csv") > > And I get the following in the file, with one column appended to the file: > > "","fsp","fsec","cincome", > "1",0,3,2.25,... > "2",0,1,2.75,... > "3",1,1,0.625,... > > Question: is there a way to avoid the first column? Thanks. > > __ > 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. Don McKenzie Research Ecologist Pacific Wildland Fire Sciences Lab US Forest Service Affiliate Professor School of Environmental and Forest Sciences University of Washington d...@uw.edu __ 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] Reading text file with fortran format
Hello I read data with fortran format: mydata<-read.fortran('foo.txt', c("4F10.4","F8.3","3F3.0","20F2.0")) colnames(mydata)<-c("q1","q2","q3","q4","income","hhsize", "weekend","dietk","quart1","quart2","quart3","male","age35", "age50","age65","midwest","south","west","nonmetro", "suburb","black","asian","other","hispan","hhtype1", "hhtype2","hhtype3","emp_stat") dstat(mydata,digits=6) I produced the following sample statistics for the first 4 variables (q1,q2,q3,q4): Mean Std.dev Min Max Obs q1 0.000923 0.002509 0 0.035245 5649 q2 0.000698 0.001681 0 0.038330 5649 q3 0.000766 0.002138 0 0.040100 5649 q4 0.000373 0.001140 0 0.026374 5649 The correct sample statistics are: Variable| Mean Std.Dev. Minimum Maximum + Q1| 9.227632 25.09311 0.0 352.4508 Q2| 6.983078 16.80984 0.0 383.2995 Q3| 7.657381 21.38337 0.0 400.9950 Q4| 3.727952 11.40446 0.0 263.7398 INCOME| 16.01603 13.70296 0.0100.0 HHSIZE| 2.586475 1.464282 1.0 16.0 In other words, values for q1-q4 were scaled down by a factor of 10,000. My raw data look like (with proper format) 0.0.0.0. 48.108... 0.0.0.0. 11.640... 35.34500. 95.76560. 4.667... 0.0.0.0. 9.000... 84.4.80380.3.1886 2.923... 0.0.0.1.1636 10.000... 0. 10.7818 109.78840. 17.000... 0.7.95280.4.7829 35.000... True that the data here are space delimited. But I need to read data elsewhere where data are not space delimited. Any idea/suggestion would be appreciated. [[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] Reading text file with fortran format
Thanks to all. Steven Yen At 06:18 PM 9/30/2014, Nordlund, Dan (DSHS/RDA) wrote: > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Steven Yen > Sent: Tuesday, September 30, 2014 2:04 PM > To: r-help > Subject: [R] Reading text file with fortran format > > Hello > > I read data with fortran format: > mydata<-read.fortran('foo.txt', > c("4F10.4","F8.3","3F3.0","20F2.0")) > colnames(mydata)<-c("q1","q2","q3","q4","income","hhsize", > "weekend","dietk","quart1","quart2","quart3","male","age35", > "age50","age65","midwest","south","west","nonmetro", > "suburb","black","asian","other","hispan","hhtype1", > "hhtype2","hhtype3","emp_stat") > dstat(mydata,digits=6) > > I produced the following sample statistics for the first 4 > variables (q1,q2,q3,q4): > > Mean Std.dev Min Max Obs > q1 0.000923 0.002509 0 0.035245 5649 > q2 0.000698 0.001681 0 0.038330 5649 > q3 0.000766 0.002138 0 0.040100 5649 > q4 0.000373 0.001140 0 0.026374 5649 > > The correct sample statistics are: > Variable| Mean Std.Dev. Minimum Maximum > + >Q1| 9.227632 25.09311 0.0 352.4508 >Q2| 6.983078 16.80984 0.0 383.2995 >Q3| 7.657381 21.38337 0.0 400.9950 >Q4| 3.727952 11.40446 0.0 263.7398 >INCOME| 16.01603 13.70296 0.0100.0 >HHSIZE| 2.586475 1.464282 1.0 16.0 > > In other words, values for q1-q4 were scaled down by a factor of > 10,000. > My raw data look like (with proper format) > > 0.0.0.0. 48.108... > 0.0.0.0. 11.640... > 35.34500. 95.76560. 4.667... > 0.0.0.0. 9.000... > 84.4.80380.3.1886 2.923... > 0.0.0.1.1636 10.000... > 0. 10.7818 109.78840. 17.000... > 0.7.95280.4.7829 35.000... > > True that the data here are space delimited. But I need to read data > elsewhere where data are not space delimited. > > Any idea/suggestion would be appreciated. > The read.fortran function appears to work differently from how FORTRAN would read the data if there are already decimals points in the numbers. If memory serves, FORTRAN ignores the decimal portion of the format if it finds a decimal in what it reads. The read.fortran function appears to read the number 'as is' and then multiplies by 10^-d, where d is the number of decimal places in the format. Since you have decimals specified, you should specify the format with 0 decimal places, i.e. c("4F10.0","F8.0","3F3.0","20F2.0")) hope this is helpful, Dan Daniel J. Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services __ 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] Reading text file with fortran format
Hello Any idea how to read a text file with fortran format, WITH MULTIPLE RECORDS? My fortran format is as follows, and I do know I need to change F7.4 to F7.0, and 2F2.0 to 2I2, etc. I just have no idea how to handle the "slash" (/) which dictates a jump to the next record in fortran. Thank you all. --- 10 FORMAT(F8.0,4F2.0,6F7.4,F8.4,3F4.1,2F3.0,36F2.0,11F8.5 * /2F8.5,F10.4,F2.0,F8.1,F10.4,F11.4,F6.2,F2.0,3F10.4,2F12.7,2F2.0, * F4.0,2F2.0,F8.5,5F2.0) __ 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] Checking if a matrix exists/is defined
Hello Can someone help me with the following, specifically in judging whether a matrix exists. I have trouble with the first line below. In this case, matrix obj$hessian exists and is 74 x 74. I receive the error message: Warning message: In all(w$hessian) : coercing argument of type 'double' to logical Thank you all. --- if (!all(obj$hessian)|OPG){ vb<-obj$gradientObs; vb<-solve(t(vb)%*%vb) vb.method<-"; v(b)=inv(G'G)" } else { vb<- solve(-obj$hessian) vb.method<-"; v(b)=inv(-H)" } [[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] Checking if a matrix exists/is defined
Thank you Rich. It works like a charm! Earlier I worked around by judging its determinant: dd<-NULL if (invH) dd<-det(obj$hessian) if (invH & exists("dd")){ ... } Now I do if (!is.null(obj$hessian) & invH){ ... } which is more direct. Thanks again. Steven At 01:50 AM 10/19/2014, Richard M. Heiberger wrote: all() takes a logical argument, not numeric. See ?all I think you are looking for is.null(obj$hessian) If this isn't what you are looking for, please send a reproducible example to the entire list. Rich On Sun, Oct 19, 2014 at 12:17 AM, Steven Yen wrote: > Hello > Can someone help me with the following, specifically in judging > whether a matrix exists. I have trouble with the first line below. In > this case, matrix obj$hessian exists and is 74 x 74. I receive the > error message: > > Warning message: > In all(w$hessian) : coercing argument of type 'double' to logical > > Thank you all. > > --- > if (!all(obj$hessian)|OPG){ >vb<-obj$gradientObs; vb<-solve(t(vb)%*%vb) >vb.method<-"; v(b)=inv(G'G)" > } else { >vb<- solve(-obj$hessian) >vb.method<-"; v(b)=inv(-H)" > } > [[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-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] Selecting rows/columns of a matrix
Dear I am interested in selecting rows and columns of a matrix with a criterion defined by a binary indicator vector. Let matrix a be > a<-matrix(1:16, 4,4,byrow=T) > a [,1] [,2] [,3] [,4] [1,]1234 [2,]5678 [3,]9 10 11 12 [4,] 13 14 15 16 Elsewhere in Gauss, I select the first and third rows and columns of a by defining a column vector j = [1,0,1,0]. Then, select the rows of a using j, and then selecting the rows of the transpose of the resulting matrix using j again. I get the 2 x 2 matrix as desired. Is there a way to do this in R? below are my Gauss commands. Thank you. --- j 1 0 1 0 a=selif(a,j); a 1 2 3 4 9 10 11 12 a=selif(a',j); a 1 9 3 11 __ 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] Selecting rows/columns of a matrix
Rui Thanks. This works great. Below, I get the 2nd, 4th, and 6th rows/columns: > (a<-matrix(1:36,6,6)) [,1] [,2] [,3] [,4] [,5] [,6] [1,]17 13 19 25 31 [2,]28 14 20 26 32 [3,]39 15 21 27 33 [4,]4 10 16 22 28 34 [5,]5 11 17 23 29 35 [6,]6 12 18 24 30 36 > (j<-matrix(c(0,1,0,1,0,1))) [,1] [1,]0 [2,]1 [3,]0 [4,]1 [5,]0 [6,]1 > ((a[as.logical(j), as.logical(j)])) [,1] [,2] [,3] [1,]8 20 32 [2,] 10 22 34 [3,] 12 24 36 Steven Yen At 02:49 PM 10/26/2014, Rui Barradas wrote: Sorry, that should be t(a[as.logical(j), as.logical(j)]) Rui Barradas Em 26-10-2014 18:45, Rui Barradas escreveu: Hello, Try the following. a[as.logical(j), as.logical(j)] # or b <- a[as.logical(j), ] t(b)[as.logical(j), ] Hope this helps, Rui Barradas Em 26-10-2014 18:35, Steven Yen escreveu: Dear I am interested in selecting rows and columns of a matrix with a criterion defined by a binary indicator vector. Let matrix a be > a<-matrix(1:16, 4,4,byrow=T) > a [,1] [,2] [,3] [,4] [1,]1234 [2,]5678 [3,]9 10 11 12 [4,] 13 14 15 16 Elsewhere in Gauss, I select the first and third rows and columns of a by defining a column vector j = [1,0,1,0]. Then, select the rows of a using j, and then selecting the rows of the transpose of the resulting matrix using j again. I get the 2 x 2 matrix as desired. Is there a way to do this in R? below are my Gauss commands. Thank you. --- j 1 0 1 0 a=selif(a,j); a 1 2 3 4 9 10 11 12 a=selif(a',j); a 1 9 3 11 __ 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-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] Injecting a column of characters to a matrix of numerics
Hello I am designing a regression printout, which works out nicely. Then, I try to inject a column of characters to indicate a discrete regressor with a dot (.). Then, all numbers seem to turn into characters, in quotations. Is there a way to do this right? Below, I show the lines of codes before and after. Thanks. Steven Yen --- out<-round(cbind(me,se,t,p),digits) colnames(out)<-c("estimates","s.e.","|t-value|","p-value") rownames(out)<-rownames(me) out estimates s.e. |t-value| p-value (Intercept) 0.223263 0.146167 1.527459 0.127173 sex 0.049830 0.039612 1.257973 0.208890 age -0.070423 0.029539 2.384035 0.017433 yrmarry 0.015567 0.005298 2.938126 0.003429 children 0.060525 0.044778 1.351659 0.176993 religius-0.053128 0.014413 3.686260 0.000248 educ 0.003226 0.008453 0.381636 0.702866 occu 0.003915 0.011860 0.330147 0.741404 rating -0.077856 0.014466 5.381925 0.00 out<-round(cbind(me,se,t,p),digits); out<-cbind(out,disc) colnames(out)<-c("estimates","s.e.","|t-value|","p-value","disc") rownames(out)<-rownames(me) (Intercept) "0.223263" "0.146167" "1.527459" "0.127173" "" sex "0.04983" "0.039612" "1.257973" "0.20889" "." age "-0.070423" "0.029539" "2.384035" "0.017433" "" yrmarry "0.015567" "0.005298" "2.938126" "0.003429" "" children"0.060525" "0.044778" "1.351659" "0.176993" "." religius"-0.053128" "0.014413" "3.68626" "0.000248" "" educ"0.003226" "0.008453" "0.381636" "0.702866" "" occu"0.003915" "0.01186" "0.330147" "0.741404" "" rating "-0.077856" "0.014466" "5.381925" "0""" __ 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] Injecting a column of characters to a matrix of numerics
Wonderful. Works great! Steven Yen At 11:52 AM 10/29/2014, Kevin E. Thorpe wrote: On 10/29/2014 11:41 AM, Steven Yen wrote: Hello I am designing a regression printout, which works out nicely. Then, I try to inject a column of characters to indicate a discrete regressor with a dot (.). Then, all numbers seem to turn into characters, in quotations. Is there a way to do this right? Below, I show the lines of codes before and after. Thanks. Steven Yen --- out<-round(cbind(me,se,t,p),digits) colnames(out)<-c("estimates","s.e.","|t-value|","p-value") rownames(out)<-rownames(me) out estimates s.e. |t-value| p-value (Intercept) 0.223263 0.146167 1.527459 0.127173 sex 0.049830 0.039612 1.257973 0.208890 age -0.070423 0.029539 2.384035 0.017433 yrmarry 0.015567 0.005298 2.938126 0.003429 children 0.060525 0.044778 1.351659 0.176993 religius-0.053128 0.014413 3.686260 0.000248 educ 0.003226 0.008453 0.381636 0.702866 occu 0.003915 0.011860 0.330147 0.741404 rating -0.077856 0.014466 5.381925 0.00 out<-round(cbind(me,se,t,p),digits); out<-cbind(out,disc) colnames(out)<-c("estimates","s.e.","|t-value|","p-value","disc") rownames(out)<-rownames(me) (Intercept) "0.223263" "0.146167" "1.527459" "0.127173" "" sex "0.04983" "0.039612" "1.257973" "0.20889" "." age "-0.070423" "0.029539" "2.384035" "0.017433" "" yrmarry "0.015567" "0.005298" "2.938126" "0.003429" "" children"0.060525" "0.044778" "1.351659" "0.176993" "." religius"-0.053128" "0.014413" "3.68626" "0.000248" "" educ"0.003226" "0.008453" "0.381636" "0.702866" "" occu"0.003915" "0.01186" "0.330147" "0.741404" "" rating "-0.077856" "0.014466" "5.381925" "0""" Use data frames instead. out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,disc) names(out)<-c("estimates","s.e.","|t-value|","p-value","disc") __ 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. -- Kevin E. Thorpe Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael's Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.tho...@utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 __ 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] Removing rows in a data frame containing string in rownames
I like to remove from a data frame rows with labels containing certain string, e.g., "sex" and "rating". Below is a list of the data frame and my failed attempt to the rows. Any clues? Thanks. > out est se t p disc p.(Intercept) 26.430 13.605 1.943 0.053 p.sex 3.502 3.930 0.891 0.373 * p.children 3.693 4.521 0.817 0.414 * p.occu 0.740 1.116 0.663 0.508 p.rating -7.897 1.331 5.933 0.000 c.(Intercept) 1.861 0.965 1.929 0.054 c.sex 0.221 0.249 0.889 0.374 * c.children 0.234 0.289 0.810 0.418 * c.occu 0.052 0.079 0.663 0.508 c.rating -0.556 0.102 5.451 0.000 u.(Intercept) 1.943 1.017 1.910 0.057 u.sex 0.221 0.248 0.888 0.375 * u.children 0.229 0.276 0.827 0.409 * u.occu 0.054 0.082 0.663 0.508 u.rating -0.581 0.109 5.331 0.000 > out<-subset(out,!(names(out) %in% c("sex","rating"))) > out est se t p disc p.(Intercept) 26.430 13.605 1.943 0.053 p.sex 3.502 3.930 0.891 0.373 * p.children 3.693 4.521 0.817 0.414 * p.occu 0.740 1.116 0.663 0.508 p.rating -7.897 1.331 5.933 0.000 c.(Intercept) 1.861 0.965 1.929 0.054 c.sex 0.221 0.249 0.889 0.374 * c.children 0.234 0.289 0.810 0.418 * c.occu 0.052 0.079 0.663 0.508 c.rating -0.556 0.102 5.451 0.000 u.(Intercept) 1.943 1.017 1.910 0.057 u.sex 0.221 0.248 0.888 0.375 * u.children 0.229 0.276 0.827 0.409 * u.occu 0.054 0.082 0.663 0.508 u.rating -0.581 0.109 5.331 0.000 __ 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] Removing rows in a data frame containing string in rownames
Thank you Bill and Dennis. grepl worked great. However, for reason I am not figuring out, the code worked as I included the procedure (subroutine) with a source command, viz., source("z:\\R\\mylib\\me.R") Compiling the routine into a library/package, as I always do, then the command got ignored. Hmm... Steven At 10:22 PM 11/16/2014, William Dunlap wrote: >Try grepl() to do pattern matching in strings. � ("%in%" checks for >equality.) � E.g., using your original 'out' do >� � out[ !grepl("sex|rating", rownames(out), ] >to get all but the rows whose names contain the character sequences >"sex" or "rating". > >Bill Dunlap >TIBCO Software >wdunlap <http://tibco.com>tibco.com > >On Sun, Nov 16, 2014 at 6:31 PM, Steven Yen ><<mailto:sye...@gmail.com>sye...@gmail.com> wrote: >I like to remove from a data frame rows with >labels containing certain string, e.g., "sex" >and "rating". Below is a list of the data frame >and my failed attempt to the rows. Any clues? Thanks. > > > out >� � � � � � � � � est� � � se� � � t� � � p disc >p.(Intercept) 26.430 13.605 1.943 0.053 >p.sex� � � � � 3.502� 3.930 0.891 0.373 * >p.children� � � 3.693� 4.521 0.817 0.414 * >p.occu� � � � � 0.740� 1.116 0.663 0.508 >p.rating� � � -7.897� 1.331 5.933 0.000 >c.(Intercept)� 1.861� 0.965 1.929 0.054 >c.sex� � � � � 0.221� 0.249 0.889 0.374 * >c.children� � � 0.234� 0.289 0.810 0.418 * >c.occu� � � � � 0.052� 0.079 0.663 0.508 >c.rating� � � -0.556� 0.102 5.451 0.000 >u.(Intercept)� 1.943� 1.017 1.910 0.057 >u.sex� � � � � 0.221� 0.248 0.888 0.375 * >u.children� � � 0.229� 0.276 0.827 0.409 * >u.occu� � � � � 0.054� 0.082 0.663 0.508 >u.rating� � � -0.581� 0.109 5.331 0.000 > > > out<-subset(out,!(names(out) %in% c("sex","rating"))) > > out >� � � � � � � � � est� � � se� � � t� � � p disc >p.(Intercept) 26.430 13.605 1.943 0.053 >p.sex� � � � � 3.502� 3.930 0.891 0.373 * >p.children� � � 3.693� 4.521 0.817 0.414 * >p.occu� � � � � 0.740� 1.116 0.663 0.508 >p.rating� � � -7.897� 1.331 5.933 0.000 >c.(Intercept)� 1.861� 0.965 1.929 0.054 >c.sex� � � � � 0.221� 0.249 0.889 0.374 * >c.children� � � 0.234� 0.289 0.810 0.418 * >c.occu� � � � � 0.052� 0.079 0.663 0.508 >c.rating� � � -0.556� 0.102 5.451 0.000 >u.(Intercept)� 1.943� 1.017 1.910 0.057 >u.sex� � � � � 0.221� 0.248 0.888 0.375 * >u.children� � � 0.229� 0.276 0.827 0.409 * >u.occu� � � � � 0.054� 0.082 0.663 0.508 >u.rating� � � -0.581� 0.109 5.331 0.000 > >__ ><mailto:R-help@r-project.org>R-help@r-project.org mailing list ><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. > [[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] Multrix-vector multiplication
I had trouble with matrix multiplication when a matrix reduces to a vector. In the following, lines 1 and 2 work when matrices u and a are both of order 2. Lines 3 and 5 do not work (message is matrix not conformable) when u is (T x 1) and a is (1 x 2) and This causes a problem for users of other matrix languages such as Gauss and MATLAB. Inserting line 4 makes it work, which is annoying. But, is it proper/safe to make it work by inserting line 4? Other approaches? Thank you! 1 a<-solve(s22)%*%s21 # (2 x 2) <- (2 x 2) %*% (2 x 2) 2 uc<-u%*%v$a # (T x 2) <- (T x 2) %*% (2 x 2) 3 a<-solve(s22)%*%s21 # (1 x 2) <- (1 x 1) %*% (1 x 2) 4 a<-as.matrix(a) # This line makes it work. OK? Other approaches? 5 uc<-u%*%v$a # (T x 1) %*% (1 x 2) [[alternative HTML version deleted]] __ 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.