Re: [R] Change points in R
On Fri, 31 Jan 2014, Benjamin Ward (ENV) wrote: Hi R helpers, I have a set of data best shown in this below graph. Each coloured line represents a statistic calculated across pairs of DNA sequences. And for each coloured line, I would like to identify breakpoints - so identify the chunks where the values are high, for example, in the light blue line, there is a large high segment just after x=2e+05. From googling the aim to find such points, I've read about something called change-point analysis, used with time series data and I wondered if it or a variant of it in R might be of use here, this data is a series of % values (double), all a single measurement i.e. for each line, a 'scanner' passed over two sequences and at each step recorded the % value. Can change-point analysis help me here and if so what package or method will allow me to do this making as little assumptions about my data as possible? The graph didn't make it through but from what you describe it seems that the tilingArray package on Bioconductor would be helpful for you. See als Huber et al. (2006, Bioinformatics, 22(16), 1963-1970). Other useful packages on CRAN include the packages: bcp, changepoint, cpm, segmented and strucchange (among others). Thanks in advance, Ben W. [X] [[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] Fit Negbin glm model with autoregressive correlation structure
Hello, I am attempting to estimate the effect of various variables on the time-series of counts of reported cattle stillbirths. We investigate the effect of day-of-week, month, holidays etc...and also the effect of non-temporal variables. We performed model comparisons between Gaussian glm, Poisson glm and negbin glm and the latter seems most appropriate for our data. We found that the residuals from our best model are not i.i.d. but follow an autoregressive process of order 5 , AR (5). I therefore wish to re-run this model after adding an AR(5) correlation structure in order to get unbiased estimates and standard errors for the variables retained in the model. In the past, I have been faced with a similar situation for a Gaussian glm and used the gls function with a corStruct object describing the within-group correlation structure. However, this would not work with our negbin model. Looking around on various help forums, I came across the possibilities of using generalized estimating equations instead. The gee function (in gee package) has a corstr object which would allow me to specify an AR process of whichever order but there is no option to include a negbin family. The geeglm function (in geepack package) does recognized the negbin family but only gives an option to fit an AR(1) correlation structure. The corstr object seems to have a userdefined option but it is unclear how it could be defined for an AR(5) process. In short, my questions are: *is it possible to include an AR (p) correlation structure directly into a negbin glm? How? * if GEE are the way forward, how can the the corstr object in geeglm be defined for an AR(p) process? Thank you for your suggestions, Dr Flavie Vial Veterinary Public Health Institute DCR-VPH, Vetsuisse Fakultät Schwarzenburgstrasse 155 CH-3003 Bern Switzerland flavie.v...@vetsuisse.unibe.chmailto:flavie.v...@vetsuisse.unibe.ch [[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] truncated cone / frustum in R rgl
Dear list! Here is a simplified script for cone in RGL (from rgl demos). I would like to draw a truncated cone / frustum using RGL in R. Do you have any suggestion how to modify code? Thanks a lot, OV library(rgl) cone3d - function(base=c(0,0,0), tip=c(0,0,1), rad=1, n=30) { trans - diag(4) p1 - c(0,1,0) p2 - c(1,0,0) degvec - seq(0, 2*pi,length = n+1)[-1] ecoord2 - function(theta) {base + rad * (cos(theta) * p1 + sin(theta) * p2)} i - rbind(1:n, c(2:n,1), rep(n+1, n)) v - cbind(sapply(degvec, ecoord2), tip) i - rbind(i, rep(n+1 ,n)) v - cbind(v, base) i.x - rbind(c(2:n, 1), 1:n, rep(n+2, n)) i.x - rbind(i.x, rep(n+2, n)) i - cbind(i, i.x) v - rbind(v, rep(1, ncol(v))) return(rotate3d(qmesh3d(v, i), matrix=trans)) } open3d() q1 - cone3d(tip = c(0,0,2)) shade3d(q1, col = green); axes3d(c('x','y','z')); box3d() __ 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] Error Help only 0's may be mixed with negative subscripts
Help Please. Iam drawing a plot i R_Studio but consistently get this error: Error in colorlut2[((zlim[2] - z_invDist)/zlen) * 7] : only 0's may be mixed with negative subscripts-- *Whenever you are confronted with an opponent. Conquer him with love. Mahatma Gadhi* [[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] Error Help only 0's may be mixed with negative subscripts
Dear Agapitus, we do not know what your variables look like but I assume ((zlim[2] - z_invDist)/zlen) * 7 produces a vector with both positive and negative numbert and you are using these numbers to extract elements of an other vector. If the numbers were all positive or negative, it would work, in this case R does not understand what you want. Try x - 1:5 x[1] x[-1] x[-1:1] #gives the same error Before posting again you should read an Intro to R at http://cran.r-project.org/doc/manuals/R-intro.pdf best wishes daniel ps: Gandhi instead of Gadhi? Feladó: r-help-boun...@r-project.org [r-help-boun...@r-project.org] ; meghatalmaz#243;: Agapitus Kato [agapitusk...@gmail.com] Küldve: 2014. január 31. 10:01 To: r-help@r-project.org Tárgy: [R] Error Help only 0's may be mixed with negative subscripts Help Please. Iam drawing a plot i R_Studio but consistently get this error: Error in colorlut2[((zlim[2] - z_invDist)/zlen) * 7] : only 0's may be mixed with negative subscripts-- *Whenever you are confronted with an opponent. Conquer him with love. Mahatma Gadhi* [[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.
Re: [R] plotting points over a map using R
library(RgoogleMaps); # download a static map from the Google server library(geomapdata);# Topographic and Geologic Mapping lbrary(PBSmapping); # Draw Maps and Implement Other GIS Procedures STEP1: my_map = GetMap(center=my_center, zoom =my_zoom , destfile=my_out_file ,maptype = mapmaker-hybrid, size=c(640,640)); ?GetMap for more info STEP2: You can add text = tmp = TextOnStaticMap(my_map,lat = my_markers_1[,lat], lon = my_markers_1[,lon], labels=as.character(site_names), col=red, cex=1.1, add=F) # STEP 3: you can add points tmp = PlotOnStaticMap(my_map,lat = my_markers[,lat], lon = my_markers[,lon], cex=2.,pch=10,col=c(black), add=T) add=T is importent # STEP 4: You can add lines tmp = PlotOnStaticMap(my_map,lat = my_markers_2[,lat], lon = my_markers_2[,lon], col=c(blueviolet), add=T, FUN = lines, lwd = 3); I hope will help ! Mohammed On Fri, 2014-01-31 at 09:58 +, 李德磊 wrote: Hello everyone, I have no idea how to plotting points over a map using R, in which the colors of the points stand for the points' value. I'd like to get a map as shown in attachment picture which was plotted using matlab. Does anyone know how to do it using R? Thank you very much for your help in advance. Kind regards,Dli __ 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] use of effects coding in instrumental variables estimation
Dear all, I am estimating a model with instrumental variables using ivreg() (from the AER library). When the regressors and the instruments are limited to continuous variables, things work fine. However, I have some regressors, such as income, for which I only have categorical data (very low income, low income etc). I have used the following procedure for effects coding (where one_car_dist_car_fam is the dataframe with the sample observations and totink_ovg is the categorical variable): one_car_dist_car_fam$totink_ovg - as.factor(one_car_dist_car_fam$totink_ovg) contrasts(one_car_dist_car_fam$totink_ovg) - contr.sum(7) See http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm for a refernece. Ordinary least squares works fine with this approach (where voorby is the dependent variable en totpr11 is a continuous regressor): common_from - formula(voorby ~ totpr11+ totink_ovg) dist_ols - lm(common_from, data = one_car_dist_car_fam) However, when I apply this to ivreg(), I get the following result (I have now added an endogenous variable verbrgem and the instruments gewleeg + cyl + kw): common_from - formula(voorby ~ totpr11 + totink_ovg + verbrgem ) form_for_iv - update.formula(common_from, ~ . | totpr11+ totink_ovg+ gewleeg + cyl + kw ) dist_iv -ivreg( form_for_iv, data = one_car_dist_car_fam) Error in lm.fit(xz, y, offset = offset, ...) : 0 (non-NA) cases In addition: Warning messages: 1: In Ops.factor(totpr11, totink_ovg) : + not meaningful for factors 2: In Ops.factor(totpr11, totink_ovg) : + not meaningful for factors Does anyone have an idea why this occurs with ivreg() and not with lm()? Of course, I still have the backup of doing the effects coding manually, but I would first like to know whether there is an elegant way out of this specific problem. Laurent Franckx, PhD Senior researcher sustainable mobility VITO NV | Boeretang 200 | 2400 Mol Tel. ++ 32 14 33 58 22| mob. +32 479 25 59 07 | Skype: laurent.franckx | laurent.fran...@vito.be | Twitter @LaurentFranckx VITO Disclaimer: http://www.vito.be/e-maildisclaimer __ 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] package mgcv - predict with bam: Error in X[ind, ] : subscript out of bounds
Hi Katharina, Could you try upgrading to mgcv_1.7-28, please? There was an occasional problem to do with matching factor levels, which is fixed, but I'm not very confident that is what is going on. If upgrading doesn't work, is there any chance you could send me a small example dataset and code that produces the error, and I'll look at it? best, Simon On 30/01/14 12:20, Katharina May wrote: Dear R-Community, I`m trying to apply the mgcv package to fill gaps in sensor data from different sites (9 sites, 2 sensors per site) and do the filling on a site-wise level. Based on http://r.789695.n4.nabble.com/mgcv-gamm-predict-to-reflect-random-s-effects-td3622738.html my model looks like this: xylemRohWeekXnn.fit.bam - bam(sensor1 ~ sensor2 + s(site, bs=re) + s(site, NthSampling, bs=re) , data=xylemRohWeekXnn2011, na.action=na.omit) However, than I try to use predict, I get an error: gapData - xylemRohWeekXnn2011[is.na(xylemRohWeekXnn2011[,2]) !is.na(xylemRohWeekXnn2011[,11]),c(2:3,6:7, 11)] xylemRohWeekXnnSite.fit - predict.gam(xylemRohWeekXnn.fit.bam,gapData, type=response, se=F) Error in X[ind, ] : subscript out of bounds I was hoping that someone might be able to provide a quick hint on if there is an obvious problem or mistake within my model declaration/approach? I attached the sessionInfo() Output below and the xylemRohWeekXnn2011 dump can be downloaded here: https://webdisk.ads.mwn.de/Handlers/AnonymousDownload.ashx?folder=1a7cbaa4path=xylemRohWeekXnn2011.txt I`m appreciating any help and hints! Thank you very much, Katharina - sessionInfo() - R version 3.0.2 (2013-09-25) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 [4] LC_NUMERIC=CLC_TIME=German_Germany.1252 attached base packages: [1] splines stats graphics grDevices utils datasets methods base other attached packages: [1] mgcv_1.7-27 plyr_1.8ggplot2_0.9.3.1 lattice_0.20-24 gdata_2.13.2nlme_3.1-113 [7] zoo_1.7-10 xlsx_0.5.5 xlsxjars_0.5.0 rJava_0.9-6 loaded via a namespace (and not attached): [1] colorspace_1.2-4 dichromat_2.0-0digest_0.6.4 grid_3.0.2 gtable_0.1.2 [6] gtools_3.2.1 labeling_0.2 MASS_7.3-29 Matrix_1.1-2 munsell_0.4.2 [11] proto_0.3-10 RColorBrewer_1.0-5 reshape2_1.2.2 scales_0.2.3 stringr_0.6.2 [16] tools_3.0.2 __ 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. -- Simon Wood, Mathematical Science, University of Bath BA2 7AY UK +44 (0)1225 386603 http://people.bath.ac.uk/sw283 __ 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] Fit Negbin glm model with autoregressive correlation structure
You may also introduce ARMA errors in negative binomial regression via Gaussian copulas. The following webpage illustrates how to use the package gcmr (Gaussian Copula Marginal Regression) for fitting negative binomial regression model with ARMA(2,1) errors: http://cristianovarin.weebly.com/gcmr.html With best regards, Cristiano Varin - Cristiano Varin cristiano.va...@unive.it Department of Environmental Sciences, Informatics and Statistics Ca' Foscari University of Venice http://cristianovarin.weebly.com Il 31/01/2014 09:41, flavie.v...@vetsuisse.unibe.ch ha scritto: Hello, I am attempting to estimate the effect of various variables on the time-series of counts of reported cattle stillbirths. We investigate the effect of day-of-week, month, holidays etc...and also the effect of non-temporal variables. We performed model comparisons between Gaussian glm, Poisson glm and negbin glm and the latter seems most appropriate for our data. We found that the residuals from our best model are not i.i.d. but follow an autoregressive process of order 5 , AR (5). I therefore wish to re-run this model after adding an AR(5) correlation structure in order to get unbiased estimates and standard errors for the variables retained in the model. In the past, I have been faced with a similar situation for a Gaussian glm and used the gls function with a corStruct object describing the within-group correlation structure. However, this would not work with our negbin model. Looking around on various help forums, I came across the possibilities of using generalized estimating equations instead. The gee function (in gee package) has a corstr object which would allow me to specify an AR process of whichever order but there is no option to include a negbin family. The geeglm function (in geepack package) does recognized the negbin family but only gives an option to fit an AR(1) correlation structure. The corstr object seems to have a userdefined option but it is unclear how it could be defined for an AR(5) process. In short, my questions are: *is it possible to include an AR (p) correlation structure directly into a negbin glm? How? * if GEE are the way forward, how can the the corstr object in geeglm be defined for an AR(p) process? Thank you for your suggestions, Dr Flavie Vial Veterinary Public Health Institute DCR-VPH, Vetsuisse Fakultät Schwarzenburgstrasse 155 CH-3003 Bern Switzerland flavie.v...@vetsuisse.unibe.chmailto:flavie.v...@vetsuisse.unibe.ch [[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.
Re: [R] plotting points over a map using R
The ggmap package (http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf) is also quite good. Best, Ista On Fri, Jan 31, 2014 at 6:23 AM, Mohammed Ouassou mohammed.ouas...@statkart.no wrote: library(RgoogleMaps); # download a static map from the Google server library(geomapdata);# Topographic and Geologic Mapping lbrary(PBSmapping); # Draw Maps and Implement Other GIS Procedures STEP1: my_map = GetMap(center=my_center, zoom =my_zoom , destfile=my_out_file ,maptype = mapmaker-hybrid, size=c(640,640)); ?GetMap for more info STEP2: You can add text = tmp = TextOnStaticMap(my_map,lat = my_markers_1[,lat], lon = my_markers_1[,lon], labels=as.character(site_names), col=red, cex=1.1, add=F) # STEP 3: you can add points tmp = PlotOnStaticMap(my_map,lat = my_markers[,lat], lon = my_markers[,lon], cex=2.,pch=10,col=c(black), add=T) add=T is importent # STEP 4: You can add lines tmp = PlotOnStaticMap(my_map,lat = my_markers_2[,lat], lon = my_markers_2[,lon], col=c(blueviolet), add=T, FUN = lines, lwd = 3); I hope will help ! Mohammed On Fri, 2014-01-31 at 09:58 +, 李德磊 wrote: Hello everyone, I have no idea how to plotting points over a map using R, in which the colors of the points stand for the points' value. I'd like to get a map as shown in attachment picture which was plotted using matlab. Does anyone know how to do it using R? Thank you very much for your help in advance. Kind regards,Dli __ 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] Controlling visibility of top-level functions
I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? -- Paul __ 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] Fw: 2 phase coxian phase distribution
On Saturday, January 18, 2014 9:20 PM, nelsonsibbs78 nelsonsibb...@yahoo.com wrote: Can you pliz send me the R code for estimating parameters of a 2 phase distribution given some data to fit. Im nelson sibanda , a final year student at Nust [[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] Defining function help
Hello everyone. I am quite new to R and I would appreciate some help in figuring out what I am doing wrong. For the following code: #with cost m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) t.control=function(m, a){(1-exp(-a*m)*(1-(1/30)*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m)*(1-(1/30)*m))} t.sham(2, 2, 0.1) [1] 0.9744979 t.control(2, 2) [1] 0.9829054 #without cost m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) t.control=function(m, a){(1-exp(-a*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))} t.sham(2, 2, 0.1) [1] 0.9726763 t.control(2, 2) [1] 0.9816844 As you can see the values I am getting for t.control and t.sham are larger in the with cost scenario and this shouldn't be the case. (the cost is 1-1/30*m). Is there something wrong I did when defining t.sham and t.control in the with cost section that I am failing to see? [[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] Generalized Ordered Logit in R
Dear community, I am replicating a paper (The Effect of Structures and Power on State Bargaining Strategies, http://tinyurl.com/oyk289c) for a class project. The author used “gologit” in STATA, and I need the equivalent function in R. She used a “Generalized Ordered Logit” (see below model description). Can you run a “generalized ordered logit” with glm? I could not find the specification in the help file for the function. Or is there another R package for it? The data can be accessed here: http://tinyurl.com/oydwl4d (McKibben_AJPS Bargaining Strategies data.tab) The results should look like this: http://s17.postimg.org/3zldxs133/Brooke.png Thank-you so much for your help. It is greatly appreciated. Kind Regards, Brooke Model Description from the original paper: To test the effects of the bargaining structure against more standard explanations of variation in state bargaining behavior, I estimate two generalized ordered logit models with clustered standard errors. Model 1 includes lnGDP, focusing on states capabilities as the central measure of their power, and Model 2 includes the alternative, Voting power index. While standard ordered logit models might appear to be the appropriate statistical technique, the data violate the assumption of proportional odds that underpins these models. In other words, all independent variables do not exert the same effect across all categories of the dependent variable. The generalized ordered logit model relaxes this assumption, allowing the effects of the independent variables to vary across the different cate- gories of cooperative bargaining behavior. Standard er- rors were also clustered by negotiation phase to deal with the fact that each state’s choice of bargaining behavior within a given bargaining interaction is dependent on the choices of other states. (These models were estimated using the gologit2 command in Stata) __ 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] Problem relocating the R installation tree
After moving the R installation tree, I modified the R script with a new RHOME value, and it worked. There seems a hard coded RHOME in Rscript, which is a binary file. Is there a way to regenerate the Rscript file for the new location, without re-installing the whole thing? Jun [[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] Problem relocating the R installation tree
After moving the R installation tree, I modified the R script with a new RHOME value, and it worked. There seems a hard coded RHOME in Rscript, which is a binary file. Is there a way to regenerate the Rscript file for the new location, without re-installing the whole thing? Jun [[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] Defining function help
On 31/01/2014 12:29 PM, Mathew Nagendran wrote: Hello everyone. I am quite new to R and I would appreciate some help in figuring out what I am doing wrong. For the following code: #with cost m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) t.control=function(m, a){(1-exp(-a*m)*(1-(1/30)*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m)*(1-(1/30)*m))} t.sham(2, 2, 0.1) [1] 0.9744979 t.control(2, 2) [1] 0.9829054 #without cost m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) t.control=function(m, a){(1-exp(-a*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))} t.sham(2, 2, 0.1) [1] 0.9726763 t.control(2, 2) [1] 0.9816844 As you can see the values I am getting for t.control and t.sham are larger in the with cost scenario and this shouldn't be the case. (the cost is 1-1/30*m). Is there something wrong I did when defining t.sham and t.control in the with cost section that I am failing to see? You aren't reading your parentheses correctly. In the first case you're multipling only the exp() part by the cost, so you are subtracting a smaller number from 1, hence you get a larger result. Duncan Murdoch __ 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] Controlling visibility of top-level functions
On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Duncan Murodch __ 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] Defining function help
Hi, Not sure if this is what you wanted: t.control=function(m, a){(1-exp(-a*m)*((1-(1/30))*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m)*((1-(1/30))*m))} t.control(2,2) #[1] 0.9645898 t.sham(2,2,0.1) #[1] 0.9471741 A.K. On Friday, January 31, 2014 12:29 PM, Mathew Nagendran mathew.nagend...@mail.utoronto.ca wrote: Hello everyone. I am quite new to R and I would appreciate some help in figuring out what I am doing wrong. For the following code: #with cost m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) t.control=function(m, a){(1-exp(-a*m)*(1-(1/30)*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m)*(1-(1/30)*m))} t.sham(2, 2, 0.1) [1] 0.9744979 t.control(2, 2) [1] 0.9829054 #without cost m.control=c(1.45,9.40,9.96,4.2,1.86,0.2) m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09) t.control=function(m, a){(1-exp(-a*m))} t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))} t.sham(2, 2, 0.1) [1] 0.9726763 t.control(2, 2) [1] 0.9816844 As you can see the values I am getting for t.control and t.sham are larger in the with cost scenario and this shouldn't be the case. (the cost is 1-1/30*m). Is there something wrong I did when defining t.sham and t.control in the with cost section that I am failing to see? [[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.
Re: [R] plotting points over a map using R
On 01/31/2014 08:58 PM, 李德磊 wrote: Hello everyone, I have no idea how to plotting points over a map using R, in which the colors of the points stand for the points' value. I'd like to get a map as shown in attachment picture which was plotted using matlab. Does anyone know how to do it using R? Thank you very much for your help in advance. Kind regards,Dli Hi Dli, Your question involves two things, the placement of the points and their color. The coordinate system of maps is almost always geographic, in decimal degrees. The x values are the longitudes measured from the prime meridian that passes through Greenwich, England. The y values are the latitudes that are measured from the equator. To get the colors for the points, if you have some value like average temperature for the points, you can convert the numbers into colors using functions like color.scale in the plotrix package. The resulting vector or matrix of colors can then be passed to the points function. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting points over a map using R
Try reading the R Spatial Task View: http://cran.r-project.org/web/views/Spatial.html or this brilliant intro, and other files in that github repository: https://github.com/Robinlovelace/Creating-maps-in-R/blob/master/intro-spatial.md or any of the tutorials I've written on the subject! Barry On Fri, Jan 31, 2014 at 9:58 AM, 李德磊 dele...@hotmail.com wrote: Hello everyone, I have no idea how to plotting points over a map using R, in which the colors of the points stand for the points' value. I'd like to get a map as shown in attachment picture which was plotted using matlab. Does anyone know how to do it using R? Thank you very much for your help in advance. Kind regards,Dli __ 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] Automatically naming subsets in a for loop
Hello, I'm trying to subet my data based on long/lat. I have written a for loop however I am not sure how to get R to name all my subsets different things. Here is my code so far... for(i in 0:31){ latl=38+(i*1) latu=39+(i*1) for(j in 0:33){ longl=(-72)+(j*1) longu=(-71)+(j*1) G$i$j-subset(G, Latlatl Latlatu Longlongl Longlongu) } } However this just freezes my computer. Any help is appreciated! -- View this message in context: http://r.789695.n4.nabble.com/Automatically-naming-subsets-in-a-for-loop-tp4684518.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.
Re: [R] Automatically naming subsets in a for loop
The standard way to do this is to use the cut function to define group categories, and then manipulate data in lists of subsets using lapply function and relatives. There are also tools such as the plyr package, data.table package, sqldf package, and the new dplyr package. If you want more explicit help, then you really need to read the posting guide (e.g. no HTML email), and make a reproducible example (e.g. include some test data and an example calculation, even if you have to do it the long way with no loops). http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On January 31, 2014 12:11:04 PM PST, denys de...@uoguelph.ca wrote: Hello, I'm trying to subet my data based on long/lat. I have written a for loop however I am not sure how to get R to name all my subsets different things. Here is my code so far... for(i in 0:31){ latl=38+(i*1) latu=39+(i*1) for(j in 0:33){ longl=(-72)+(j*1) longu=(-71)+(j*1) G$i$j-subset(G, Latlatl Latlatu Longlongl Longlongu) } } However this just freezes my computer. Any help is appreciated! -- View this message in context: http://r.789695.n4.nabble.com/Automatically-naming-subsets-in-a-for-loop-tp4684518.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-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] Automatically naming subsets in a for loop
Here is one way by just truncating your data to integers based on what you are doing: # create some test data n - 1000 G - data.frame(Lat = runif(n, 38, 50), Long = runif(n, -72, -65)) # add a 'key' for segment - based on truncating data G$key - paste(as.integer(G$Lat), as.integer(G$Long), sep = :) # number of unique keys length(unique(G$key)) [1] 84 # split into segments segs - split(G, G$key) head(G, 20) Lat Longkey 1 41.59301 -68.76338 41:-68 2 46.49720 -66.33649 46:-66 3 45.53748 -68.03110 45:-68 4 39.66539 -68.20521 39:-68 5 39.82959 -71.26644 39:-71 6 41.44688 -70.43106 41:-70 7 43.71112 -66.73736 43:-66 8 44.88753 -69.75961 44:-69 9 44.50466 -71.77183 44:-71 10 46.40642 -65.00966 46:-65 11 47.80190 -65.48757 47:-65 12 40.46522 -68.00612 40:-68 13 40.98004 -71.48152 40:-71 14 49.57253 -66.71064 49:-66 15 49.22970 -69.04267 49:-69 16 48.63010 -68.33392 48:-68 17 43.20188 -67.70121 43:-67 18 46.42424 -68.34635 46:-68 19 49.46040 -70.26964 49:-70 20 40.10401 -65.23893 40:-65 Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Fri, Jan 31, 2014 at 3:11 PM, denys de...@uoguelph.ca wrote: Hello, I'm trying to subet my data based on long/lat. I have written a for loop however I am not sure how to get R to name all my subsets different things. Here is my code so far... for(i in 0:31){ latl=38+(i*1) latu=39+(i*1) for(j in 0:33){ longl=(-72)+(j*1) longu=(-71)+(j*1) G$i$j-subset(G, Latlatl Latlatu Longlongl Longlongu) } } However this just freezes my computer. Any help is appreciated! -- View this message in context: http://r.789695.n4.nabble.com/Automatically-naming-subsets-in-a-for-loop-tp4684518.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-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] sapply returning list instead of matrix
Can anyone suggest a rationale for why sapply() returns different types (list and matrix) in the two examples below? Is there any way to get sapply() or any other apply() function to return a matrix in both cases? simplify=TRUE doesn't change the outcome. I understand why it is happening, I just can't understand why such unpredictable behavior makes sense. [[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] sapply returning list instead of matrix
As you ignored the posting guide and posted in HTML, your below didn't get through. So one can only guess that it has something to do with (see ?sapply) Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the same (positive) length. If the common length is one the result is a vector, and if greater than one is a matrix with a column corresponding to each element of X. Return values most also be of the same type, also, obviously. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. H. Gilbert Welch On Fri, Jan 31, 2014 at 1:36 PM, chris warth cswa...@gmail.com wrote: Can anyone suggest a rationale for why sapply() returns different types (list and matrix) in the two examples below? Is there any way to get sapply() or any other apply() function to return a matrix in both cases? simplify=TRUE doesn't change the outcome. I understand why it is happening, I just can't understand why such unpredictable behavior makes sense. [[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.
Re: [R] Controlling visibility of top-level functions
On 01/02/14 08:13, Duncan Murdoch wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Would it not be more perspicuous to do: fun2 - local({ fun1 - function(...) { ... } function(...) { ... fun1 ...} }) Having fun3 kicking around seems to be an effect brought to you by the Department of Redundancy Department. cheers, Rolf __ 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] sapply returning list instead of matrix
Hey thanks for the helpful snark, Bert. To everyone else, I apologize for neglecting to actually include the examples. a - function(i) { list(1) } b - function(i) { list(1,2) } ll - sapply(seq(3), a, simplfy=list) mm - sapply(seq(3), b) class(ll) class(mm) class(ll) [1] list class(mm) [1] matrix I can read the documentation, I see why it happens, but who in their right mind would design a function this way? Can you imagine how many bugs are lurking because people haven't yet hit the right set of input that is going to cause sapply() to return a list instead of a matrix(). The point is that having the type of return value depend on the length of output from the applied function is simply madness. It is a terrible design decision. What is to be gained from the fact that I have to test the type of value returned from sapply()? I was hoping plyr::laply() would be better but it perpetuates the same bad interface. [so sorry for sending html, if that is what's happening. I guess gmail send html by default? ] On Fri, Jan 31, 2014 at 1:44 PM, Bert Gunter gunter.ber...@gene.com wrote: As you ignored the posting guide and posted in HTML, your below didn't get through. So one can only guess that it has something to do with (see ?sapply) Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the same (positive) length. If the common length is one the result is a vector, and if greater than one is a matrix with a column corresponding to each element of X. Return values most also be of the same type, also, obviously. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. H. Gilbert Welch On Fri, Jan 31, 2014 at 1:36 PM, chris warth cswa...@gmail.com wrote: Can anyone suggest a rationale for why sapply() returns different types (list and matrix) in the two examples below? Is there any way to get sapply() or any other apply() function to return a matrix in both cases? simplify=TRUE doesn't change the outcome. I understand why it is happening, I just can't understand why such unpredictable behavior makes sense. [[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.
Re: [R] Controlling visibility of top-level functions
Thanks for the suggestions. I'm going to need to create an R package, eventually, so I'll use that mechanism to control visibility. -- Paul On Fri, Jan 31, 2014 at 11:13 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Duncan Murodch __ 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] sapply returning list instead of matrix
I can read the documentation, I see why it happens, but who in their right mind would design a function this way? Can you imagine how many bugs are lurking because people haven't yet hit the right set of input that is going to cause sapply() to return a list instead of a matrix(). If you always want a list output use lapply(). If you want the simplification that sapply does, but with sanity checks, use vapply(). vapply() lets you assert the type and size of FUN's return value. If all goes well it returns what sapply() would return but it throws an error if any call to FUN returns something unexpected. (Also, if length(X) is 0, vapply makes the output be a zero-length object of the appropriate type.) vapply(1:3, FUN=seq_along, FUN.VALUE=1L) [1] 1 1 1 vapply(1:3, FUN=range, FUN.VALUE=c(0,0)) [,1] [,2] [,3] [1,]123 [2,]123 vapply(1:3, FUN=seq, FUN.VALUE=1L) Error in vapply(1:3, FUN = seq, FUN.VALUE = 1L) : values must be length 1, but FUN(X[[2]]) result is length 2 vapply(numeric(0), FUN=range, FUN.VALUE=c(0,0)) # returns 2 by 0 numeric matrix [1,] [2,] Bill Dunlap TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of chris warth Sent: Friday, January 31, 2014 2:22 PM To: r-help@r-project.org Subject: Re: [R] sapply returning list instead of matrix Hey thanks for the helpful snark, Bert. To everyone else, I apologize for neglecting to actually include the examples. a - function(i) { list(1) } b - function(i) { list(1,2) } ll - sapply(seq(3), a, simplfy=list) mm - sapply(seq(3), b) class(ll) class(mm) class(ll) [1] list class(mm) [1] matrix I can read the documentation, I see why it happens, but who in their right mind would design a function this way? Can you imagine how many bugs are lurking because people haven't yet hit the right set of input that is going to cause sapply() to return a list instead of a matrix(). The point is that having the type of return value depend on the length of output from the applied function is simply madness. It is a terrible design decision. What is to be gained from the fact that I have to test the type of value returned from sapply()? I was hoping plyr::laply() would be better but it perpetuates the same bad interface. [so sorry for sending html, if that is what's happening. I guess gmail send html by default? ] On Fri, Jan 31, 2014 at 1:44 PM, Bert Gunter gunter.ber...@gene.com wrote: As you ignored the posting guide and posted in HTML, your below didn't get through. So one can only guess that it has something to do with (see ?sapply) Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the same (positive) length. If the common length is one the result is a vector, and if greater than one is a matrix with a column corresponding to each element of X. Return values most also be of the same type, also, obviously. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. H. Gilbert Welch On Fri, Jan 31, 2014 at 1:36 PM, chris warth cswa...@gmail.com wrote: Can anyone suggest a rationale for why sapply() returns different types (list and matrix) in the two examples below? Is there any way to get sapply() or any other apply() function to return a matrix in both cases? simplify=TRUE doesn't change the outcome. I understand why it is happening, I just can't understand why such unpredictable behavior makes sense. [[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-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] sapply returning list instead of matrix
Pot, meet kettle. You claim to be able to read documentation, yet you don't reference knowledge gained or clarity lost from such activity in your question. I think this is a case of inertia of history that we all have to live with at this point. If you thoroughly read the documentation for ?sapply you will encounter the vapply function, which will provide the reliability you want at the cost of some additional syntactic complexity. Or not. I rarely use apply functions for arrays... if I can't vectorize my calculation, I preallocate my result array and use a for loop to fill it up. I don't have this problem with ddply. BTW: Gmail is capable of sending plain text... but you might have to read some documentation to find out how. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On January 31, 2014 2:22:00 PM PST, chris warth cswa...@gmail.com wrote: Hey thanks for the helpful snark, Bert. To everyone else, I apologize for neglecting to actually include the examples. a - function(i) { list(1) } b - function(i) { list(1,2) } ll - sapply(seq(3), a, simplfy=list) mm - sapply(seq(3), b) class(ll) class(mm) class(ll) [1] list class(mm) [1] matrix I can read the documentation, I see why it happens, but who in their right mind would design a function this way? Can you imagine how many bugs are lurking because people haven't yet hit the right set of input that is going to cause sapply() to return a list instead of a matrix(). The point is that having the type of return value depend on the length of output from the applied function is simply madness. It is a terrible design decision. What is to be gained from the fact that I have to test the type of value returned from sapply()? I was hoping plyr::laply() would be better but it perpetuates the same bad interface. [so sorry for sending html, if that is what's happening. I guess gmail send html by default? ] On Fri, Jan 31, 2014 at 1:44 PM, Bert Gunter gunter.ber...@gene.com wrote: As you ignored the posting guide and posted in HTML, your below didn't get through. So one can only guess that it has something to do with (see ?sapply) Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the same (positive) length. If the common length is one the result is a vector, and if greater than one is a matrix with a column corresponding to each element of X. Return values most also be of the same type, also, obviously. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. H. Gilbert Welch On Fri, Jan 31, 2014 at 1:36 PM, chris warth cswa...@gmail.com wrote: Can anyone suggest a rationale for why sapply() returns different types (list and matrix) in the two examples below? Is there any way to get sapply() or any other apply() function to return a matrix in both cases? simplify=TRUE doesn't change the outcome. I understand why it is happening, I just can't understand why such unpredictable behavior makes sense. [[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-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] Controlling visibility of top-level functions
I think you will find that using namespaces and export directives are a better way of controlling visibility with packages. That is their intended purpose. Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 Data is not information. Information is not knowledge. And knowledge is certainly not wisdom. H. Gilbert Welch On Fri, Jan 31, 2014 at 2:28 PM, Paul A. Steckler st...@stecksoft.com wrote: Thanks for the suggestions. I'm going to need to create an R package, eventually, so I'll use that mechanism to control visibility. -- Paul On Fri, Jan 31, 2014 at 11:13 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Duncan Murodch __ 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.
Re: [R] Controlling font size on code chunk outputs using Knitr
Yihui/Jeff, I'm trying to determine where the default CSS file is located as I don't see this in any of the documentation. I can definitely find a markdwon.css file in C:\Program Files\RStudio\resources I also see an R.css file in that directory. I also have R.css in C:\Users\jeffjohn\Dropbox\R\Rlibs\rstudio\html which is where I have all of my packages installed. Would you know how I can determine what CSS file a given .Rmd file is referencing? However, I've tried making a simple change to each of them (first backing them up of course) by changing the h1 to small instead of x-large and saving the doc, but when I knit the document it does not change anything. Any guidance you can provide would be extremely helpful. Again, I'm using R-Studio on Windows. On Thu, Jan 30, 2014 at 3:54 PM, Jeff Johnson mrjeffto...@gmail.com wrote: Thanks Yihui and Jeff. I've retrieved the default CSS file and made a tweak to it (changing a header 1 size just to test it) and saved it to the same local directory as my .Rmd file using the name 'mymarkdown.css' for testing. I've added: options(rstudio.markdownToHTML = function(inputFile, outputFile) { require(markdown) markdownToHTML(inputFile, outputFile, stylesheet='mymarkdown.css') } ) to the top of my testfile.Rmd file so that my file now looks like: options(rstudio.markdownToHTML = function(inputFile, outputFile) { require(markdown) markdownToHTML(inputFile, outputFile, stylesheet='mymarkdown.css') } ) Title This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **Help** toolbar button for more details on using R Markdown). When you click the **Knit HTML** button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: ```{r} summary(cars) ``` But when I knit it, it just writes the options chunk at the top of my document. Am I supposed to add something else to get the .rmd file to reference the css? I'm quite new to programming and R (as if you couldn't tell!), so not sure what additional steps I need to add. Thanks much. Jeff On Thu, Jan 30, 2014 at 1:48 PM, Yihui Xie x...@yihui.name wrote: Exactly. Please see RStudio documentation: https://support.rstudio.com/hc/en-us/articles/200552186-Customizing-Markdown-Rendering Regards, Yihui -- Yihui Xie xieyi...@gmail.com Web: http://yihui.name On Thu, Jan 30, 2014 at 10:57 AM, Jeff Newmiller jdnew...@dcn.davis.ca.us wrote: This sounds like a classic you need to write a custom CSS file problem... Which is off-topic here, so is homework for you. On January 30, 2014 8:34:32 AM PST, Jeff Johnson mrjeffto...@gmail.com wrote: Hi Yihui, The package I have installed is knitr. To generate the HTML, I run Knit HTML from within R Studio version .98.490 (there's an icon to initiate it. You can load that dataset, then: Print the column names ```{r, echo=showcode, comment=commentchar} colnames(mydf) ``` The resulting font is a couple of points larger than I'd like. I'd like to be able to control this either globally or at the code chunk level. Thanks for your help with this! -- Jeff -- Jeff [[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] Controlling visibility of top-level functions
On 14-01-31 5:09 PM, Rolf Turner wrote: On 01/02/14 08:13, Duncan Murdoch wrote: On 31/01/2014 1:46 PM, Paul A. Steckler wrote: I'm fairly new to R, and have checked the R FAQ and done an RSiteSearch for help on this topic, to no avail. I want to write some R code that has functions at the top-level that are not visible when the code is loaded. So in fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} I'd like fun2 to be callable, but have fun1 be invisible. That is, the scope of fun1 is limited to the file in which it's defined. In Python, I believe that prepending an underscore to a variable name limits its scope in this way. Is there a similar mechanism in R? There are a couple ways. The heavyweight way is to write a package that exports fun2 but not fun1. fun2 can see fun1, but the rest of the world can't. Scope isn't limited to one file, any function in the package can see it. The lightweight way is to define fun1 and fun2 in a local scope, e.g. fun3 - local({ fun1 - function(...) { ... } fun2 - function(...) { ... fun1 ...} fun2 }) This way fun1 and fun2 can see each other but nobody else can see them, and fun3 is a copy of fun2 that is visible in the workspace. You don't need a third name, I just changed it to make the explanation easier. Would it not be more perspicuous to do: fun2 - local({ fun1 - function(...) { ... } function(...) { ... fun1 ...} }) Having fun3 kicking around seems to be an effect brought to you by the Department of Redundancy Department. Sure, that's how I'd write it, but then it's harder to figure out how to say fun1 and fun2 can see each other. (Of course, fun1 and fun2 can see fun3 in my version, but in a different way, and not necessarily in a more complicated example.) Duncan __ 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] Problem connecting to database via RPostgreSQL/RS-DBI: could not connect error
In the description section of the RPostgreSQL package documentation, it states: In order to build and install this package from source, PostgreSQL itself must be present your system to provide PostgreSQL functionality via its libraries and header files. . . . On Microsoft Windows system the attached libpq library source will be used. I am not sure what attached means in this setting. After successfully running: install.packages('RPostgreSQL') When I tried to run: require('RPostgreSQL') drv - dbDriver(PostgreSQL, fetch.default.rec=1) con - dbConnect(drv, dbname=postgres) I get the error message: Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect Administrator@local on dbname postgres) I am guessing that this error is because PostgreSQL either does not exist or has been improperly installed. Since RPostgreSQL is supposed to work with the particular version of PostgreSQL that comes with it in libpq, I assume that either install.packages('RPostgreSQL') does this installation or I have to do it. I have not done it, because I can not find attached libpq library. There is no file or directory called either libpq or PostgreSQL in my \Program Files\ directory, my \R-3.0.2 directory, my \R-3.0.2 \library directory, or my R-3.0.2\library\RPostgreSQL directory, nor is such a file or folder in my working directory. There is no library called libpq on CRAN. There is no discussion that I could find of how to install it, where it comes from, or where installation puts it in either the package documentation or on the Google site for the package, http://code.google.com/p/rpostgresql/ At that site under the Source tab I did find some files at RPostgreSQL/src/libpq, but no information on what to do with them. I tried: require('libpq') and got: Warning message: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no package called ‘libpg’ I though maybe I was misunderstanding what or where libpq is, and that I should be going to the PostgreSQL site and folowing their installation instructions without reference to R. But I have carefully gone over the documentation for dbDriver and dbConnect several times, and neither one gives me any way (except a name or an IP address for a remote server) to tell R where the PostgreSQL directory, program, or file are located. That seems to imply that the local database RPostgreSQL connects to must be in a location where R puts it. So I am missing something, but I do not know what. I am running R 3.0.2 through RStudio on a Windows XP 32-bit machine. Any help anyone could offer would be greatly Warmest regards, andrewH -- View this message in context: http://r.789695.n4.nabble.com/Problem-connecting-to-database-via-RPostgreSQL-RS-DBI-could-not-connect-error-tp4684534.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.
Re: [R] Automatically naming subsets in a for loop
Thank you very much ! -- View this message in context: http://r.789695.n4.nabble.com/Automatically-naming-subsets-in-a-for-loop-tp4684518p4684524.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.