Re: [R] random numbers with constraints
Thanks again for your help, One digit after the decimal point is enough for my purposes; so, I can round the qexp function, even if possible errors in floating points are not a problem. Thank you very very much, Denis Il giorno mer 27 gen 2021 alle ore 20:48 Abby Spurdle ha scritto: > I note that there's a possibility of floating point errors. > If all values have one digit after the decimal point, you could replace: > qexp (p, rate) with round (qexp (p, rate), 1). > > However, sometimes uniroot will fail, due to problems with input. > > On Thu, Jan 28, 2021 at 5:02 AM Denis Francisci > wrote: > > > > Wonderful! > > This is exactly what I need! > > Thank you very much!! > > > > Denis > > > > > > > > Il giorno mer 27 gen 2021 alle ore 10:58 Abby Spurdle < > spurdl...@gmail.com> ha scritto: > >> > >> u <- runif (410) > >> u <- (u - min (u) ) / diff (range (u) ) > >> > >> constrained.sample <- function (rate) > >> { plim <- pexp (c (9.6, 11.6), rate) > >> p <- plim [1] + diff (plim) * u > >> qexp (p, rate) > >> } > >> > >> diff.sum <- function (rate) > >> sum (constrained.sample (rate) ) - 4200 > >> > >> rate <- uniroot (diff.sum, c (1, 2) )$root > >> q <- constrained.sample (rate) > >> > >> length (q) > >> range (q) > >> sum (q) > >> > >> > >> On Wed, Jan 27, 2021 at 9:03 PM Denis Francisci > >> wrote: > >> > > >> > Hi, > >> > I would like to generate random numbers in R with some constraints: > >> > - my vector of numbers must contain 410 values; > >> > - min value must be 9.6 and max value must be 11.6; > >> > - sum of vector's values must be 4200. > >> > Is there a way to do this in R? > >> > And is it possible to generate this series in such a way that it > follows a > >> > specific distribution form (for example exponential)? > >> > Thank you in advance, > >> > > >> > D. > >> > > >> > [[alternative HTML version deleted]] > >> > > >> > __ > >> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> > https://stat.ethz.ch/mailman/listinfo/r-help > >> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > >> > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] random numbers with constraints
Wonderful! This is exactly what I need! Thank you very much!! Denis Il giorno mer 27 gen 2021 alle ore 10:58 Abby Spurdle ha scritto: > u <- runif (410) > u <- (u - min (u) ) / diff (range (u) ) > > constrained.sample <- function (rate) > { plim <- pexp (c (9.6, 11.6), rate) > p <- plim [1] + diff (plim) * u > qexp (p, rate) > } > > diff.sum <- function (rate) > sum (constrained.sample (rate) ) - 4200 > > rate <- uniroot (diff.sum, c (1, 2) )$root > q <- constrained.sample (rate) > > length (q) > range (q) > sum (q) > > > On Wed, Jan 27, 2021 at 9:03 PM Denis Francisci > wrote: > > > > Hi, > > I would like to generate random numbers in R with some constraints: > > - my vector of numbers must contain 410 values; > > - min value must be 9.6 and max value must be 11.6; > > - sum of vector's values must be 4200. > > Is there a way to do this in R? > > And is it possible to generate this series in such a way that it follows > a > > specific distribution form (for example exponential)? > > Thank you in advance, > > > > D. > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] random numbers with constraints
Hi, I would like to generate random numbers in R with some constraints: - my vector of numbers must contain 410 values; - min value must be 9.6 and max value must be 11.6; - sum of vector's values must be 4200. Is there a way to do this in R? And is it possible to generate this series in such a way that it follows a specific distribution form (for example exponential)? Thank you in advance, D. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] problem with PCA
Thank you David for your answer. If I understood the relative positions of variable arrows don't reflect the coefficient of correlation of the original variables. In fact these positions change if I use different PC axes. But in some manual about PCA in R I read: "Pairs of variables that form acute angles at the origin, close to 0°, should be highly and positively correlated; variables close to right angles tend to have low correlation; variables at obtuse angles, close to 180°, tend to have high negative correlation". And If I do a fictional test, it seems true: tb<-data.frame( c(1,2,3,4,5,6,7,8,9), #orig data c(2,4,5,8,10,12,14,16,18),#strong positive correlation c(25,29,52,63,110,111,148,161,300),#weakly correlation c(-1,-2,-3,-4,-5,-6,-7,-8,-9),#strong negative correlation c(3,8,4,6,1,3,2,5,7)#not correlation ) names(tb)<-c("orig","corr+","corr+2","corr-","random") pca<-prcomp(as.matrix(tb),scale=T) biplot(pca,choices = c(1,2)) On the first 2 PC the positions of arrows reflect perfectly the original correlations. My data behaviour differently, maybe because my original variables are not strong correlated? 2017-03-10 15:49 GMT+01:00 David L Carlson : > This is more a question about principal components analysis than about R. > You have 4 variables and they are moderately correlated with one another > (weight and hole are only .2). When the data consist of measurements, this > usually suggests that the overall size of the object is being partly > measured by each variable. In your case object size is measured by the > first principle component (PC1) with larger objects having more negative > scores so larger objects are on the left and smaller ones are on the right > of the biplot. > > The biplot can only display 2 of the 4 dimensions of your data at one > time. In the first 2 dimensions, diam and height are close together, but in > the 3rd dimension (PC3), they are on opposite sides of the component. If > you plot different pairs of dimensions (e.g. 1 with 3 or 2 with 3, see > below), the arrows will look different because you are looking from > different directions. > > > pca > Standard deviations: > [1] 1.5264292 0.8950379 0.7233671 0.5879295 > > Rotation: > PC1 PC2 PC3PC4 > height -0.5210224 -0.06545193 0.80018012 -0.2897646 > diam -0.5473677 0.06309163 -0.57146893 -0.6081376 > hole -0.4598646 -0.70952862 -0.17476677 0.5045297 > weight -0.4663141 0.69878797 -0.05090785 0.5400508 > > > biplot(pca, choices=c(1, 3)) > > biplot(pca, choices=c(2, 3)) > > - > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Denis > Francisci > Sent: Friday, March 10, 2017 4:45 AM > To: R-help Mailing List > Subject: [R] problem with PCA > > Hi all. > I'm newbie in PCA by I don't understand a behaviour of R. > I have this data matrix: > > >mx_fus > height diam hole weight > 12.3 3.5 1.1 18 > 22.0 3.5 0.9 17 > 33.8 4.3 0.7 34 > 42.1 3.4 0.9 15 > 52.3 3.8 1.0 19 > 62.2 3.8 1.0 19 > 73.2 4.4 0.9 34 > 83.0 4.3 1.0 30 > 92.8 3.9 0.9 21 > 10 3.3 4.2 1.1 33 > 11 2.3 3.9 0.9 25 > 12 2.3 3.3 0.5 17 > 13 0.9 2.4 0.4 10 > 14 1.4 2.4 0.5 10 > 15 2.2 3.6 0.7 22 > 16 2.9 3.8 0.8 30 > 17 2.9 3.5 0.6 27 > 18 2.3 3.5 0.5 24 > 19 1.8 2.3 0.5 29 > 20 1.4 2.5 0.6 34 > 21 0.8 2.3 0.6 21 > 22 1.8 2.4 0.6 23 > 23 1.5 2.2 0.67 > 24 0.9 1.7 0.4 14 > 25 2.1 2.2 0.5 25 > 26 1.3 2.4 0.6 33 > 27 1.3 2.7 0.4 39 > 28 0.5 2.2 0.5 13 > 29 1.4 4.2 0.8 23 > 30 1.6 2.0 0.4 30 > 31 1.4 2.2 0.6 25 > 32 1.8 2.5 0.6 28 > 33 1.4 2.6 0.6 41 > 34 1.6 2.3 0.3 32 > 35 1.6 2.5 0.5 41 > 36 2.8 2.9 0.8 47 > 37 0.6 2.5 0.8 21 > 38 1.6 2.8 0.7 13 > 39 1.7 3.3 0.8 17 > 40 1.6 3.9 1.9 20 > 41 1.4 4.7 0.9 26 > 42 1.2 4.2 0.7 21 > 43 3.5 4.2 0.9 47 > 44 2.3 3.6 0.7 24 > 45 2.3 3.4 0.4 21 > 46 1.9 2.6 0.7 14 > 47 1.9 3.0 0.7 15 > 48 2.7 3.7 0.9 26 > 49 3.0 3.8 0.7 35 > 50 1.2 2.0 0.75 > 51 1.6 2.5 0.5 15 > 52 1.3 2.6 0.5 16 > 53 2.5 3.9 0.9 32 > 54 0.9 3.3 0.69 > 55 1.8 2.4 0.5 17 > 56 2.4 3.7 1.1 30 > 57 2.1 3.5 1.1 22 > 58 2.6 3.9 1.0 38 &g
[R] problem with PCA
Hi all. I'm newbie in PCA by I don't understand a behaviour of R. I have this data matrix: >mx_fus height diam hole weight 12.3 3.5 1.1 18 22.0 3.5 0.9 17 33.8 4.3 0.7 34 42.1 3.4 0.9 15 52.3 3.8 1.0 19 62.2 3.8 1.0 19 73.2 4.4 0.9 34 83.0 4.3 1.0 30 92.8 3.9 0.9 21 10 3.3 4.2 1.1 33 11 2.3 3.9 0.9 25 12 2.3 3.3 0.5 17 13 0.9 2.4 0.4 10 14 1.4 2.4 0.5 10 15 2.2 3.6 0.7 22 16 2.9 3.8 0.8 30 17 2.9 3.5 0.6 27 18 2.3 3.5 0.5 24 19 1.8 2.3 0.5 29 20 1.4 2.5 0.6 34 21 0.8 2.3 0.6 21 22 1.8 2.4 0.6 23 23 1.5 2.2 0.67 24 0.9 1.7 0.4 14 25 2.1 2.2 0.5 25 26 1.3 2.4 0.6 33 27 1.3 2.7 0.4 39 28 0.5 2.2 0.5 13 29 1.4 4.2 0.8 23 30 1.6 2.0 0.4 30 31 1.4 2.2 0.6 25 32 1.8 2.5 0.6 28 33 1.4 2.6 0.6 41 34 1.6 2.3 0.3 32 35 1.6 2.5 0.5 41 36 2.8 2.9 0.8 47 37 0.6 2.5 0.8 21 38 1.6 2.8 0.7 13 39 1.7 3.3 0.8 17 40 1.6 3.9 1.9 20 41 1.4 4.7 0.9 26 42 1.2 4.2 0.7 21 43 3.5 4.2 0.9 47 44 2.3 3.6 0.7 24 45 2.3 3.4 0.4 21 46 1.9 2.6 0.7 14 47 1.9 3.0 0.7 15 48 2.7 3.7 0.9 26 49 3.0 3.8 0.7 35 50 1.2 2.0 0.75 51 1.6 2.5 0.5 15 52 1.3 2.6 0.5 16 53 2.5 3.9 0.9 32 54 0.9 3.3 0.69 55 1.8 2.4 0.5 17 56 2.4 3.7 1.1 30 57 2.1 3.5 1.1 22 58 2.6 3.9 1.0 38 59 2.6 3.6 1.0 27 60 2.6 4.1 1.0 34 61 2.9 3.6 0.8 32 62 2.6 3.3 0.7 22 63 1.8 2.5 0.7 26 64 3.0 2.8 1.32 65 0.5 2.2 0.43 66 1.9 3.4 0.7 14 67 1.4 3.8 0.9 18 68 2.0 4.0 1.0 30 69 3.1 4.0 1.3 21 70 2.5 4.0 0.8 19 71 2.5 4.5 1.0 20 72 1.8 3.5 1.4 18 73 2.1 3.5 1.4 25 74 1.5 2.6 0.59 75 2.8 3.2 1.2 16 76 1.0 5.0 0.3 32 77 0.3 5.8 0.5 56 78 0.5 1.5 0.21 79 0.7 1.4 0.21 80 0.5 1.3 0.21 81 0.7 3.3 0.47 82 1.9 4.7 1.0 24 83 3.1 4.2 0.9 49 84 2.8 3.6 0.7 28 85 2.7 3.2 0.7 29 86 3.0 4.0 0.9 36 87 1.7 2.7 0.7 14 88 1.5 2.9 0.7 18 89 2.9 3.5 0.7 30 90 3.0 3.4 0.8 30 91 2.0 2.8 0.5 14 92 2.4 3.5 0.7 24 93 0.8 4.1 0.6 12 94 1.7 2.5 0.5 23 95 1.4 2.4 0.8 31 96 1.5 2.7 0.4 20 97 2.6 3.7 0.6 31 98 2.6 3.0 0.6 18 99 2.5 5.0 0.7 40 100 2.5 3.7 0.5 30 101 2.4 2.9 0.7 17 102 2.3 3.0 0.5 15 103 2.2 3.3 0.6 19 104 1.5 2.1 0.55 105 2.0 2.2 0.5 10 106 2.6 3.5 0.6 26 107 2.3 3.0 0.6 15 108 2.5 4.5 0.7 40 109 2.1 3.1 0.5 15 110 1.3 2.1 0.8 14 111 0.8 2.5 0.25 112 0.6 3.1 0.78 I perform a PCA in R >pca<-prcomp(mx_fus,scale=TRUE) >biplot(pca, choices = c(1,2), cex=0.7) The biplot put the arrows of diam and height very near on the first component axis. So I understand that these 2 variables are well represented in the PC1 and they are correlated each other. But if I test the correlation, the value o correlation coefficient is low >cor(mx_fus[,1],mx_fus[,2]) 0.4828185 Why the plot says a thing and correlation function says the opposite? Two near arrows don't represent a strong correlation between the 2 variables (as I read in some manuals), but only with the component axis? Than's in advance [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] 3D plots in R.3.2.3
For 3D scatterplot you can use also "scatterplot3d" package. 2016-05-19 8:40 GMT+02:00 David Winsemius : > > > On May 18, 2016, at 7:51 AM, ch.elahe via R-help > wrote: > > > > > > Hi all, > > I am using R version 3.2.3 and I want to plot 3D histogram or 3D > scatterplot. Does anyone know which packages can be used for this version > for 3D plots? I tried plot3d but it's not working for this version. > > Perhaps you =misspelled the package name. I see no plot3d package. (But > there is a plot3D package.) > > > -- > David Winsemius > Alameda, CA, USA > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] problem with matrix
Thank you very much, just what I needed!! 2016-05-16 18:25 GMT+02:00 Duncan Murdoch : > On 16/05/2016 12:10 PM, Denis Francisci wrote: > >> Hi all, >> I've a simple question. >> I have a matrix with same values over and under the diagonal. That's an >> example: >>[,1] [,2] [,3] >> [1,] NaN 45 63.43495 >> [2,] 45.0 NaN 90.0 >> [3,] 63.43495 90 NaN >> How can I extract just the three values over (or under) the diagonal and >> convert them in a vector like this: 45, 63.43495, 90 ? >> >> Thank's in advance >> >> > See ?upper.tri. > > Duncan Murdoch > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] problem with matrix
Hi all, I've a simple question. I have a matrix with same values over and under the diagonal. That's an example: [,1] [,2] [,3] [1,] NaN 45 63.43495 [2,] 45.0 NaN 90.0 [3,] 63.43495 90 NaN How can I extract just the three values over (or under) the diagonal and convert them in a vector like this: 45, 63.43495, 90 ? Thank's in advance [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] alignments in random points
Dear forum, I have a number of random point in a polygon. I would like to find those points lying on the same lines. Is there an R function to find alignments in points (something like this: https://en.wikipedia.org/wiki/Alignments_of_random_points)? And is it possible to do the same thing but changing the width of lines? If it could be useful, I attached an example of R code that I'm using. Thank's in advance, DF CODE: library(sp) poly<-matrix(c(16,17,25,22,16,58,55,55,61,58),ncol=2,nrow=5) > poly [,1] [,2] [1,] 16 58 [2,] 17 55 [3,] 25 55 [4,] 22 61 [5,] 16 58 p=Polygon(poly) ps=Polygons(list(p),1) sps=SpatialPolygons(list(ps)) p.rd=spsample(sps,n=150,"random") plot(sps) points(p.rd,pch=16, col='blue',cex=0.3) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Addressing Columns in a Data Frame
I don't know if I understood your problem, but maybe you can retrieve your columns simply in this way: > DF$y [1] 10 9 8 7 6 5 4 3 2 1 or (for looking at column) > DF[2] y 1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1 Bye D. 2013/2/9 Lorenzo Isella : > Dear All, > Probably a one liner, but I am banging my head against the floor. > Consider the following > > DF <- data.frame( >x=1:10, >y=10:1, >z=rep(5,10), >a=11:20 > ) > > mn<-names(DF) > > but then I cannot retrieve a column by doing e.g, > > DF$mn[2] > > I tried to play with the quotes and so on, but so far with no avail. > Any suggestion is welcome. > Cheers > > Lorenzo > > __ > 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 plot like candlestick
Wonderful! Thank you Eik! I thought there was a specific package or plot to get my purpose, but your idea solves perfectly my problem. Maybe I could try to write a simple R function using your suggest, just to make fast my work. If I can get a result, I'll announce it in this list. Thank you very much. Denis 2013/1/29 Eik Vettorazzi : > Hi Denis, > there is no "if", only "how" in R ;) > > how about this: > > rmail2<-read.table(textConnection("item, min, int_1, int_2, max > a, 2.5, 3, 4, 5.5 > b, 2, 3.5, 4, 4.5 > c, 3.5, 4, 4.5, 5"),header=T,sep=",") > with(rmail2,symbols(item, (int_1+ int_2)/2, boxplots=cbind(.25, > int_2-int_1, > int_1-min,max-int_2,0),inches=F,ylim=range(rmail2[,-1]),xaxt="n",ylab="")) > axis(1,seq_along(rmail2$item),labels=rmail2$item) > > hth. > > Am 28.01.2013 22:26, schrieb Denis Francisci: >> Hi all, >> I'm new on this list so I greet all. >> My question is: does exist in R a plot similar to candlestick plot but >> not based on xts (time series)? I have to plot a range of 4 value: for >> every item I have min value, max value and 2 intermediate values. I >> would like plot this like a candlestick, i.e. with a box between 2 >> intermediate values and 1 segment between box and min value and a >> segment between box and max value. Candlestick plot is provided by >> quantmod package, but it is very specific for financial purpose and it >> uses time series for x axis. I need a simpler method for plotting my >> data that are stored in a table like this: >> >> item, min, int_1, int_2, max >> a, 2.5, 3, 4, 5.5 >> b, 2, 3.5, 4, 4.5 >> c, 3.5, 4, 4.5, 5 >> . >> >> Does anyone know if there is an R-plot for this purpose? >> Thank you very much, >> >> D. >> >> __ >> 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. >> > > > -- > Eik Vettorazzi > > Department of Medical Biometry and Epidemiology > University Medical Center Hamburg-Eppendorf > > Martinistr. 52 > 20246 Hamburg > > T ++49/40/7410-58243 > F ++49/40/7410-57790 > -- > Pflichtangaben gemäß Gesetz über elektronische Handelsregister und > Genossenschaftsregister sowie das Unternehmensregister (EHUG): > > Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; > Gerichtsstand: Hamburg > > Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander > Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus > __ 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 plot like candlestick
Hi all, I'm new on this list so I greet all. My question is: does exist in R a plot similar to candlestick plot but not based on xts (time series)? I have to plot a range of 4 value: for every item I have min value, max value and 2 intermediate values. I would like plot this like a candlestick, i.e. with a box between 2 intermediate values and 1 segment between box and min value and a segment between box and max value. Candlestick plot is provided by quantmod package, but it is very specific for financial purpose and it uses time series for x axis. I need a simpler method for plotting my data that are stored in a table like this: item, min, int_1, int_2, max a, 2.5, 3, 4, 5.5 b, 2, 3.5, 4, 4.5 c, 3.5, 4, 4.5, 5 . Does anyone know if there is an R-plot for this purpose? Thank you very much, D. __ 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.