Re: [R] Speed up studentized confidence intervals ?

2021-12-29 Thread Fox, John
Dear varin sacha,

You didn't correctly adapt the code to the median. The outer call to mean() in 
the last line shouldn't be replaced with median() -- it computes the proportion 
of intervals that include the population median.

As well, you can't rely on the asymptotics of the bootstrap for a nonlinear 
statistic like the median with an n as small as 5, as your example, properly 
implemented (and with the code slightly cleaned up), illustrates:

> library(boot)
> set.seed(123)
> s <- rgamma(n=10, shape=2, rate=5)
> (m <- median(s))
[1] 0.3364465
> N <- 1000
> n <- 5
> set.seed(321)
> out <- replicate(N, {
+   dat <- data.frame(sample(s, size=n))
+   med <- function(d, i) {
+ median(d[i, ])
+   }
+   boot.out <- boot(data = dat, statistic = med, R = 1)
+   boot.ci(boot.out, type = "bca")$bca[, 4:5]
+ })
> #coverage probability
> mean(out[1, ] < m & m < out[2, ])
[1] 0.758


You do get the expected coverage, however, for a larger sample, here with n = 
100:

> N <- 1000
> n <- 100
> set.seed(321)
> out <- replicate(N, {
+   dat <- data.frame(sample(s, size=n))
+   med <- function(d, i) {
+ median(d[i, ])
+   }
+   boot.out <- boot(data = dat, statistic = med, R = 1)
+   boot.ci(boot.out, type = "bca")$bca[, 4:5]
+ })
> #coverage probability
> mean(out[1, ] < m & m < out[2, ])
[1] 0.952

I hope this helps,
 John

-- 
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: http://socserv.mcmaster.ca/jfox/
 
 


On 2021-12-29, 2:09 PM, "R-help on behalf of varin sacha via R-help" 
 wrote:

Dear David,
Dear Rui,

Many thanks for your response. It perfectly works for the mean. Now I have 
a problem with my R code for the median. Because I always get 1 (100%) coverage 
probability that is more than very strange. Indeed, considering that an 
interval whose lower limit is the smallest value in the sample and whose upper 
limit is the largest value has 1/32 + 1/32 = 1/16 probability of non-coverage, 
implying that the confidence of such an interval is 15/16 rather than 1 (100%), 
I suspect that the confidence interval I use for the median is not correctly 
defined for n=5 observations, and likely contains all observations in the 
sample ? What is wrong with my R code ?


library(boot)

s=rgamma(n=10,shape=2,rate=5)
median(s)

N <- 100
out <- replicate(N, {
a<- sample(s,size=5)
median(a) 

dat<-data.frame(a)
med<-function(d,i) {
temp<-d[i,]
median(temp)
}

  boot.out <- boot(data = dat, statistic = med, R = 1)
  boot.ci(boot.out, type = "bca")$bca[, 4:5]
})

#coverage probability
median(out[1, ] < median(s) & median(s) < out[2, ])





Le jeudi 23 décembre 2021, 14:10:36 UTC+1, Rui Barradas 
 a écrit : 





Hello,

The code is running very slowly because you are recreating the function 
in the replicate() loop and because you are creating a data.frame also 
in the loop.

And because in the bootstrap statistic function med() you are computing 
the variance of yet another loop. This is probably statistically wrong 
but like David says, without a problem description it's hard to say.

Also, why compute variances if they are never used?

Here is complete code executing in much less than 2:00 hours. Note that 
it passes the vector a directly to med(), not a df with just one column.


library(boot)

set.seed(2021)
s <- sample(178:798, 10, replace = TRUE)
mean(s)

med <- function(d, i) {
  temp <- d[i]
  f <- mean(temp)
  g <- var(temp)
  c(Mean = f, Var = g)
}

N <- 1000
out <- replicate(N, {
  a <- sample(s, size = 5)
  boot.out <- boot(data = a, statistic = med, R = 1)
  boot.ci(boot.out, type = "stud")$stud[, 4:5]
})
mean(out[1, ] < mean(s) & mean(s) < out[2, ])
#[1] 0.952



Hope this helps,

Rui Barradas

Às 11:45 de 19/12/21, varin sacha via R-help escreveu:
> Dear R-experts,
> 
> Here below my R code working but really really slowly ! I need 2 hours 
with my computer to finally get an answer ! Is there a way to improve my R code 
to speed it up ? At least to win 1 hour ;=)
> 
> Many thanks
> 
> 
> library(boot)
> 
> s<- sample(178:798, 10, replace=TRUE)
> mean(s)
> 
> N <- 1000
> out <- replicate(N, {
> a<- sample(s,size=5)
> mean(a)
> dat<-data.frame(a)
> 
> med<-function(d,i) {
> temp<-d[i,]
> f<-mean(temp)
> g<-var(replicate(50,mean(sample(temp,replace=T
> return(c(f,g))
> 
> }
> 
>boot.out <- boot(data = dat, statistic = med, R = 1)
>boot.ci(boot.out, type = "stud")$stud[, 4:5]
> })
> mean(out[1,] < mean(s) & mean(s) < out[2,])
> 

Re: [R-es] raspado web desde Shiny

2021-12-29 Thread Diego Maldonado via R-help-es
 Gracias lo voy a revisar pero no se si se pueda aplicar a páginas dinámicas 
con Java Script
 En miércoles, 29 de diciembre de 2021 08:03:57 GMT-5, Emilio L. Cano 
 escribió:  
 
 Hola Diego,
No sé si es exactamente lo que buscas, pero yo probaría el paquete rvest. En 
este artículo lo combinan con shiny, pero no sé si es eso lo que quieres 
https://towardsdatascience.com/scrape-data-and-build-a-webapp-in-r-using-rvest-and-shiny-f20d84dd1b74
Emilio



El 29 dic 2021, a las 2:38, Diego Maldonado via R-help-es 
 escribió:
Saludos comunidad por consultarles si alguien a realizado un raspado web en 
paginas dinámicas desde Shiny app en la nube. He utilizado el paquete crrri 
pero solo me funciona en Shiny de manera local y no lo logro desde la nube. 

Si alguien me podría indicar donde puedo encontrar mayor documentación para 
poder hacer raspado web con Shiny desde la nube les agradecería.

Saludos
Diego M.

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


  
[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R-es] raspado web desde Shiny

2021-12-29 Thread Diego Maldonado via R-help-es
 Gracias Javier por tu consejo
 En miércoles, 29 de diciembre de 2021 15:41:09 GMT-5, Javier Marcuzzi 
 escribió:  
 
 Estimado Diego Maldonado
Tal como dice Emiliano Cano, posiblemente rvest, pero usted debe tener en 
cuenta que el gasto en tiempo es importante, las personas están acostumbradas a 
que el navegador de internet de una respuesta rápida, pueden pensar que hay 
problemas y se van cuándo no lo hay. Por otro lado, rvest no tiene, salvo un 
cambio que yo desconozco, una forma para javascript, hay otras alternativas que 
tienen conección a un navegador, pero este se abre y busca cada página, con la 
descarga de imágenes y etc., lo que se traduce en tiempo. O si el sitio es 
moderno y utiliza web ensambly, otra posibilidad que usted debe contemplar.
Su desafío en R es muy grande. 
Javier Rubén Marcuzzi
El mar, 28 dic 2021 a las 22:40, Diego Maldonado via R-help-es 
() escribió:

Saludos comunidad por consultarles si alguien a realizado un raspado web en 
paginas dinámicas desde Shiny app en la nube. He utilizado el paquete crrri 
pero solo me funciona en Shiny de manera local y no lo logro desde la nube. 

Si alguien me podría indicar donde puedo encontrar mayor documentación para 
poder hacer raspado web con Shiny desde la nube les agradecería.

Saludos
Diego M.

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es

  
[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


Re: [R] Speed up studentized confidence intervals ?

2021-12-29 Thread David Winsemius



On 12/29/21 11:08 AM, varin sacha via R-help wrote:

Dear David,
Dear Rui,

Many thanks for your response. It perfectly works for the mean. Now I have a 
problem with my R code for the median. Because I always get 1 (100%) coverage 
probability that is more than very strange. Indeed, considering that an 
interval whose lower limit is the smallest value in the sample and whose upper 
limit is the largest value has 1/32 + 1/32 = 1/16 probability of non-coverage, 
implying that the confidence of such an interval is 15/16 rather than 1 (100%), 
I suspect that the confidence interval I use for the median is not correctly 
defined for n=5 observations, and likely contains all observations in the 
sample ? What is wrong with my R code ?



Seems to me that doing  a bootstrap within a `replicate` call is not 
needed. (Use one or the other as a mechanism for replication.


Here's what I would consider to be a "bootstrap" operation for 
estimating a 95% CI on the Gamma distributed population you created:


Used a sample size of 1 rather than 10


> quantile( replicate( 1000, {median(sample(s,5))}) , .5+c(-0.475,0.475))
 2.5% 97.5%
0.1343071 0.6848352

This is using boot::boot to calculate medians of samples of size 5

> med <- function( data, indices) {
+ d <- data[indices[1:5]] # allows boot to select sample
+ return( median(d))
+ }
> res <- boot(data=s, med, 1000)

> str(res)
List of 11
 $ t0   : num 0.275
 $ t    : num [1:1000, 1] 0.501 0.152 0.222 0.11 0.444 ...
 $ R    : num 1000
 $ data : num [1:1] 0.7304 0.4062 0.1901 0.0275 0.2748 ...
 $ seed : int [1:626] 10403 431 -118115842 -603122380 -2026881868 
758139796 1148648893 -1161368223 1814605964 -1456558535 ...

 $ statistic:function (data, indices)
  ..- attr(*, "srcref")= 'srcref' int [1:8] 1 8 4 1 8 1 1 4
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' 


 $ sim  : chr "ordinary"
 $ call : language boot(data = s, statistic = med, R = 1000)
 $ stype    : chr "i"
 $ strata   : num [1:1] 1 1 1 1 1 1 1 1 1 1 ...
 $ weights  : num [1:1] 1e-04 1e-04 1e-04 1e-04 1e-04 1e-04 1e-04 
1e-04 1e-04 1e-04 ...

 - attr(*, "class")= chr "boot"
 - attr(*, "boot_type")= chr "boot"

> quantile( res$t , .5+c(-0.475,0.475))
 2.5% 97.5%
0.1283309 0.6821874






library(boot)

s=rgamma(n=10,shape=2,rate=5)
median(s)

N <- 100
out <- replicate(N, {
a<- sample(s,size=5)
median(a)

dat<-data.frame(a)
med<-function(d,i) {
temp<-d[i,]
median(temp)
}

   boot.out <- boot(data = dat, statistic = med, R = 1)
   boot.ci(boot.out, type = "bca")$bca[, 4:5]
})

#coverage probability
median(out[1, ] < median(s) & median(s) < out[2, ])





Le jeudi 23 décembre 2021, 14:10:36 UTC+1, Rui Barradas  
a écrit :





Hello,

The code is running very slowly because you are recreating the function
in the replicate() loop and because you are creating a data.frame also
in the loop.

And because in the bootstrap statistic function med() you are computing
the variance of yet another loop. This is probably statistically wrong
but like David says, without a problem description it's hard to say.

Also, why compute variances if they are never used?

Here is complete code executing in much less than 2:00 hours. Note that
it passes the vector a directly to med(), not a df with just one column.


library(boot)

set.seed(2021)
s <- sample(178:798, 10, replace = TRUE)
mean(s)

med <- function(d, i) {
   temp <- d[i]
   f <- mean(temp)
   g <- var(temp)
   c(Mean = f, Var = g)
}

N <- 1000
out <- replicate(N, {
   a <- sample(s, size = 5)
   boot.out <- boot(data = a, statistic = med, R = 1)
   boot.ci(boot.out, type = "stud")$stud[, 4:5]
})
mean(out[1, ] < mean(s) & mean(s) < out[2, ])
#[1] 0.952



Hope this helps,

Rui Barradas

Às 11:45 de 19/12/21, varin sacha via R-help escreveu:

Dear R-experts,

Here below my R code working but really really slowly ! I need 2 hours with my 
computer to finally get an answer ! Is there a way to improve my R code to 
speed it up ? At least to win 1 hour ;=)

Many thanks


library(boot)

s<- sample(178:798, 10, replace=TRUE)
mean(s)

N <- 1000
out <- replicate(N, {
a<- sample(s,size=5)
mean(a)
dat<-data.frame(a)

med<-function(d,i) {
temp<-d[i,]
f<-mean(temp)
g<-var(replicate(50,mean(sample(temp,replace=T
return(c(f,g))

}

     boot.out <- boot(data = dat, statistic = med, R = 1)
     boot.ci(boot.out, type = "stud")$stud[, 4:5]
})
mean(out[1,] < mean(s) & mean(s) < out[2,])


__
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 

Re: [R] Mapping of groups of countries

2021-12-29 Thread Jim Lemon
Hi George,
This example is obviously a mess, but it shows the basic elements. The
labels may be placed manually using something like boxed.labels
(plotrix) with lines connecting the labels to the smaller countries.
If you want the labels within the countries you will have to use a big
graphics device.

library(maps)
map("world")
countries=c("China","Bangladesh","Egypt","Indonesia","South Korea","Nigeria",
 "Brazil","Pakistan","Philippines","Vietnam","South Africa",
 "India","Mexico","Turkey")
countrycol<-c(rep("red",6),rep("blue",4),"green",rep("orange",3))
for(i in 1:length(countries)) {
 mapinf<-map(region=countries[i],col=countrycol[i],add=TRUE)
 xpos<-sum(mapinf$range[1:2])/2
 ypos<-sum(mapinf$range[3:4])/2
 text(xpos,ypos,countries[i])
}


Jim

On Thu, Dec 30, 2021 at 7:34 AM george brida  wrote:
>
> Dear R users,
>
> I would like to map with R the following countries (with their names on the
> map) as follows: the first group in red composed by China, Bangladesh,
> Eypt, Indonesia, South Korea, Nigeria. The second group in blue composed by
> Brazil, Pakistan, Philippines, Vietnam.
>
> The third group in green solely composed by South Africa.
>
> Finally, the fourth group in orange composed by India, Mexico and Turkey.
>
> I'd also like a legend that assigns the different colors to the various
> group.
> I tried several times with library maps but it seems that I failed to find
> the real setting of xlim and ylim. Can you please help me, I will be very
> grateful.
>
> Best regards
> George
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R-es] raspado web desde Shiny

2021-12-29 Thread Javier Marcuzzi
Estimado Diego Maldonado

Tal como dice Emiliano Cano, posiblemente rvest, pero usted debe tener en
cuenta que el gasto en tiempo es importante, las personas están
acostumbradas a que el navegador de internet de una respuesta rápida,
pueden pensar que hay problemas y se van cuándo no lo hay. Por otro lado,
rvest no tiene, salvo un cambio que yo desconozco, una forma para
javascript, hay otras alternativas que tienen conección a un navegador,
pero este se abre y busca cada página, con la descarga de imágenes y etc.,
lo que se traduce en tiempo. O si el sitio es moderno y utiliza web
ensambly, otra posibilidad que usted debe contemplar.

Su desafío en R es muy grande.

Javier Rubén Marcuzzi

El mar, 28 dic 2021 a las 22:40, Diego Maldonado via R-help-es (<
r-help-es@r-project.org>) escribió:

> Saludos comunidad por consultarles si alguien a realizado un raspado web
> en paginas dinámicas desde Shiny app en la nube. He utilizado el paquete
> crrri pero solo me funciona en Shiny de manera local y no lo logro desde la
> nube.
>
> Si alguien me podría indicar donde puedo encontrar mayor documentación
> para poder hacer raspado web con Shiny desde la nube les agradecería.
>
> Saludos
> Diego M.
>
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es
>

[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


[R] Mapping of groups of countries

2021-12-29 Thread george brida
Dear R users,

I would like to map with R the following countries (with their names on the
map) as follows: the first group in red composed by China, Bangladesh,
Eypt, Indonesia, South Korea, Nigeria. The second group in blue composed by
Brazil, Pakistan, Philippines, Vietnam.

The third group in green solely composed by South Africa.

Finally, the fourth group in orange composed by India, Mexico and Turkey.

I'd also like a legend that assigns the different colors to the various
group.
I tried several times with library maps but it seems that I failed to find
the real setting of xlim and ylim. Can you please help me, I will be very
grateful.

Best regards
George

[[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] Speed up studentized confidence intervals ?

2021-12-29 Thread varin sacha via R-help
Dear David,
Dear Rui,

Many thanks for your response. It perfectly works for the mean. Now I have a 
problem with my R code for the median. Because I always get 1 (100%) coverage 
probability that is more than very strange. Indeed, considering that an 
interval whose lower limit is the smallest value in the sample and whose upper 
limit is the largest value has 1/32 + 1/32 = 1/16 probability of non-coverage, 
implying that the confidence of such an interval is 15/16 rather than 1 (100%), 
I suspect that the confidence interval I use for the median is not correctly 
defined for n=5 observations, and likely contains all observations in the 
sample ? What is wrong with my R code ?


library(boot)

s=rgamma(n=10,shape=2,rate=5)
median(s)

N <- 100
out <- replicate(N, {
a<- sample(s,size=5)
median(a) 

dat<-data.frame(a)
med<-function(d,i) {
temp<-d[i,]
median(temp)
}

  boot.out <- boot(data = dat, statistic = med, R = 1)
  boot.ci(boot.out, type = "bca")$bca[, 4:5]
})

#coverage probability
median(out[1, ] < median(s) & median(s) < out[2, ])





Le jeudi 23 décembre 2021, 14:10:36 UTC+1, Rui Barradas  
a écrit : 





Hello,

The code is running very slowly because you are recreating the function 
in the replicate() loop and because you are creating a data.frame also 
in the loop.

And because in the bootstrap statistic function med() you are computing 
the variance of yet another loop. This is probably statistically wrong 
but like David says, without a problem description it's hard to say.

Also, why compute variances if they are never used?

Here is complete code executing in much less than 2:00 hours. Note that 
it passes the vector a directly to med(), not a df with just one column.


library(boot)

set.seed(2021)
s <- sample(178:798, 10, replace = TRUE)
mean(s)

med <- function(d, i) {
  temp <- d[i]
  f <- mean(temp)
  g <- var(temp)
  c(Mean = f, Var = g)
}

N <- 1000
out <- replicate(N, {
  a <- sample(s, size = 5)
  boot.out <- boot(data = a, statistic = med, R = 1)
  boot.ci(boot.out, type = "stud")$stud[, 4:5]
})
mean(out[1, ] < mean(s) & mean(s) < out[2, ])
#[1] 0.952



Hope this helps,

Rui Barradas

Às 11:45 de 19/12/21, varin sacha via R-help escreveu:
> Dear R-experts,
> 
> Here below my R code working but really really slowly ! I need 2 hours with 
> my computer to finally get an answer ! Is there a way to improve my R code to 
> speed it up ? At least to win 1 hour ;=)
> 
> Many thanks
> 
> 
> library(boot)
> 
> s<- sample(178:798, 10, replace=TRUE)
> mean(s)
> 
> N <- 1000
> out <- replicate(N, {
> a<- sample(s,size=5)
> mean(a)
> dat<-data.frame(a)
> 
> med<-function(d,i) {
> temp<-d[i,]
> f<-mean(temp)
> g<-var(replicate(50,mean(sample(temp,replace=T
> return(c(f,g))
> 
> }
> 
>    boot.out <- boot(data = dat, statistic = med, R = 1)
>    boot.ci(boot.out, type = "stud")$stud[, 4:5]
> })
> mean(out[1,] < mean(s) & mean(s) < out[2,])
> 
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] mixture univariate distributions fit

2021-12-29 Thread Bert Gunter
No.

However, if the object returned is the "Value" structure of whatever
density function you use, it probably contains the original data. You
need to check the docs to see. But this does not appear to be your
situation.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Wed, Dec 29, 2021 at 3:05 AM PIKAL Petr  wrote:
>
> Dear all
>
> I have data which are either density distribution estimate or cummulative
> density distribution estimate (temp1, temp2 below). I would like to get
> values (mu, sd) for underlaying original data but they are not available.
>
> I found mixtools package which calculate what I need but it requires
> original data (AFAIK). They could be generated from e.g. temp1 by
>
> set.seed(111)
> x<- sample(temp1$velik, size=10, replace=TRUE, prob=temp1$proc)
>
> library(mixtools)
> fit <- normalmixEM(x)
> plot(fit, which=2)
> summary(fit)
> summary of normalmixEM object:
>comp 1 comp 2
> lambda   0.576346   0.423654
> mu 170.784520 229.192823
> sigma7.203491  10.793461
> loglik at estimate:  -424062.7
> >
>
> Is there any way how to get such values directly from density or cummulative
> density estimation without generating fake data by sample?
>
> Best regards
> Petr
>
>
> temp1 <- structure(list(velik = c(155, 156.8, 157.9, 158.8, 159.6, 160.4,
> 161.2, 161.9, 162.5, 163.1, 163.8, 164.3, 164.7, 165.3, 165.8,
> 166.2, 166.7, 167.2, 167.7, 168.2, 168.7, 169.1, 169.6, 170.1,
> 170.6, 171.1, 171.6, 172, 172.5, 173, 173.5, 174, 174.5, 175.1,
> 175.7, 176.3, 177, 177.6, 178.3, 179.1, 179.9, 180.6, 181.4,
> 182.4, 183.5, 184.7, 186.1, 187.9, 189.8, 192, 194.4, 197, 200.1,
> 203.5, 206.7, 209.2, 211.3, 213.1, 214.8, 216.3, 217.4, 218.5,
> 219.5, 220.4, 221.3, 222.1, 223, 223.7, 224.5, 225.2, 225.9,
> 226.7, 227.5, 228.2, 228.9, 229.6, 230.4, 231.2, 231.9, 232.6,
> 233.4, 234.2, 235, 235.9, 236.8, 237.7, 238.6, 239.7, 241, 242.3,
> 243.6, 245.2, 247.1, 249.3, 251.9, 255.3, 260, 266, 274.9, 323.4
> ), proc = c(0.6171, 1.583, 1.371, 2.13, 1.828, 2.095, 1.994,
> 2.694, 2.824, 2.41, 2.909, 3.768, 3.179, 3.029, 3.798, 3.743,
> 3.276, 3.213, 3.579, 2.928, 4.634, 3.415, 3.473, 3.135, 3.476,
> 3.759, 3.726, 3.9, 3.593, 2.89, 3.707, 4.08, 2.846, 2.685, 3.394,
> 2.737, 2.693, 2.878, 2.248, 2.368, 2.258, 2.662, 1.866, 1.895,
> 1.457, 1.513, 1.181, 1.008, 0.9641, 0.799, 0.7878, 0.7209, 0.5869,
> 0.5778, 0.7313, 0.9531, 1.053, 1.317, 1.247, 1.739, 2.064, 1.99,
> 2.522, 2.401, 2.48, 2.687, 2.797, 2.918, 3.243, 3.055, 3.009,
> 2.89, 3.037, 3.25, 3.349, 3.141, 2.771, 2.985, 3.203, 3.298,
> 3.215, 2.637, 2.683, 2.782, 2.632, 2.625, 2.475, 2.014, 1.781,
> 1.987, 1.627, 1.374, 1.352, 0.9441, 1.01, 0.5737, 0.5265, 0.3794,
> 0.2513, 0.0351)), row.names = 2:101, class = "data.frame")
>
> temp2 <- structure(list(velik = c(153.8, 156.3, 157.3, 158.4, 159.2, 160.1,
> 160.8, 161.6, 162.2, 162.8, 163.5, 164, 164.5, 165, 165.5, 166,
> 166.4, 166.9, 167.5, 167.9, 168.5, 168.9, 169.4, 169.8, 170.4,
> 170.9, 171.3, 171.8, 172.2, 172.7, 173.3, 173.8, 174.2, 174.8,
> 175.5, 176, 176.6, 177.3, 177.9, 178.7, 179.5, 180.3, 180.9,
> 181.9, 182.9, 184.1, 185.3, 186.9, 188.8, 190.8, 193.2, 195.6,
> 198.4, 201.8, 205.3, 208.1, 210.3, 212.3, 213.9, 215.7, 216.9,
> 218, 219.1, 219.9, 220.8, 221.7, 222.6, 223.4, 224.1, 224.8,
> 225.6, 226.3, 227.1, 227.8, 228.5, 229.2, 230, 230.8, 231.6,
> 232.3, 233, 233.7, 234.6, 235.5, 236.3, 237.2, 238.1, 239.1,
> 240.3, 241.6, 242.9, 244.4, 246.1, 248, 250.6, 253.1, 257.6,
> 262.5, 269.5, 280.4, 372.9), proc = c(0, 1, 2, 3, 4, 5, 6, 7,
> 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
> 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
> 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
> 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
> 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
> 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)), row.names = c(NA,
> 101L), class = "data.frame")
> >
>
> >
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to modify object's code living in some environment?

2021-12-29 Thread Duncan Murdoch

On 29/12/2021 9:29 a.m., Grzegorz Smoliński wrote:

Thank you. I posted this question on SO in the meantime as well, but
there is no answer for my question, so I pasted your answer
(https://stackoverflow.com/questions/70517625/how-to-use-trace-for-function-in-parent-or-child-environment).
Hope it is fine for you.


No problem, though I'd suggest posting the answer as an answer, rather 
than an edit to your question.


Duncan Murdoch



śr., 29 gru 2021 o 11:24 Duncan Murdoch  napisał(a):


On 28/12/2021 4:21 p.m., Grzegorz Smoliński wrote:

Thank you for all the comments. If this is not a problem, I would like
to continue this thread as for me a lot of questions are still open.
But if I should post other questions in different messages / topics,
please let me know.

I didn’t answer Ivan’s question previously, but yes, my desired result
is indeed what debug() / debugonce() does and Ivan mentioned trace()
earlier also, so I have tried this. I thought that trace() will be
better in my case (than debug()) since I would like to be able to call
trace() from one environment and insert browser() (using trace()) to
the function defined in parent environment (still not sure if I
understand this concept of environments).

Even if I would like to use it in an shiny app, I have started with
this example:

--

fun1 <- function() {
a <-  1
a
}

fun2 <- function() {
fun1 <- function(){
  b <- 2
  b
}
trace(fun1, browser, where = globalenv())
}

fun1()
fun2()
fun1()



So I was hoping to use trace inside child environment of global env,
i.e. set browser() using trace() to the body of function defined in
parent environment, but for sure I do not understand this since
trace() in my example above is putting the browser() function inside
fun1() function defined not in global environment, but in the body of
fun2(). I.e. I'm starting to browse the fun1() function with the local
variable 'b', not 'a' as I wanted.


You are being bitten by normal R scoping.  While fun2 is executing, as
soon as it executes the fun1 assignment the name fun1 is bound to the
local function, so when you use fun1 in the first argument to trace(),
you get that one.  It will ignore then "where" argument because it was
passed a function object.  If you quote the name it will have to do a
lookup and it will use "where".  So write fun2 like this:

fun2 <- function() {
fun1 <- function(){
  b <- 2
  b
}
trace("fun1", browser, where = globalenv())
}




Can I ask for help with this? In other words, how can I refer to some
function from the parent environment (not necessarily defined in the
global environment) being in the child environment?


You could have used parent.frame() in place of globalenv() if you want a
function that was visible to the caller of fun2.  If you want a function
that was defined with the same environment as fun2, you can usually use
environment(fun2), or parent.env(environment()).




Also, if I could ask - is the reversed operation possible? I mean -
can I refer to some function from the child environment being in the
parent environment? I guess not, because of ephemerality?


Sometimes you can, but not usually.  If fun2 had returned a value that
referenced the evaluation frame, you would be able to see the "b"
version of fun1 there.  But yours didn't.  This one would:


fun2 <- function() {
fun1 <- function(){
  b <- 2
  b
}
trace("fun1", browser, where = globalenv())
environment()
}

Now if you do e <- fun2(), you'll set the trace on the global fun1, but
e$fun1 will be the local one.

I don't really know how Shiny sets things up, so I can't help with the
stuff below.

Duncan Murdoch




As I mentioned, I'm thinking about all of this in the context of shiny
app and the shiny app which consists of multiple files, modules. I'm
thinking if I could refer to any function from any environment (or at
least to any function in any parent environment) from one place (one,
current environment) where I will call debug() or trace() to insert
browser() at the beginning of this chosen function. And by "any
function" I'm thinking about another thing problematic for me. If I
consider this example:

---
mod.R inside R/
---

mod_UI <- function(id) {
}

name_mod <- function(input, output, session) {
fun1 <- reactive({
  a  <-  1
})
}

---
app.R
---

library(shiny)

ui <- fluidPage(
mod_UI("mod"),
textOutput("env")
)

server <- function(input, output, session) {

name_mod("mod")

output$env <- renderPrint({
  names(environment(name_mod))
})

observe({
  #trace(fun1, browser, where = environment(name_mod))
})
}

shinyApp(ui, server)
---
I tried to refer to the fun1 in environment(name_mod) - if I'm not
wrong, this is my parent environment, where objects "mod_UI" and
"name_mod" exist, but obviously I'm doing this 

Re: [R] How to modify object's code living in some environment?

2021-12-29 Thread Grzegorz Smoliński
Thank you. I posted this question on SO in the meantime as well, but
there is no answer for my question, so I pasted your answer
(https://stackoverflow.com/questions/70517625/how-to-use-trace-for-function-in-parent-or-child-environment).
Hope it is fine for you.

śr., 29 gru 2021 o 11:24 Duncan Murdoch  napisał(a):
>
> On 28/12/2021 4:21 p.m., Grzegorz Smoliński wrote:
> > Thank you for all the comments. If this is not a problem, I would like
> > to continue this thread as for me a lot of questions are still open.
> > But if I should post other questions in different messages / topics,
> > please let me know.
> >
> > I didn’t answer Ivan’s question previously, but yes, my desired result
> > is indeed what debug() / debugonce() does and Ivan mentioned trace()
> > earlier also, so I have tried this. I thought that trace() will be
> > better in my case (than debug()) since I would like to be able to call
> > trace() from one environment and insert browser() (using trace()) to
> > the function defined in parent environment (still not sure if I
> > understand this concept of environments).
> >
> > Even if I would like to use it in an shiny app, I have started with
> > this example:
> >
> > --
> >
> > fun1 <- function() {
> >a <-  1
> >a
> > }
> >
> > fun2 <- function() {
> >fun1 <- function(){
> >  b <- 2
> >  b
> >}
> >trace(fun1, browser, where = globalenv())
> > }
> >
> > fun1()
> > fun2()
> > fun1()
> >
> > 
> >
> > So I was hoping to use trace inside child environment of global env,
> > i.e. set browser() using trace() to the body of function defined in
> > parent environment, but for sure I do not understand this since
> > trace() in my example above is putting the browser() function inside
> > fun1() function defined not in global environment, but in the body of
> > fun2(). I.e. I'm starting to browse the fun1() function with the local
> > variable 'b', not 'a' as I wanted.
>
> You are being bitten by normal R scoping.  While fun2 is executing, as
> soon as it executes the fun1 assignment the name fun1 is bound to the
> local function, so when you use fun1 in the first argument to trace(),
> you get that one.  It will ignore then "where" argument because it was
> passed a function object.  If you quote the name it will have to do a
> lookup and it will use "where".  So write fun2 like this:
>
> fun2 <- function() {
>fun1 <- function(){
>  b <- 2
>  b
>}
>trace("fun1", browser, where = globalenv())
> }
>
>
> >
> > Can I ask for help with this? In other words, how can I refer to some
> > function from the parent environment (not necessarily defined in the
> > global environment) being in the child environment?
>
> You could have used parent.frame() in place of globalenv() if you want a
> function that was visible to the caller of fun2.  If you want a function
> that was defined with the same environment as fun2, you can usually use
> environment(fun2), or parent.env(environment()).
>
>
> >
> > Also, if I could ask - is the reversed operation possible? I mean -
> > can I refer to some function from the child environment being in the
> > parent environment? I guess not, because of ephemerality?
>
> Sometimes you can, but not usually.  If fun2 had returned a value that
> referenced the evaluation frame, you would be able to see the "b"
> version of fun1 there.  But yours didn't.  This one would:
>
>
> fun2 <- function() {
>fun1 <- function(){
>  b <- 2
>  b
>}
>trace("fun1", browser, where = globalenv())
>environment()
> }
>
> Now if you do e <- fun2(), you'll set the trace on the global fun1, but
> e$fun1 will be the local one.
>
> I don't really know how Shiny sets things up, so I can't help with the
> stuff below.
>
> Duncan Murdoch
>
>
> >
> > As I mentioned, I'm thinking about all of this in the context of shiny
> > app and the shiny app which consists of multiple files, modules. I'm
> > thinking if I could refer to any function from any environment (or at
> > least to any function in any parent environment) from one place (one,
> > current environment) where I will call debug() or trace() to insert
> > browser() at the beginning of this chosen function. And by "any
> > function" I'm thinking about another thing problematic for me. If I
> > consider this example:
> >
> > ---
> > mod.R inside R/
> > ---
> >
> > mod_UI <- function(id) {
> > }
> >
> > name_mod <- function(input, output, session) {
> >fun1 <- reactive({
> >  a  <-  1
> >})
> > }
> >
> > ---
> > app.R
> > ---
> >
> > library(shiny)
> >
> > ui <- fluidPage(
> >mod_UI("mod"),
> >textOutput("env")
> > )
> >
> > server <- function(input, output, session) {
> >
> >name_mod("mod")
> >
> >output$env <- renderPrint({
> >  names(environment(name_mod))
> >})
> >
> >observe({
> >  #trace(fun1, browser, where = environment(name_mod))
> >})
> > }
> >

Re: [R-es] raspado web desde Shiny

2021-12-29 Thread Emilio L. Cano
Hola Diego,

No sé si es exactamente lo que buscas, pero yo probaría el paquete rvest. En 
este artículo lo combinan con shiny, pero no sé si es eso lo que quieres 
https://towardsdatascience.com/scrape-data-and-build-a-webapp-in-r-using-rvest-and-shiny-f20d84dd1b74
 


Emilio



> El 29 dic 2021, a las 2:38, Diego Maldonado via R-help-es 
>  escribió:
> 
> Saludos comunidad por consultarles si alguien a realizado un raspado web en 
> paginas dinámicas desde Shiny app en la nube. He utilizado el paquete crrri 
> pero solo me funciona en Shiny de manera local y no lo logro desde la nube. 
> 
> Si alguien me podría indicar donde puedo encontrar mayor documentación para 
> poder hacer raspado web con Shiny desde la nube les agradecería.
> 
> Saludos
> Diego M.
> 
> ___
> R-help-es mailing list
> R-help-es@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es


[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


[R] mixture univariate distributions fit

2021-12-29 Thread PIKAL Petr
Dear all

I have data which are either density distribution estimate or cummulative
density distribution estimate (temp1, temp2 below). I would like to get
values (mu, sd) for underlaying original data but they are not available.

I found mixtools package which calculate what I need but it requires
original data (AFAIK). They could be generated from e.g. temp1 by

set.seed(111)
x<- sample(temp1$velik, size=10, replace=TRUE, prob=temp1$proc)

library(mixtools)
fit <- normalmixEM(x)
plot(fit, which=2)
summary(fit)
summary of normalmixEM object:
   comp 1 comp 2
lambda   0.576346   0.423654
mu 170.784520 229.192823
sigma7.203491  10.793461
loglik at estimate:  -424062.7 
>

Is there any way how to get such values directly from density or cummulative
density estimation without generating fake data by sample? 

Best regards
Petr


temp1 <- structure(list(velik = c(155, 156.8, 157.9, 158.8, 159.6, 160.4, 
161.2, 161.9, 162.5, 163.1, 163.8, 164.3, 164.7, 165.3, 165.8, 
166.2, 166.7, 167.2, 167.7, 168.2, 168.7, 169.1, 169.6, 170.1, 
170.6, 171.1, 171.6, 172, 172.5, 173, 173.5, 174, 174.5, 175.1, 
175.7, 176.3, 177, 177.6, 178.3, 179.1, 179.9, 180.6, 181.4, 
182.4, 183.5, 184.7, 186.1, 187.9, 189.8, 192, 194.4, 197, 200.1, 
203.5, 206.7, 209.2, 211.3, 213.1, 214.8, 216.3, 217.4, 218.5, 
219.5, 220.4, 221.3, 222.1, 223, 223.7, 224.5, 225.2, 225.9, 
226.7, 227.5, 228.2, 228.9, 229.6, 230.4, 231.2, 231.9, 232.6, 
233.4, 234.2, 235, 235.9, 236.8, 237.7, 238.6, 239.7, 241, 242.3, 
243.6, 245.2, 247.1, 249.3, 251.9, 255.3, 260, 266, 274.9, 323.4
), proc = c(0.6171, 1.583, 1.371, 2.13, 1.828, 2.095, 1.994, 
2.694, 2.824, 2.41, 2.909, 3.768, 3.179, 3.029, 3.798, 3.743, 
3.276, 3.213, 3.579, 2.928, 4.634, 3.415, 3.473, 3.135, 3.476, 
3.759, 3.726, 3.9, 3.593, 2.89, 3.707, 4.08, 2.846, 2.685, 3.394, 
2.737, 2.693, 2.878, 2.248, 2.368, 2.258, 2.662, 1.866, 1.895, 
1.457, 1.513, 1.181, 1.008, 0.9641, 0.799, 0.7878, 0.7209, 0.5869, 
0.5778, 0.7313, 0.9531, 1.053, 1.317, 1.247, 1.739, 2.064, 1.99, 
2.522, 2.401, 2.48, 2.687, 2.797, 2.918, 3.243, 3.055, 3.009, 
2.89, 3.037, 3.25, 3.349, 3.141, 2.771, 2.985, 3.203, 3.298, 
3.215, 2.637, 2.683, 2.782, 2.632, 2.625, 2.475, 2.014, 1.781, 
1.987, 1.627, 1.374, 1.352, 0.9441, 1.01, 0.5737, 0.5265, 0.3794, 
0.2513, 0.0351)), row.names = 2:101, class = "data.frame")

temp2 <- structure(list(velik = c(153.8, 156.3, 157.3, 158.4, 159.2, 160.1, 
160.8, 161.6, 162.2, 162.8, 163.5, 164, 164.5, 165, 165.5, 166, 
166.4, 166.9, 167.5, 167.9, 168.5, 168.9, 169.4, 169.8, 170.4, 
170.9, 171.3, 171.8, 172.2, 172.7, 173.3, 173.8, 174.2, 174.8, 
175.5, 176, 176.6, 177.3, 177.9, 178.7, 179.5, 180.3, 180.9, 
181.9, 182.9, 184.1, 185.3, 186.9, 188.8, 190.8, 193.2, 195.6, 
198.4, 201.8, 205.3, 208.1, 210.3, 212.3, 213.9, 215.7, 216.9, 
218, 219.1, 219.9, 220.8, 221.7, 222.6, 223.4, 224.1, 224.8, 
225.6, 226.3, 227.1, 227.8, 228.5, 229.2, 230, 230.8, 231.6, 
232.3, 233, 233.7, 234.6, 235.5, 236.3, 237.2, 238.1, 239.1, 
240.3, 241.6, 242.9, 244.4, 246.1, 248, 250.6, 253.1, 257.6, 
262.5, 269.5, 280.4, 372.9), proc = c(0, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)), row.names = c(NA, 
101L), class = "data.frame")
>

>
__
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] How to modify object's code living in some environment?

2021-12-29 Thread Duncan Murdoch

On 28/12/2021 4:21 p.m., Grzegorz Smoliński wrote:

Thank you for all the comments. If this is not a problem, I would like
to continue this thread as for me a lot of questions are still open.
But if I should post other questions in different messages / topics,
please let me know.

I didn’t answer Ivan’s question previously, but yes, my desired result
is indeed what debug() / debugonce() does and Ivan mentioned trace()
earlier also, so I have tried this. I thought that trace() will be
better in my case (than debug()) since I would like to be able to call
trace() from one environment and insert browser() (using trace()) to
the function defined in parent environment (still not sure if I
understand this concept of environments).

Even if I would like to use it in an shiny app, I have started with
this example:

--

fun1 <- function() {
   a <-  1
   a
}

fun2 <- function() {
   fun1 <- function(){
 b <- 2
 b
   }
   trace(fun1, browser, where = globalenv())
}

fun1()
fun2()
fun1()



So I was hoping to use trace inside child environment of global env,
i.e. set browser() using trace() to the body of function defined in
parent environment, but for sure I do not understand this since
trace() in my example above is putting the browser() function inside
fun1() function defined not in global environment, but in the body of
fun2(). I.e. I'm starting to browse the fun1() function with the local
variable 'b', not 'a' as I wanted.


You are being bitten by normal R scoping.  While fun2 is executing, as 
soon as it executes the fun1 assignment the name fun1 is bound to the 
local function, so when you use fun1 in the first argument to trace(), 
you get that one.  It will ignore then "where" argument because it was 
passed a function object.  If you quote the name it will have to do a 
lookup and it will use "where".  So write fun2 like this:


fun2 <- function() {
  fun1 <- function(){
b <- 2
b
  }
  trace("fun1", browser, where = globalenv())
}




Can I ask for help with this? In other words, how can I refer to some
function from the parent environment (not necessarily defined in the
global environment) being in the child environment?


You could have used parent.frame() in place of globalenv() if you want a 
function that was visible to the caller of fun2.  If you want a function 
that was defined with the same environment as fun2, you can usually use 
environment(fun2), or parent.env(environment()).





Also, if I could ask - is the reversed operation possible? I mean -
can I refer to some function from the child environment being in the
parent environment? I guess not, because of ephemerality?


Sometimes you can, but not usually.  If fun2 had returned a value that 
referenced the evaluation frame, you would be able to see the "b" 
version of fun1 there.  But yours didn't.  This one would:



fun2 <- function() {
  fun1 <- function(){
b <- 2
b
  }
  trace("fun1", browser, where = globalenv())
  environment()
}

Now if you do e <- fun2(), you'll set the trace on the global fun1, but 
e$fun1 will be the local one.


I don't really know how Shiny sets things up, so I can't help with the 
stuff below.


Duncan Murdoch




As I mentioned, I'm thinking about all of this in the context of shiny
app and the shiny app which consists of multiple files, modules. I'm
thinking if I could refer to any function from any environment (or at
least to any function in any parent environment) from one place (one,
current environment) where I will call debug() or trace() to insert
browser() at the beginning of this chosen function. And by "any
function" I'm thinking about another thing problematic for me. If I
consider this example:

---
mod.R inside R/
---

mod_UI <- function(id) {
}

name_mod <- function(input, output, session) {
   fun1 <- reactive({
 a  <-  1
   })
}

---
app.R
---

library(shiny)

ui <- fluidPage(
   mod_UI("mod"),
   textOutput("env")
)

server <- function(input, output, session) {

   name_mod("mod")

   output$env <- renderPrint({
 names(environment(name_mod))
   })

   observe({
 #trace(fun1, browser, where = environment(name_mod))
   })
}

shinyApp(ui, server)
---
I tried to refer to the fun1 in environment(name_mod) - if I'm not
wrong, this is my parent environment, where objects "mod_UI" and
"name_mod" exist, but obviously I'm doing this wrong, right? Because
here: "#trace(fun1, browser, where = environment(name_mod))" I have
tried to refer to the module environment, but I should refer to the
environment inside "name_mod". I don't know how to do this and here I
also would like to ask for help.

I know this may not be the best place for questions regarding shiny,
but I think these questions are purely linked to environment topics.

I also remember Bert's post about "body()" and yes, this seems to be a
better idea than my first try in my first post,