Re: [R] Bug in predict.lm?
On 16 Nov 2013, at 03:06 , Charles Berry ccbe...@ucsd.edu wrote: Rolf Turner r.turner at auckland.ac.nz writes: I *do* see the same phenomenon that Bert describes and the code of predict.lm() *does* appear to contain a bug. There is a line: [snip] The operative difference between my set-up and Chuck's is that I am using version 3.0.2 Patched. So I am very puzzled as to why Chuck does *not* get an error thrown! [rest deleted] The answer is that I made a rookie error. I ran example(predict.lm) before trying Bert's ECM. And then I did not bother to see what example(predict.lm) left lying around in R_GlobalEnv ... As you can probably guess, there was an object called 'w'. So predict.lm finds it and is.null(w) is FALSE. Mea culpa! Hmm, maybe, but this is the sort of thing that code analysis tries to catch (as in no visible binding for global variable 'w'). Apparently, the code checker is not smart enough to detect cases where a local variable is _sometimes_ not defined. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@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] The smallest enclosing ball problem
I wanted to solve the following geometric optimization problem, sometimes called the enclosing ball problem: Given a set P = {p_1, ..., p_n} of n points in R^d, find a point p_0 such that max ||p_i - p_0|| is minimized. A known algorithm to solve this as a Qudratic Programming task is as follows: Define matrix C = (p_1, ..., p_n), i.e. coordinates of points in columns, and minimize x' C' C x - \sum (p_i' p_i) x_i subject to \sum x_1 = 1, x_i = 0 . Let x0 = (x0_1, ..., x0_n) be an optimal solution, then the linear combination p0 = \sum x0_i p_i is the center of the smallest enclosing ball, and the negative value of the objective function at x0 is the squared radius of the ball. As an example, find the smallest ball enclosing the points (1,0,0), (0,1,0), and (0.5, 0.5, 0.1) in 3-dim. space. We would expect the center of the ball to be at (0.5, 0.5, 0), and with radius 1/sqrt(2). C - matrix( c(1, 0, 0.5, 0, 1, 0.5, 0, 0, 0.1), ncol = 3, byrow = TRUE) # We need to define D = 2C' C, d = (p_i' p_i), A = c(1,1,1), and b = 1 D - 2 * t(C) %*% C d - apply(C^2, 2, sum) A - matrix(1, nrow = 1, ncol = 3) b - 1 # Now solve with solve.QP() in package quadprog ... require(quadprog) sol1 - solve.QP(D, d, t(A), b, meq = 1) # ... and check the result sum(sol1$solution) # 1 p0 - c(C %*% sol1$solution)# 0.50 0.50 -2.45 r0 - sqrt(-sol1$value) # 2.55 # distance of all points to the center sqrt(colSums((C - p0)^2)) # 2.55 2.55 2.55 As a result we get a ball such that all points lie on the boundary. The same happens for 100 points in 100-dim. space (to keep D positive definite, n = d is required). That is a very nice, even astounding result, but not what I had hoped for! Compare this with another quadratic programming solver in R, for example LowRankQP() in the package of the same name. require(LowRankQP) sol2 - LowRankQP(D, -d, A, b, u = rep(3, 3), method=LU) p2 - c(C %*% sol2$alpha) # 5.00e-01 5.00e-01 1.032019e-12 sqrt(colSums((C - p2)^2)) # 0.7071068 0.7071068 0.100 The correct result, and we get correct solutions in higher dimensions, too. LowRankQP works also if we apply it with n d, e.g., 100 points in 2- or 3-dimensional space, when matrix D is not positive definite -- solve.QP( ) does not work in these cases. *** What do I do wrong in calling solve.QP()? *** My apologies for this overly long request. I am sending it through the weekend hoping that someone may have a bit of time to look at it more closely. Hans Werner __ 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] Help
Sorry to do this to you, but questions about mixed models(especially lmer) are better handled on the mixed models list, r-sig-mixed-models . So subscribe and post this there. Cheers, Bert On Fri, Nov 15, 2013 at 3:30 PM, Dalal Hanna dalal.e.ha...@gmail.com wrote: Hi, I tried to post the below message and it was rejected because I hadn't yet received my subscription to the group. Can it be posted now? Cheers, Thanks very much for all your dedication to this project! Dalal MESSAGE I ATTEMPTED TO POST BELOW I am trying to code the following idea using lmer (package lme4) and running into problems. I am also unsure that I am coding this properly and wanted to double check, as there are not enough people in my entourage that can do this for me. For individual fish i of species j in lake k, Hgijk ~ b0j[i] + b1k[i] + b2j[i]*lengthi + ei ei ~ N(0,s2e) b0j ~ N(mb0,s2b0) b1k ~ go + g1*chlorophyll-ak + eb1 eb1 ~ N(0,s2b1) b2j ~ N(mb2,s2b2) This model says that the Hg for fish i of species j from lake k is a function of the species, the lake, and the length of the fish. The species effects are like intercepts – the average Hg for a particular species at average length (if the length data are centered) in an average lake – and they come from a normal distribution. The lake effect is itself a linear function of chlorophyll concentration, so that lakes with higher chlorophyll can potentially have higher or lower average Hg concentrations. The length effects vary by species, on the assumption that Hg concentration may change with size differently in different species (which is the case in the systems I am working in). These length effects come from a normal distribution. *Problems:* 1. My original idea was to code this as such: M.A4-lmer(LogTHg~ Chlorophyll + Length+ (1 + Length|SpeciesCode) +(1|Location), data=mercury.subset) -Does this function really represent the ideas I explained above? I am not sure if Length is supposed to be included in 2 places as it is here -When I run this model, the se.ceof() of Length always comes out to 0, which doesn't really make sense. Any ideas why this might be happening? Thank you very much for taking the time to read this and providing input. I appreciate it. [[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. -- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 __ 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] R 2.14.2 - Installation problem - could not load lattice
Hello, I'm new to R, and have a problem related to spss integration with R (but is actually related to the R installation): I have windows 8, 64bit, and installed R 2.14.2, and then the R plug in for spss (spss version 21). When I tried to run somthing in spss, I got the following error messgae: Error in loadNamespace(i, c(lib.loc, .libPaths())) : there is no package called 'lattice' Installing package(s) into 'C:/Program Files/IBM/SPSS/Statistics/21/extensions' (as 'lib' is unspecified) I unserstood that lattice should come with the default installation, and that this messgae indicates that something went wrong with the installation, but after uninstalling and re-installing I got the same thing... I have some suspicion that this might be related to 32/64 bit. When I downloaded R I took the default installation suggestion and this downloaded both 32 and 64. Could it be the case? Any help would be very much appreciated Yael [[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] Apply function to one specific column / Alternative to for loop
Hi guys, I am a total newbie to R, so I hope this isn't a totally dumb question. I have a dataframe with a title in one row and the corresponding values in the next rows. Let's take this example: test_df - data.frame(cbind(titel = , x = 4:5, y = 1:2)) test_df = rbind(cbind(titel=1.Test, x=, y=), test_df, cbind(titel=2.Test, x=, y=), test_df, cbind(titel=3.Test, x=, y=), test_df) test_df titel x y 1 1.Test 24 1 35 2 4 2.Test 54 1 65 2 7 3.Test 84 1 95 2 What I want to have is: titel x y 2 1.Test 4 1 3 1.Test 5 2 5 2.Test 4 1 6 2.Test 5 2 8 3.Test 4 1 9 3.Test 5 2 In my example, the title is in every third line, but in my real data there is no pattern. Each title has at least one line but can have x lines. I was able to solve my problem in a for loop with the following code: test_df$titel - as.character(test_df$titel) for (i in 1:nrow(test_df)) { if (nchar(test_df$titel[i])==0){ test_df$titel[i]=test_df$titel[i-1] } } test_df - subset(test_df,test_df$x!=) The problem is, I have a lot of data and the for loop is obviously very slow. Is there a more elegant way to achieve the same? I think I have to use the apply function, but I don't know how to use it with just one column. -- View this message in context: http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566.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] R 2.14.2 - Installation problem - could not load lattice
On 16.11.2013 09:36, Yael Inbar wrote: Hello, I'm new to R, and have a problem related to spss integration with R (but is actually related to the R installation): I have windows 8, 64bit, and installed R 2.14.2, and then the R plug in for spss (spss version 21). When I tried to run somthing in spss, I got the following error messgae: Error in loadNamespace(i, c(lib.loc, .libPaths())) : there is no package called 'lattice' Installing package(s) into 'C:/Program Files/IBM/SPSS/Statistics/21/extensions' (as 'lib' is unspecified) I unserstood that lattice should come with the default installation, and that this messgae indicates that something went wrong with the installation, but after uninstalling and re-installing I got the same thing... I have some suspicion that this might be related to 32/64 bit. When I downloaded R I took the default installation suggestion and this downloaded both 32 and 64. Could it be the case? Probably not, but version R-2.14.2 is ancient and hence unsupported. If SPSS requires it, please contact your SPSS support for help. Best, Uwe Ligges Any help would be very much appreciated Yael [[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] there is no package called 'boot'
On 14.10.2013 21:59, Christian Hoffmann wrote: Hi, R CMD mypackage /Users/hoffmann/R/cwhmisc check --as-cran gives me * checking Rd cross-references ... WARNING Error in find.package(package, lib.loc) : there is no package called 'boot' although there is no textual reference to 'boot' in my /R and /man within /cwhmisc nor any file of that name. How could I possibly find the culprit? I do not know your current version of the package, but probably you forgot to declare it as a dependency level (e.g. Suggests) in the DESCRIPTION file? Best, Uwe Ligges TIA C. sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-apple-darwin10.8.0 (64-bit) locale: [1] C attached base packages: [1] tools tcltk stats4splines parallel datasets compiler [8] graphics grDevices stats grid utils methods base other attached packages: [1] survival_2.37-4spatial_7.3-6 rpart_4.1-1 nnet_7.3-6 [5] nlme_3.1-109 foreign_0.8-53 codetools_0.2-8 cluster_1.14.4 [9] class_7.3-7Matrix_1.0-12 MASS_7.3-26 KernSmooth_2.23-10 [13] cwhmisc_4.1lattice_0.20-15 __ 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] Double Pareto Log Normal Distribution DPLN
Guys Any help in this please. Regards Bander On 14 Nov 2013, at 09:29 pm, David R Forrest d...@vims.edu wrote: Hi Bander, I'm pushing this discussion back to the list, because I'm not sure of the shape/rate parameters for rpareto and rexp and how they'd be applied across this mix of typo'd papers. # Reed Equation 6 http://www.math.uvic.ca/faculty/reed/dPlN.3.pdf exponentiated per end of sec 3: rdpln-function(n,a=1,b=1,t=1,v=0){exp(v+t*rnorm(n,sd=t)+rexp(n,rate=1/a)-rexp(n,rate=1/b))} # Reed Equation 10: library(VGAM) rdpln2-function(n,a,b,v,t){ rlnorm(n,meanlog=v,sdlog=t)* rpareto(n,location=1,shape=1/a)/ rpareto(n,location=1,shape=1/b)} boxplot(data.frame(x1= log(rdpln(a=2.5,b=.01,t=0.45,v=6.5,n=10)), x2= log(rdpln2(a=2.5,b=.01,t=0.45,v=6.5,n=10 # Reed equation 8 http://www.math.uvic.ca/faculty/reed/dPlN.3.pdf # with S1 errata #1 from http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0048964 ddpln - function(x, a=1, b=1, v=0, t=1){ # Density of Double Pareto LogNormal distribution # from b. alzahrani cs_2...@hotmail.com email of 2013-11-13 # per formula 8 from http://cs.stanford.edu/people/jure/pubs/dpln-kdd08.pdf c - (a * b /(a+b)) norm1-pnorm((log(x)-v-(a*t^2))/t) norm2-pnorm((log(x)-v+(b*t^2))/t) expo1- a*v+(a^2*t^2/2) expo2- -b*v+(b^2*t^2/2) # edited from the paper's eqn 8 with: s/t/v/ z- (x^(-a-1)) * exp(expo1)*( norm1) y- (x^( b-1)) * exp(expo2)*(1-norm2) # 1-norm is the complementary CDF of N(0,1) c*(z+y) } Dave On Nov 14, 2013, at 9:12 AM, David Forrest d...@vims.edu wrote: I think exponentiation of eqn 6 from the Reed paper generates DPLN variates directly, so maybe: rdpln=function(n,a=1,b=1,t=1,v=0){exp(v+t*rnorm(n,sd=t)+rexp(n,rate=1/a)-rexp(n,rate=1/b))} Dave On Nov 13, 2013, at 4:34 PM, b. alzahrani cs_2...@hotmail.com wrote: You help is much appreciated. Just one last point if you could help, Now I want to pass this curve to a function that can generate random numbers distributed according to DPLN ( right curve). I found the package Runuran can do that http://cran.r-project.org/web/packages/Runuran/Runuran.pdf but I do not know how to do it I think it would be something similar to page 8 and 9. Regards ** Bander * From: d...@vims.edu To: cs_2...@hotmail.com Subject: Re: [R] Double Pareto Log Normal Distribution DPLN Date: Wed, 13 Nov 2013 21:13:43 + I read the parameters in Fig 4, right as DPLN[2.5,0.1,0.45,6.5], so: x- 10^seq(0,4,by=.1) plot(x,ddpln(x,a=2.5,b=.01,v=6.5,t=0.45),log='xy',type='l') ... and the attached graph does not look dissimilar the figure--It starts at 10^-2, goes through 10^-4 at x=100, and elbows down around 900 and passes through 10^-6 at about 2000. The correction of Reed helps -- The uncorrected Reed Eq9 equation suggests that the the 't' in Sehshadri Eq9 should be a 'v' , but it doesn't exactly make sense with the extra 'a' in there. If the errata clears that up, then your expo2 term looks just like the expo1 term, but with a=-b. On Nov 13, 2013, at 3:43 PM, b. alzahrani wrote: Thank you very much for the help and the change you suggested in my code, I also found a correction on equation 9 that has been published by Reed ( here http://www.math.uvic.ca/faculty/reed/dPlN.3.pdf i.e. the original paper on Double Pareto Log Normal Distribution ). can you please see the correction in this linkhttp://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0048964 (in Supporting Information section, appendix S1), does your suggested code coincide with the correction on this link? as I can see Actually, I am interested in the most right curve in figure 4. and if we plot the curve with same order of the paper's parameters you suggests: plot(x,ddpln(x,a=2.8,b=.01,v=6.5,t=0.45),log='xy',type='l') the curve is different from the one in the paper?? Thanks ** Bander Alzahrani, *! From: d...@vims.edu To: cs_2...@hotmail.com CC: r-h...@stat.math.ethz.ch Subject: Re: [R] Double Pareto Log Normal Distribution DPLN Date: Wed, 13 Nov 2013 19:09:34 + ...Additionally...your set of parameters match none of the curves in figure 4. I think the ordering of the parameters as listed on the graphs is different than in the text of the article. The 'v' parameter controls the location of the 'elbow' and should be near log(x) in each graph, while the 't' parameter is the sharpness of the elbow. So eyeballing the elbows: log(c(80,150,300))= 4.382027 5.010635 5.703782 These appear to match positional parameter #4 in
Re: [R] variable standardization in manova() call
thank you for your reply. However, your remark was not so clear to me so I attach a short script I tried to launch. The comparison between results got from MANOVA() call with the non-standardized and standardized version of the same data frame, convinced me that it is not necessary to standardize data before calling MANOVA. The only difference you get is in the residuals values, of course. Could you kindly confirm my conclusion? All the best, Sergio Fonda __ set.seed(1234) # non- standardized series x1 = rnorm(n=10, mean=50, sd=11) x2 = rnorm(n=10, mean=93, sd=23) x1 = rnorm(n=10, mean=217, sd=52) fact= rep(1:2,20) glob1=data.frame(cbind(x1,x2,x3,fact)) fitta1=manova(cbind(x1,x2,x3)~fact, data=glob1) fitta1.wilks=summary(fitta1, test=Wilks) summary.aov(fitta1) #after standardization x.stand=scale(glob1[,-4]) glob2=data.frame(x.stand,fact) fitta2=manova(cbind(x1,x2,x3)~fact, data=glob2) fitta2.wilks=summary(fitta2, test=Wilks) summary.aov(fitta2) 2013/11/4 Sergio Fonda sergio.fond...@gmail.com: Hi, I'm not able to get information about the following question: is the variables standardization a default option in manova() (stats package)? Or if you want to compare variables with different units or scales and rather different variances, you have to previously standardize the variables ? Thanks a lot for any help, Sergio Fonda www.unimore.it __ 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] export vector with write() introduces line breaks
Hi, I have a long vector which I want to export as a simple ascii text file via write(1:600, file='test.txt', sep=',') When I open the text file with my text editor I see that the data is structured in columns. So it seems that line breaks are introduced. How can I prevent this? Thank you! __ 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] export vector with write() introduces line breaks
1. Read ?write carefully. Note what it says about write() writing to columns and the link to cat(). 2. Use cat() with appropriate arguments to write your file instead. e.g. cat(1:600, file=yourfile,fill=FALSE) Cheers, Bert On Sat, Nov 16, 2013 at 11:20 AM, Martin Batholdy batho...@googlemail.com wrote: Hi, I have a long vector which I want to export as a simple ascii text file via write(1:600, file='test.txt', sep=',') When I open the text file with my text editor I see that the data is structured in columns. So it seems that line breaks are introduced. How can I prevent this? Thank you! __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 __ 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] contour plot axis correspondence
I am struggling with a contour plot. I want to place a cross over the minimum. alas, I don't seem to be able to map axes appropriately. here is what I mean: N - 1000 rm - rnorm(N, mean=0.0, sd=0.4) rx - rnorm(N, mean=0.0, sd=0.4) rt - rnorm(N, mean=0.0, sd=0.4) exploss - function(hdgM,hdgX) { ## this could be any function that is not vectorized losscosts - function(FirmV) { D - 50; ifelse( FirmVD, 0.2*(D-FirmV), 0) } FirmV - 100*(1+rt)*(1+rm)*(1+rx) + 100*(hdgM*(1+rm)-hdgM) + 100*(hdgX*(1+rx)-hdgX) mean( losscosts( FirmV ) ) } ss - seq(-2,0.5,0.1) MX - expand.grid( hdgM= ss, hdgX= ss ) MX$z - unlist(lapply( 1:nrow(MX), function(i) with(MX, exploss(hdgM[i],hdgX[i])) )) M - matrix(MX$z, nrow=length(ss), ncol=length(ss)) rownames(M) - colnames(M) - ss filled.contour( x=ss, y=ss, M ) vline - function(x, y=c(-99,99), ...) lines(c(x,x), y, ...) vline(-0.5, col=blue, lwd=3 ) how do I map the -0.5 in the vline() to the true -0.5? it is drawn . as a sidenote, it was not easy to figure out how I could plot an z function for an x-axis and y-axis. plot(x,y) is very intuitive. it would have been nice to have analogous plot3d(x,y,z) and contour(x,y,z) functions. as with everything in R, it probably exists, but I did not find it. my above code had to map MX (with x,y,z columns) into a matrix first. advice appreciated. best, /iaw Ivo Welch (ivo.we...@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.
Re: [R] contour plot axis correspondence
filled.contour() makes two plots (the contour plot and the legend) and must adjust some par() parameters to do that. It appears to set them back to a state where you cannot easily add things to the plot. There is a trick mentioned in Stackoverflow a while back about how to use the plot.title argument to fiddle with filled.contour() plots. In your case you could do something like x - 11:15 y - 101:108 z - outer(x,y,function(x,y)sin(x^2.5+y^.9/1.2)) filled.contour(x,y,z, plot.title=abline(v=12, h=103)) If you call abline() after filled.contour you can see how it the horizontal margin settings do not correspond to the plot you are trying to add to. abline(v=12, h=103, lwd=3, col=red, lty=2) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of ivo welch Sent: Saturday, November 16, 2013 11:40 AM To: r-help Subject: [R] contour plot axis correspondence I am struggling with a contour plot. I want to place a cross over the minimum. alas, I don't seem to be able to map axes appropriately. here is what I mean: N - 1000 rm - rnorm(N, mean=0.0, sd=0.4) rx - rnorm(N, mean=0.0, sd=0.4) rt - rnorm(N, mean=0.0, sd=0.4) exploss - function(hdgM,hdgX) { ## this could be any function that is not vectorized losscosts - function(FirmV) { D - 50; ifelse( FirmVD, 0.2*(D-FirmV), 0) } FirmV - 100*(1+rt)*(1+rm)*(1+rx) + 100*(hdgM*(1+rm)-hdgM) + 100*(hdgX*(1+rx)-hdgX) mean( losscosts( FirmV ) ) } ss - seq(-2,0.5,0.1) MX - expand.grid( hdgM= ss, hdgX= ss ) MX$z - unlist(lapply( 1:nrow(MX), function(i) with(MX, exploss(hdgM[i],hdgX[i])) )) M - matrix(MX$z, nrow=length(ss), ncol=length(ss)) rownames(M) - colnames(M) - ss filled.contour( x=ss, y=ss, M ) vline - function(x, y=c(-99,99), ...) lines(c(x,x), y, ...) vline(-0.5, col=blue, lwd=3 ) how do I map the -0.5 in the vline() to the true -0.5? it is drawn . as a sidenote, it was not easy to figure out how I could plot an z function for an x-axis and y-axis. plot(x,y) is very intuitive. it would have been nice to have analogous plot3d(x,y,z) and contour(x,y,z) functions. as with everything in R, it probably exists, but I did not find it. my above code had to map MX (with x,y,z columns) into a matrix first. advice appreciated. best, /iaw Ivo Welch (ivo.we...@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-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] optimization
There are lots of errors in your code. In particular, the optimization routines do not like functions that ignore the parameters. And you have not provided out or out1 to the optimizer -- they are returned as elements of func(), but not correctly. Please try some of the examples for optim or optimx to learn how to structure your problem. JN On 13-11-16 06:00 AM, r-help-requ...@r-project.org wrote: Message: 19 Date: Fri, 15 Nov 2013 09:17:47 -0800 (PST) From: IZHAK shabsogh ishaqb...@yahoo.com To: r-help@r-project.org r-help@r-project.org Subject: [R] optimization Message-ID: 1384535867.58595.yahoomail...@web142506.mail.bf1.yahoo.com Content-Type: text/plain x1-c(5.548,4.896,1.964,3.586,3.824,3.111,3.607,3.557,2.989,18.053,3.773,1.253,2.094,2.726,1.758,5.011,2.455,0.913,0.890,2.468,4.168,4.810,34.319,1.531,1.481,2.239,4.204,3.463,1.727) y-c(2.590,3.770,1.270,1.445,3.290,0.930,1.600,1.250,3.450,1.096,1.745,1.060,0.890,2.755,1.515,4.770,2.220,0.590,0.530,1.910,4.010,1.745,1.965,2.555,0.770,0.720,1.730,2.860,0.760) x2-c(0.137,2.499,0.419,1.699,0.605,0.677,0.159,1.699,0.340,2.899,0.082,0.425,0.444,0.225,0.241,0.099,0.644,0.266,0.351,0.027,0.030,3.400,1.499,0.351,0.082,0.518,0.471,0.036,0.721) k-rep(1,29) x-data.frame(k,x1,x2) freg-function(y,x1,x2){ reg- rlm(y ~ x1 + x2 , data=x) return(reg) } func - function(x1,x2,b){ fit-freg(y,x1,x2) b-c(coef(fit)) dv-1+ b[2]*x2^b[3] dv1-b[2]*x2^b[3]*log(x2) out - ( x1/(1+ b[2]*x2^b[3])) out1- c(-x1*x2^b[3]/dv^2,-x1* dv1/dv^2) return(list( out,out1)) } optim(par=c(b[2],b[3]), fn=out, gr =out1, method = c(BFGS), lower = -Inf, upper = Inf, control = list(), hessian = T) can someone help me try running this code because i try many occasion but prove abortive. the aim is to optimize the parameter of the model that is parameter estimates using optimization [[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] optimization
There are lots of errors in your code. In particular, the optimization routines do not like functions that ignore the parameters. I would like to nominate this delicious riposte as a fortune candidate. Anyone to second the motion? Dennis On Sat, Nov 16, 2013 at 1:26 PM, Prof J C Nash (U30A) nas...@uottawa.ca wrote: There are lots of errors in your code. In particular, the optimization routines do not like functions that ignore the parameters. And you have not provided out or out1 to the optimizer -- they are returned as elements of func(), but not correctly. Please try some of the examples for optim or optimx to learn how to structure your problem. JN On 13-11-16 06:00 AM, r-help-requ...@r-project.org wrote: Message: 19 Date: Fri, 15 Nov 2013 09:17:47 -0800 (PST) From: IZHAK shabsogh ishaqb...@yahoo.com To: r-help@r-project.org r-help@r-project.org Subject: [R] optimization Message-ID: 1384535867.58595.yahoomail...@web142506.mail.bf1.yahoo.com Content-Type: text/plain x1-c(5.548,4.896,1.964,3.586,3.824,3.111,3.607,3.557,2.989,18.053,3.773,1.253,2.094,2.726,1.758,5.011,2.455,0.913,0.890,2.468,4.168,4.810,34.319,1.531,1.481,2.239,4.204,3.463,1.727) y-c(2.590,3.770,1.270,1.445,3.290,0.930,1.600,1.250,3.450,1.096,1.745,1.060,0.890,2.755,1.515,4.770,2.220,0.590,0.530,1.910,4.010,1.745,1.965,2.555,0.770,0.720,1.730,2.860,0.760) x2-c(0.137,2.499,0.419,1.699,0.605,0.677,0.159,1.699,0.340,2.899,0.082,0.425,0.444,0.225,0.241,0.099,0.644,0.266,0.351,0.027,0.030,3.400,1.499,0.351,0.082,0.518,0.471,0.036,0.721) k-rep(1,29) x-data.frame(k,x1,x2) freg-function(y,x1,x2){ reg- rlm(y ~ x1 + x2 , data=x) return(reg) } func - function(x1,x2,b){ fit-freg(y,x1,x2) b-c(coef(fit)) dv-1+ b[2]*x2^b[3] dv1-b[2]*x2^b[3]*log(x2) out - ( x1/(1+ b[2]*x2^b[3])) out1- c(-x1*x2^b[3]/dv^2,-x1* dv1/dv^2) return(list( out,out1)) } optim(par=c(b[2],b[3]), fn=out, gr =out1, method = c(BFGS), lower = -Inf, upper = Inf, control = list(), hessian = T) can someone help me try running this code because i try many occasion but prove abortive. the aim is to optimize the parameter of the model that is parameter estimates using optimization [[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] optimization
On 11/17/13 11:49, Dennis Murphy wrote: There are lots of errors in your code. In particular, the optimization routines do not like functions that ignore the parameters. I would like to nominate this delicious riposte as a fortune candidate. Anyone to second the motion? Indeed. I so second! cheers, Rolf Turner __ 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] Change date time to epoch time
Hi All, I have time series data in (Year-month-date-hour-minute-second) format and i want to convert it to epoch time. I have tried the following: * Sys.time()** **[1] 2013-11-17 10:39:46 MYT** ** as.numeric(Sys.time())** **[1] 1384656006** ** as.numeric(2013-11-17 10:39:46 MYT)** **[1] NA** **Warning message:** **NAs introduced by coercion ** ** as.numeric(2013-11-17 10:39:46 MYT)** **[1] NA** **Warning message:** **NAs introduced by coercion ** * Please let me know which function in R converts date-time to epoch time. Regards, Vivek [[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] Change date time to epoch time
On Nov 16, 2013, at 8:46 PM, vivek kumar singh wrote: Hi All, I have time series data in (Year-month-date-hour-minute-second) format and i want to convert it to epoch time. I have tried the following: * Sys.time()** **[1] 2013-11-17 10:39:46 MYT** ** as.numeric(Sys.time())** **[1] 1384656006** Should we assume this is epoch time? ** as.numeric(2013-11-17 10:39:46 MYT)** **[1] NA** **Warning message:** **NAs introduced by coercion ** ** as.numeric(2013-11-17 10:39:46 MYT)** **[1] NA** If it is then this is close: as.numeric(as.POSIXct(2013-11-17 10:39:46 MYT)) (You may need to supply an appropriate TZ offset in seconds ?POSIXct -- David. **Warning message:** **NAs introduced by coercion ** * Please let me know which function in R converts date-time to epoch time. Regards, Vivek [[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. David Winsemius, MD Alameda, CA, USA __ 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] Change date time to epoch time
There are many epochs. Which one are you interested in? If you are interested in the POSIX time_t epoch, then you could look at ?DateTimeClasses to find out about ?as.POSIXct. You might also find my email on inputting zones informative [1]. Also, per the Posting Guide, please don't post in html format... it messes up your R code. [1] https://www.stat.math.ethz.ch/pipermail/r-help/2013-August/357939.html --- 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. vivek kumar singh vksingh.ii...@gmail.com wrote: Hi All, I have time series data in (Year-month-date-hour-minute-second) format and i want to convert it to epoch time. I have tried the following: * Sys.time()** **[1] 2013-11-17 10:39:46 MYT** ** as.numeric(Sys.time())** **[1] 1384656006** ** as.numeric(2013-11-17 10:39:46 MYT)** **[1] NA** **Warning message:** **NAs introduced by coercion ** ** as.numeric(2013-11-17 10:39:46 MYT)** **[1] NA** **Warning message:** **NAs introduced by coercion ** * Please let me know which function in R converts date-time to epoch time. Regards, Vivek [[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] 2SLS for panel data, re
Hi Millo Giovanni, Thanks for your response. In regards to my STATA code it would be: xi:xtiverg wecon polrightsreversed lnrgdpch execleft mulim2 c100rat c1100rat i.year (trade fdistockgdp = lnpop lnarea devcountrycomlanguage bitcum), re. Any suggestions will help so much.. Regards On Thursday, November 14, 2013 8:12:33 AM UTC-8, Phdstudent2 wrote: Hi, I am trying to estimate a 2sls using panel data (random effect model). I tried the same estimation in STATA using the ivtreg2 command. However STATA and R are giving me two different results. I figure there is something with my R code: iv=plm(formula=wecon~fdistockgdp +trade + polrightsreversed +lnrgdpch + execleft + muslim2+c100rat +c111rat +yeardum| polrightsreversed+lnrgdpch+ execleft+muslim2+c100rat+c111rat+yeardum +lnpop+lnarea+devcountrycomlanguage+bitcum, data = women, index = c(country, year), random.method = c(swar), inst.method = c(bvk), model=random) summary(iv) Coefficients : Estimate Std. Error t-value Pr(|t|) (Intercept) -0.2258528 0.2951301 -0.7653 0.4441954 fdistockgdp -0.0067207 0.0077315 -0.8693 0.3847993 trade 0.0068462 0.0023687 2.8903 0.0038863 ** polrightsreversed 0.0092366 0.0106174 0.8699 0.3844229 lnrgdpch 0.1246679 0.0389043 3.2045 0.0013724 ** execleft 0.1118046 0.0340817 3.2805 0.0010524 ** muslim2 -0.0044742 0.0012433 -3.5986 0.0003270 *** c100rat0.0226208 0.0595134 0.3801 0.7039114 c111rat0.0165951 0.0618339 0.2684 0.7884310 yeardum19820.1479947 0.0588824 2.5134 0.0120282 * yeardum19830.1783255 0.0606153 2.9419 0.0032958 ** yeardum19840.0344572 0.0597167 0.5770 0.5639907 yeardum19850.2206961 0.0610344 3.6159 0.0003060 *** yeardum19860.2428015 0.0649779 3.7367 0.0001912 *** yeardum19870.0489043 0.0615708 0.7943 0.4271186 yeardum19880.2243599 0.0605343 3.7063 0.0002155 *** yeardum19890.2215060 0.0624042 3.5495 0.0003940 *** yeardum19900.0688333 0.0607056 1.1339 0.2569648 yeardum19910.1370871 0.0638830 2.1459 0.0319892 * yeardum19920.1851857 0.0630868 2.9354 0.0033655 ** yeardum19930.0904620 0.0698526 1.2950 0.1954420 yeardum19940.1003735 0.0737431 1.3611 0.1736137 yeardum19950.1164818 0.0721240 1.6150 0.1064494 yeardum19960.0482520 0.0787232 0.6129 0.5399837 yeardum19970.1049161 0.0895001 1.1722 0.2412247 yeardum19980.2191887 0.1109757 1.9751 0.0483807 * yeardum19990.1573342 0.1397150 1.1261 0.2602422 yeardum20000.1532796 0.1627206 0.9420 0.3463059 --- However STATA gives me --- wecon | Coef. Std. Err. zP|z| [95% Conf. Interval] --+- --- trade | .0093915 .0027483 3.42 0.001 .004005 .014778 fdistockgdp | -.0169171 .0092405-1.83 0.067-.0350281 .0011938 polrightsreversed | .0165855 .0119176 1.39 0.164-.0067726 .0399436 lnrgdpch | .1045675 .0431179 2.43 0.015 .0200579 .189077 execleft | .1373652 .0384442 3.57 0.000 .0620159 .2127145 muslim2 | -.0043645 .0013551-3.22 0.001-.0070205 -.0017085 c100rat | .0480539 .0657304 0.73 0.465-.0807752 .1768831 c111rat | .0170048 .0676272 0.25 0.801-.1155421 .1495516 Really would appreciate any help explaining why the results are so different [[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] repeated-measures multiple regression/ANCOVA/MANCOVA
Dear List, I am trying to analyze a dataset where I have 1 continuous between-item variable (C), and 2 factorial within-item variables (3- and 2-level: F3, F2). I'm interested in whether slope of C is different from 0 at different combinations of F3 and F2, and whether it varies between these combinations. And unfortunately I need a decent anova-like table with p-values. The reason is that 1) this analysis is going to be repeated 9 times for different parts of the data (not comparable), so such an omnibus table will give a good overview of which places need a follow up with simpler models; 2) this is the norm in my field of reseach, although usually with factorial variables only. I'm wondering how to do it properly in R without falling into any pitfalls, avoiding violations of any assumptions (like sphericity) and what is the most apropriate type of sum of squares for this analysis. The 2 solutions I found so far are: based on nlme::lme(): res.lme = nlme::lme(data=d, y ~ C * F3 * F2, random = ~ 1|item/F3/F2) anova(res.lme) for type I SS Anova(res.lme, type=II/III) # for type II/III SS based on lmerTest package: res.lmertest = lmerTest::lmer(data=d, y ~ C * F3 * F2 + (1|item)) anova(res.lmertest) # for type III SS I also considered running repeated-measures ANCOVA using aov() with nested error terms, but that wouldn't protect me against sphericity assumption violations. I also considered using car::Anova() for running a repeated-measures MANCOVA analysis, but if I got this thread right (http://thread.gmane.org/gmane.comp.lang.r.general/270271/focus=270275), this is (at present) not possible to do. Are these ways of analyzing data valid? Concerning the type of SS: I tried to read all discussions in this list on this topic. If I got it right, since I'm interested in interactions of C with other factors in the first place, in my case using SS type III would make sense - is this a good logic? Many thanks for help, Jakub __ 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] Bug in predict.lm?
This definitely looks like a bug and should really be reported. Denes' diagnosis is right on. I get the bug here: z - lm(rnorm(10)~I(1:10)) predict(z, int=conf, scale=1) Error in predict.lm(z, int = conf, scale = 1) : object 'w' not found And also here: z - lm(rnorm(10)~I(1:10)) predict(z, se.fit=T, scale=1) Error in predict.lm(z, se.fit = T, scale = 1) : object 'w' not found ** platform x86_64-w64-mingw32 arch x86_64 os mingw32 system x86_64, mingw32 status major 3 minor 0.2 year 2013 month 09 day25 svn rev63987 language R version.string R version 3.0.2 (2013-09-25) nickname Frisbee Sailing ** -Original Message- From: peter dalgaard [mailto:pda...@gmail.com] Sent: Saturday, November 16, 2013 4:36 AM To: Charles Berry Cc: r-help Subject: Re: [R] Bug in predict.lm? On 16 Nov 2013, at 03:06 , Charles Berry ccbe...@ucsd.edu wrote: Rolf Turner r.turner at auckland.ac.nz writes: I *do* see the same phenomenon that Bert describes and the code of predict.lm() *does* appear to contain a bug. There is a line: [snip] The operative difference between my set-up and Chuck's is that I am using version 3.0.2 Patched. So I am very puzzled as to why Chuck does *not* get an error thrown! [rest deleted] The answer is that I made a rookie error. I ran example(predict.lm) before trying Bert's ECM. And then I did not bother to see what example(predict.lm) left lying around in R_GlobalEnv ... As you can probably guess, there was an object called 'w'. So predict.lm finds it and is.null(w) is FALSE. Mea culpa! Hmm, maybe, but this is the sort of thing that code analysis tries to catch (as in no visible binding for global variable 'w'). Apparently, the code checker is not smart enough to detect cases where a local variable is _sometimes_ not defined. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@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.
Re: [R] Apply function to one specific column / Alternative to for loop
This might be of some use : http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/ Umair Durrani email: umairdurr...@outlook.com Date: Sat, 16 Nov 2013 07:30:29 -0800 From: ron...@gmx.net To: r-help@r-project.org Subject: [R] Apply function to one specific column / Alternative to for loop Hi guys, I am a total newbie to R, so I hope this isn't a totally dumb question. I have a dataframe with a title in one row and the corresponding values in the next rows. Let's take this example: test_df - data.frame(cbind(titel = , x = 4:5, y = 1:2)) test_df = rbind(cbind(titel=1.Test, x=, y=), test_df, cbind(titel=2.Test, x=, y=), test_df, cbind(titel=3.Test, x=, y=), test_df) test_df titel x y 1 1.Test 24 1 35 2 4 2.Test 54 1 65 2 7 3.Test 84 1 95 2 What I want to have is: titel x y 2 1.Test 4 1 3 1.Test 5 2 5 2.Test 4 1 6 2.Test 5 2 8 3.Test 4 1 9 3.Test 5 2 In my example, the title is in every third line, but in my real data there is no pattern. Each title has at least one line but can have x lines. I was able to solve my problem in a for loop with the following code: test_df$titel - as.character(test_df$titel) for (i in 1:nrow(test_df)) { if (nchar(test_df$titel[i])==0){ test_df$titel[i]=test_df$titel[i-1] } } test_df - subset(test_df,test_df$x!=) The problem is, I have a lot of data and the for loop is obviously very slow. Is there a more elegant way to achieve the same? I think I have to use the apply function, but I don't know how to use it with just one column. -- View this message in context: http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566.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. [[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] Calculate Range
Hi My R version is the current version as at 15 Nov 2013. I have tried to calculate range using tapply() with FUN=range. tapply() returns two fields, the ID field and a field of two text items one is the maximum and the other is the minimum. I take as the difference max - min, does R use a different term for range in tapply? I have also tried aggregate() with Fun=range, with Fun=min and FUN=max and they also gave problems. What is the best route to calculate ranges for groups within a data frame. Thanks. Scriptham. -- View this message in context: http://r.789695.n4.nabble.com/Calculate-Range-tp4680579.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] Apply function to one specific column / Alternative to for loop
Hi, Try: indx - grep(Test,test_df[,1]) ##assuming that there is some pattern res - within(test_df[-indx,],titel - rep(test_df$titel[indx], diff(c(indx,nrow(test_df)+1))-1)) ## If you need to change the class res[] - lapply(res,function(x) if(any(grepl([[:alpha:]],x))) as.character(x) else as.numeric(as.character(x))) ##Using data.frame(cbind()), etc. creates A.K. On Saturday, November 16, 2013 11:14 AM, Stageexp ron...@gmx.net wrote: Hi guys, I am a total newbie to R, so I hope this isn't a totally dumb question. I have a dataframe with a title in one row and the corresponding values in the next rows. Let's take this example: test_df - data.frame(cbind(titel = , x = 4:5, y = 1:2)) test_df = rbind(cbind(titel=1.Test, x=, y=), test_df, cbind(titel=2.Test, x=, y=), test_df, cbind(titel=3.Test, x=, y=), test_df) test_df titel x y 1 1.Test 2 4 1 3 5 2 4 2.Test 5 4 1 6 5 2 7 3.Test 8 4 1 9 5 2 What I want to have is: titel x y 2 1.Test 4 1 3 1.Test 5 2 5 2.Test 4 1 6 2.Test 5 2 8 3.Test 4 1 9 3.Test 5 2 In my example, the title is in every third line, but in my real data there is no pattern. Each title has at least one line but can have x lines. I was able to solve my problem in a for loop with the following code: test_df$titel - as.character(test_df$titel) for (i in 1:nrow(test_df)) { if (nchar(test_df$titel[i])==0){ test_df$titel[i]=test_df$titel[i-1] } } test_df - subset(test_df,test_df$x!=) The problem is, I have a lot of data and the for loop is obviously very slow. Is there a more elegant way to achieve the same? I think I have to use the apply function, but I don't know how to use it with just one column. -- View this message in context: http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566.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] order() function, decreasing=TRUE unexpected behaviour
There appears to be an issue with the decreasing=TRUE option on the order() function that indicates either a bug or perhaps a design flaw (potentially flawed because I would suggest the majority of users would expect different behaviour). # demonstration of problem: x - c(2,1,3,4,5) order(x) order(x, decreasing=TRUE) order(x) correctly reports the order as: 2 1 3 4 5 I expected the result for order(x, decreasing=TRUE) to be: 4 5 3 2 1 but the values actually reported are: 5 4 3 1 2 which is also the result of order(-x) (this may indicate a possible cause of this issue?). Thus, the decreasing=TRUE option in the order() function does not reverse the order of the values reported under the default of decreasing=FALSE in an example with no ties. If this behaviour is by design I am struggling to understand what purpose the decreasing=TRUE option serves and would like to suggest that this is not consistent with how many (most?) users would expect this option to work. Tested on R2.15.3 and 3.0.1 (both x86_64 linux-gnu) with identical results. I have also tried it on other series of numbers with the same result. Apologies if this has been discussed or reported elsewhere but I could not find any relevant posts. Perhaps I have failed to understand some fundamental issue? Sorry if this is me being dimwitted - it does seem hard to believe that there is a bug in a relatively straightforward r-base function. But I thought I would mention this as it could impact many people. Many thanks to all the R developers and R community for producing and supporting such an outstanding and invaluable product. Hawthorne Beyer University of Queensland [[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] r documentation rugarch egarch
Hi, I`m about to switch from STATA to R and have serious troubles to find proper documentations on the internet. Right now I try to find a proper documentation of the eGARCH model being part of the rugarch package. Neither here http://cran.r-project.org/web/packages/rugarch/vignettes/Introduction_to_the_rugarch_package.pdf nor here http://cran.r-project.org/web/packages/rugarch/rugarch.pdf could i find some information that was helping. In this post http://r.789695.n4.nabble.com/RUGARCH-eGARCH-and-variance-targeting-td4634896.html someone proposes to read the vignette on how the unconditional variance is calculated for the eGARCH model I don`t know what is meant by vignette. The R-built-in help function tells me about eGARCH: assymetry term: gamma1 Thanks a lot R-help. This is relly a lot of information. I`d like to find out about the eGARCH model notation used in R, the settings and estimation techniques that are used and/or can be configured in this case, ... When searching the internet I`ve also not been very successful. When working with STATA I`ve so far never had troubles to find the right information. So i think I`m not in general too stupid to do some search on the internet. Please can anyone tell me what this vignette is supposed to be and where to find it? Thanks a lot, Samson -- View this message in context: http://r.789695.n4.nabble.com/r-documentation-rugarch-egarch-tp4680580.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] selecting optimal cluster validation score
Hi: I have calculated the Silhouette score and Dunn score after hierarchical clustering for 3 clusters: #Distance measure d - dist(USArrests, method = euclidean) #Hierarchical clustering hc - hclust(dist(USArrests), ave) #calculating silhouette value for 3 clusters sil- silhouette(cutree(hc, k=3), d) #calculating Dunn index for 3 clusters clus - cutree(hc, 3) dun - dunn(d, clus) How can the best of the two score be obtained? Is there any package to automatically obtain the optimal (or best) of the Silhouette and the Dunn scores ? Regards: John __ 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] R for a stats intro for undergrads in the US?
Hello, All: Would anyone recommend R for an introductory statistics class for freshman psychology students in the US? If yes, might there be any notes for such available? I just checked r-projects.org and CRAN contributed documentation and found nothing. I have a friend who teaches such a class, and wondered if R might be suitable. The alternative is SPSS at $406 per student. Thanks, Spencer -- Spencer Graves, PE, PhD President and Chief Technology Officer Structure Inspection and Monitoring, Inc. 751 Emerson Ct. San José, CA 95126 ph: 408-655-4567 web: www.structuremonitoring.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] R for a stats intro for undergrads in the US?
Hi Spencer, I would definitely recommend R for introductory stats. course because it is free and easy to learn. You can visit www.twotorials.com for two-minute tutorials on R. Also www.coursera.org offers many free courses on R, for intro stats check this out: https://www.coursera.org/course/stats1 Hope this helps, Umair Durrani email: umairdurr...@outlook.com Date: Sat, 16 Nov 2013 18:19:16 -0800 From: spencer.gra...@prodsyse.com To: R-help@r-project.org Subject: [R] R for a stats intro for undergrads in the US? Hello, All: Would anyone recommend R for an introductory statistics class for freshman psychology students in the US? If yes, might there be any notes for such available? I just checked r-projects.org and CRAN contributed documentation and found nothing. I have a friend who teaches such a class, and wondered if R might be suitable. The alternative is SPSS at $406 per student. Thanks, Spencer -- Spencer Graves, PE, PhD President and Chief Technology Officer Structure Inspection and Monitoring, Inc. 751 Emerson Ct. San José, CA 95126 ph: 408-655-4567 web: www.structuremonitoring.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. [[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] R for a stats intro for undergrads in the US?
Hi, Umair Durrani: Thanks very much for the quick reply. The course you mentioned does not feature psychology, that I could see. However, my friend might be able to use pieces of that course in hers. Thanks again. Spencer On 11/16/2013 7:58 PM, umair durrani wrote: /Hi Spencer,/ / / /I would definitely recommend R for introductory stats. course because it is free and easy to learn. You can visit www.twotorials.com for two-minute tutorials on R. Also www.coursera.org offers many free courses on R, for intro stats check this out: /https://www.coursera.org/course/stats1 Hope this helps, /Umair Durrani/ /email: umairdurr...@outlook.com mailto:umairdurr...@hotmail.com/ Date: Sat, 16 Nov 2013 18:19:16 -0800 From: spencer.gra...@prodsyse.com To: R-help@r-project.org Subject: [R] R for a stats intro for undergrads in the US? Hello, All: Would anyone recommend R for an introductory statistics class for freshman psychology students in the US? If yes, might there be any notes for such available? I just checked r-projects.org and CRAN contributed documentation and found nothing. I have a friend who teaches such a class, and wondered if R might be suitable. The alternative is SPSS at $406 per student. Thanks, Spencer __ 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] order() function, decreasing=TRUE unexpected behaviour
I think you are confused, and there is no problem with the order function. Keep in mind that order returns the index values in a sequence such that when the result of the order function is used as indexes for the original sequence then the data will be sorted as desired. You may see the error of your ways more clearly if you use a vector of strings like c(b,a,c,d,e) instead of integer values. Please configure your email client to send plain text to this list as the Posting Guide requests, as HTML messes up R code. --- 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. Hawthorne Beyer h.be...@uq.edu.au wrote: There appears to be an issue with the decreasing=TRUE option on the order() function that indicates either a bug or perhaps a design flaw (potentially flawed because I would suggest the majority of users would expect different behaviour). # demonstration of problem: x - c(2,1,3,4,5) order(x) order(x, decreasing=TRUE) order(x) correctly reports the order as: 2 1 3 4 5 I expected the result for order(x, decreasing=TRUE) to be: 4 5 3 2 1 but the values actually reported are: 5 4 3 1 2 which is also the result of order(-x) (this may indicate a possible cause of this issue?). Thus, the decreasing=TRUE option in the order() function does not reverse the order of the values reported under the default of decreasing=FALSE in an example with no ties. If this behaviour is by design I am struggling to understand what purpose the decreasing=TRUE option serves and would like to suggest that this is not consistent with how many (most?) users would expect this option to work. Tested on R2.15.3 and 3.0.1 (both x86_64 linux-gnu) with identical results. I have also tried it on other series of numbers with the same result. Apologies if this has been discussed or reported elsewhere but I could not find any relevant posts. Perhaps I have failed to understand some fundamental issue? Sorry if this is me being dimwitted - it does seem hard to believe that there is a bug in a relatively straightforward r-base function. But I thought I would mention this as it could impact many people. Many thanks to all the R developers and R community for producing and supporting such an outstanding and invaluable product. Hawthorne Beyer University of Queensland [[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] r documentation rugarch egarch
You don't seem to read very well. The rugarch vignette that you said you read clearly states that questions on that package should be directed to the R-sig-finance mailing list. This is not that list. Before you go deliver this screed to that forum, you may want to give yourself some time to cool off because at this rate you are only going to alienate people who otherwise might help you. If you fail to adjust your approach, we the users of and contributors to R who answer each others questions on our own time (not tech support staff paid to put up with irate buyers of R, since said staff don't exist) are merely going to wait quietly for you to go back to wherever you came from. PS: You might try typing the word vignette into Google. --- 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. randomsamson joh...@web.de wrote: Hi, I`m about to switch from STATA to R and have serious troubles to find proper documentations on the internet. Right now I try to find a proper documentation of the eGARCH model being part of the rugarch package. Neither here http://cran.r-project.org/web/packages/rugarch/vignettes/Introduction_to_the_rugarch_package.pdf nor here http://cran.r-project.org/web/packages/rugarch/rugarch.pdf could i find some information that was helping. In this post http://r.789695.n4.nabble.com/RUGARCH-eGARCH-and-variance-targeting-td4634896.html someone proposes to read the vignette on how the unconditional variance is calculated for the eGARCH model I don`t know what is meant by vignette. The R-built-in help function tells me about eGARCH: assymetry term: gamma1 Thanks a lot R-help. This is relly a lot of information. I`d like to find out about the eGARCH model notation used in R, the settings and estimation techniques that are used and/or can be configured in this case, ... When searching the internet I`ve also not been very successful. When working with STATA I`ve so far never had troubles to find the right information. So i think I`m not in general too stupid to do some search on the internet. Please can anyone tell me what this vignette is supposed to be and where to find it? Thanks a lot, Samson -- View this message in context: http://r.789695.n4.nabble.com/r-documentation-rugarch-egarch-tp4680580.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] The smallest enclosing ball problem
Forgot to forward my answer to R-help. Berend On 16-11-2013, at 13:11, Hans W.Borchers hwborch...@googlemail.com wrote: I wanted to solve the following geometric optimization problem, sometimes called the enclosing ball problem: Given a set P = {p_1, ..., p_n} of n points in R^d, find a point p_0 such that max ||p_i - p_0|| is minimized. A known algorithm to solve this as a Qudratic Programming task is as follows: Define matrix C = (p_1, ..., p_n), i.e. coordinates of points in columns, and minimize x' C' C x - \sum (p_i' p_i) x_i subject to \sum x_1 = 1, x_i = 0 . Let x0 = (x0_1, ..., x0_n) be an optimal solution, then the linear combination p0 = \sum x0_i p_i is the center of the smallest enclosing ball, and the negative value of the objective function at x0 is the squared radius of the ball. As an example, find the smallest ball enclosing the points (1,0,0), (0,1,0), and (0.5, 0.5, 0.1) in 3-dim. space. We would expect the center of the ball to be at (0.5, 0.5, 0), and with radius 1/sqrt(2). C - matrix( c(1, 0, 0.5, 0, 1, 0.5, 0, 0, 0.1), ncol = 3, byrow = TRUE) # We need to define D = 2C' C, d = (p_i' p_i), A = c(1,1,1), and b = 1 D - 2 * t(C) %*% C d - apply(C^2, 2, sum) A - matrix(1, nrow = 1, ncol = 3) b - 1 # Now solve with solve.QP() in package quadprog ... require(quadprog) sol1 - solve.QP(D, d, t(A), b, meq = 1) # ... and check the result sum(sol1$solution) # 1 p0 - c(C %*% sol1$solution)# 0.50 0.50 -2.45 r0 - sqrt(-sol1$value) # 2.55 # distance of all points to the center sqrt(colSums((C - p0)^2)) # 2.55 2.55 2.55 As a result we get a ball such that all points lie on the boundary. The same happens for 100 points in 100-dim. space (to keep D positive definite, n = d is required). That is a very nice, even astounding result, but not what I had hoped for! At the risk of making a complete fool of myself. Why the restriction: \sum x_1 = 1, x_i = 0 ? Isn’t just x_i = 0 sufficient? Change your code with this A - diag(3) b - rep(0,3) sol2 - solve.QP(D, d, A, b, meq = 0) sol2 resulting in this $solution [1] 0.5 0.5 0.0 $value [1] -0.5 $unconstrained.solution [1] 12.75 12.75 -24.50 $iterations [1] 2 0 $Lagrangian [1] 0.00 0.00 0.49 $iact [1] 3 And $solution seems to be what you want. And: p0 - c(C %*% sol2$solution) r0 - sqrt(-sol2$value) # distance of all points to the center sqrt(colSums((C - p0)^2)) gives the same results as LowRankQP for the last expression. Berend Compare this with another quadratic programming solver in R, for example LowRankQP() in the package of the same name. require(LowRankQP) sol2 - LowRankQP(D, -d, A, b, u = rep(3, 3), method=LU) p2 - c(C %*% sol2$alpha) # 5.00e-01 5.00e-01 1.032019e-12 sqrt(colSums((C - p2)^2)) # 0.7071068 0.7071068 0.100 The correct result, and we get correct solutions in higher dimensions, too. LowRankQP works also if we apply it with n d, e.g., 100 points in 2- or 3-dimensional space, when matrix D is not positive definite -- solve.QP( ) does not work in these cases. *** What do I do wrong in calling solve.QP()? *** My apologies for this overly long request. I am sending it through the weekend hoping that someone may have a bit of time to look at it more closely. Hans Werner __ 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] Calculate Range
On 11/17/2013 08:49 AM, SCRIPTHAM wrote: Hi My R version is the current version as at 15 Nov 2013. I have tried to calculate range using tapply() with FUN=range. tapply() returns two fields, the ID field and a field of two text items one is the maximum and the other is the minimum. I take as the difference max - min, does R use a different term for range in tapply? I have also tried aggregate() with Fun=range, with Fun=min and FUN=max and they also gave problems. What is the best route to calculate ranges for groups within a data frame. Hi Scriptham, It looks like you want to get the difference between the maximum and minimum values rather than the actual values. Define a function: range_span-function(x) return(diff(range(x))) and use that as the FUN argument. 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.