Re: [R-es] Reemplazar con NAs en la columna "esc" condicionado a lo que sucede en otra columna
Hola, Prueba esto: library(tidyverse) data_f <- data %>% mutate(esc = ifelse(b2c == 99, NA, b2c)) Saludos __ Luis Balcázar Universidad Autónoma del Estado de México Instituto Interamericano de Tecnología y Ciencias del Agua https://www.researchgate.net/profile/Luis_Balcazar2 lbalcazaro...@alumno.uaemex.mx Toluca, Edo. de México El jue, 6 may 2021 a las 23:28, JCMld () escribió: > Hola Eric, > > Una solución sería > > df[df$b2c==99,"esc"]<-NA > > (suponiendo que el nombre del data frame sea df) > > Saludos, > Juan > > -Mensaje original- > De: R-help-es En nombre de Eric Concha > M. > Enviado el: viernes, 7 de mayo de 2021 0:41 > Para: r-help-es > Asunto: [R-es] Reemplazar con NAs en la columna "esc" condicionado a lo que > sucede en otra columna > > > Hola comunidad, una consulta por favor, de un problema que no puedo > resolver > ... tengo que asignar NA (missing data) a la columna "esc" > cuando el valor de "b2c" sea 99 ... cómo lo puedo hacer en un único paso ? > he probado con las funciones: > > is.na() > na_if(x, y) > replace_with_na() > > pero esas funciones siempre reemplazan en la misma columna que establece la > condición, me explico ? Aquí un set de datos de ejemplo. > > > folio b2c b2n a16 a19a22 a23 esc > 1: 112721 99 99 1 27 Sin pareja Pueblo originario > 2: 112741 1 99 1 24 Sin pareja Pueblo originario > 3: 115861 99 99 1 40 Con parejaNPANPI > 4: 116081 99 99 1 23 Con parejaNPANPI > 5: 123761 7 99 1 25 Con parejaNPANPI > > > Muchas gracias por la orientación !! > > Saludos, > > Eric. > > ___ > R-help-es mailing list > R-help-es@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-help-es > > ___ > 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] Reemplazar con NAs en la columna "esc" condicionado a lo que sucede en otra columna
Hola Eric, Una solución sería df[df$b2c==99,"esc"]<-NA (suponiendo que el nombre del data frame sea df) Saludos, Juan -Mensaje original- De: R-help-es En nombre de Eric Concha M. Enviado el: viernes, 7 de mayo de 2021 0:41 Para: r-help-es Asunto: [R-es] Reemplazar con NAs en la columna "esc" condicionado a lo que sucede en otra columna Hola comunidad, una consulta por favor, de un problema que no puedo resolver ... tengo que asignar NA (missing data) a la columna "esc" cuando el valor de "b2c" sea 99 ... cómo lo puedo hacer en un único paso ? he probado con las funciones: is.na() na_if(x, y) replace_with_na() pero esas funciones siempre reemplazan en la misma columna que establece la condición, me explico ? Aquí un set de datos de ejemplo. folio b2c b2n a16 a19a22 a23 esc 1: 112721 99 99 1 27 Sin pareja Pueblo originario 2: 112741 1 99 1 24 Sin pareja Pueblo originario 3: 115861 99 99 1 40 Con parejaNPANPI 4: 116081 99 99 1 23 Con parejaNPANPI 5: 123761 7 99 1 25 Con parejaNPANPI Muchas gracias por la orientación !! Saludos, Eric. ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] calculating area of ellipse
On 5/6/21 6:29 PM, Jim Lemon wrote: Hi James, If the result contains the major (a) and minor (b) axes of the ellipse, it's easy: area<-pi*a*b ITYM semi-major and semi-minor axes. -- David try using str() on the result you get. Jim On Fri, May 7, 2021 at 3:51 AM james meyer wrote: In doing meta-analysis of diagnostic accuracy I produce ellipses of confidence and prediction intervals in two dimensions. How can I calculate the area of the ellipse in ggplot2 or base R? thank you James Meyer __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] solving integral equations with undefined parameters using multiroot
lm (cl$y ~ cl$x)$coef (Intercept)cl$x 0.1817509 -1.000 On Fri, May 7, 2021 at 1:56 PM Abbs Spurdle wrote: > > #using vF1 function > #from my previous posts > u <- seq (0, 0.25,, 200) > cl <- contourLines (u, u, outer (u, u, vF1),, 0)[[1]] > plot (cl$x, cl$y, type="l") > > > On Thu, May 6, 2021 at 10:18 PM Ursula Trigos-Raczkowski > wrote: > > > > Thanks for your reply. Unfortunately the code doesn't work even when I > > change the parameters to ensure I have "different" equations. > > Using mathematica I do see that my two equations form planes, intersecting > > in a line of infinite solutions but it is not very accurate, I was hoping R > > would be more accurate and tell me what this line is, or at least a set of > > solutions. > > > > On Thu, May 6, 2021 at 5:28 AM Abbs Spurdle wrote: > >> > >> Just realized five minutes after posting that I misinterpreted your > >> question, slightly. > >> However, after comparing the solution sets for *both* equations, I > >> can't see any obvious difference between the two. > >> If there is any difference, presumably that difference is extremely small. > >> > >> > >> On Thu, May 6, 2021 at 8:39 PM Abbs Spurdle wrote: > >> > > >> > Hi Ursula, > >> > > >> > If I'm not mistaken, there's an infinite number of solutions, which > >> > form a straight (or near straight) line. > >> > Refer to the following code, and attached plot. > >> > > >> > begin code--- > >> > library (barsurf) > >> > vF1 <- function (u, v) > >> > { n <- length (u) > >> > k <- numeric (n) > >> > for (i in seq_len (n) ) > >> > k [i] <- intfun1 (c (u [i], v [i]) ) > >> > k > >> > } > >> > plotf_cfield (vF1, c (0, 0.2), fb = (-2:2) / 10, > >> > main="(integral_1 - 1)", > >> > xlab="S[1]", ylab="S[2]", > >> > n=40, raster=TRUE, theme="heat", contour.labels=TRUE) > >> > end code > >> > > >> > I'm not familiar with the RootSolve package. > >> > Nor am I quite sure what you're trying to compute, given the apparent > >> > infinite set of solutions. > >> > > >> > So, for now at least, I'll leave comments on the root finding to someone > >> > who is. > >> > > >> > > >> > Abby > >> > > >> > > >> > On Thu, May 6, 2021 at 8:46 AM Ursula Trigos-Raczkowski > >> > wrote: > >> > > > >> > > Hello, > >> > > I am trying to solve a system of integral equations using multiroot. I > >> > > have > >> > > tried asking on stack exchange and reddit without any luck. > >> > > Multiroot uses the library(RootSolve). > >> > > > >> > > I have two integral equations involving constants S[1] and S[2] (which > >> > > are > >> > > free.) I would like to find what *positive* values of S[1] and S[2] > >> > > make > >> > > the resulting > >> > > (Integrals-1) = 0. > >> > > (I know that the way I have the parameters set up the equations are > >> > > very > >> > > similar but I am interested in changing the parameters once I have the > >> > > code > >> > > working.) > >> > > My attempt at code: > >> > > > >> > > ```{r} > >> > > a11 <- 1 #alpha_{11} > >> > > a12 <- 1 #alpha_{12} > >> > > a21 <- 1 #alpha_{21} > >> > > a22 <- 1 #alpha_{22} > >> > > b1 <- 2 #beta1 > >> > > b2 <- 2 #beta2 > >> > > d1 <- 1 #delta1 > >> > > d2 <- 1 #delta2 > >> > > g <- 0.5 #gamma > >> > > > >> > > > >> > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > >> > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > >> > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > >> > > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > >> > > > >> > > #defining equation we would like to solve > >> > > intfun1<- function(S) {integrate(function(x) integrand1(x, > >> > > S),lower=0,upper=Inf)[[1]]-1} > >> > > intfun2<- function(S) {integrate(function(x) integrand2(x, > >> > > S),lower=0,upper=Inf)[[1]]-1} > >> > > > >> > > #putting both equations into one term > >> > > model <- function(S) c(F1 = intfun1,F2 = intfun2) > >> > > > >> > > #Solving for roots > >> > > (ss <-multiroot(f=model, start=c(0,0))) > >> > > ``` > >> > > > >> > > This gives me the error Error in stode(y, times, func, parms = parms, > >> > > ...) : > >> > > REAL() can only be applied to a 'numeric', not a 'list' > >> > > > >> > > However this simpler example works fine: > >> > > > >> > > ```{r} > >> > > #Defining the functions > >> > > model <- function(x) c(F1 = x[1]+ 4*x[2] -8,F2 = x[1]-4*x[2]) > >> > > > >> > > #Solving for the roots > >> > > (ss <- multiroot(f = model, start = c(0,0))) > >> > > ``` > >> > > > >> > > Giving me the required x_1= 4 and x_2 =1. > >> > > > >> > > I was given some code to perform a least squares analysis on the same > >> > > system but I neither understand the code, nor believe that it is doing > >> > > what > >> > > I am looking for as different initial values give wildly different S > >> > > values. > >> > > > >> > > ```{r} > >> > > a11 <- 1 #alpha_{11} > >> > > a12 <- 1 #alpha_{12} > >> > > a21 <- 1 #alpha_{21} > >> > > a22 <- 1
Re: [R] solving integral equations with undefined parameters using multiroot
#using vF1 function #from my previous posts u <- seq (0, 0.25,, 200) cl <- contourLines (u, u, outer (u, u, vF1),, 0)[[1]] plot (cl$x, cl$y, type="l") On Thu, May 6, 2021 at 10:18 PM Ursula Trigos-Raczkowski wrote: > > Thanks for your reply. Unfortunately the code doesn't work even when I change > the parameters to ensure I have "different" equations. > Using mathematica I do see that my two equations form planes, intersecting in > a line of infinite solutions but it is not very accurate, I was hoping R > would be more accurate and tell me what this line is, or at least a set of > solutions. > > On Thu, May 6, 2021 at 5:28 AM Abbs Spurdle wrote: >> >> Just realized five minutes after posting that I misinterpreted your >> question, slightly. >> However, after comparing the solution sets for *both* equations, I >> can't see any obvious difference between the two. >> If there is any difference, presumably that difference is extremely small. >> >> >> On Thu, May 6, 2021 at 8:39 PM Abbs Spurdle wrote: >> > >> > Hi Ursula, >> > >> > If I'm not mistaken, there's an infinite number of solutions, which >> > form a straight (or near straight) line. >> > Refer to the following code, and attached plot. >> > >> > begin code--- >> > library (barsurf) >> > vF1 <- function (u, v) >> > { n <- length (u) >> > k <- numeric (n) >> > for (i in seq_len (n) ) >> > k [i] <- intfun1 (c (u [i], v [i]) ) >> > k >> > } >> > plotf_cfield (vF1, c (0, 0.2), fb = (-2:2) / 10, >> > main="(integral_1 - 1)", >> > xlab="S[1]", ylab="S[2]", >> > n=40, raster=TRUE, theme="heat", contour.labels=TRUE) >> > end code >> > >> > I'm not familiar with the RootSolve package. >> > Nor am I quite sure what you're trying to compute, given the apparent >> > infinite set of solutions. >> > >> > So, for now at least, I'll leave comments on the root finding to someone >> > who is. >> > >> > >> > Abby >> > >> > >> > On Thu, May 6, 2021 at 8:46 AM Ursula Trigos-Raczkowski >> > wrote: >> > > >> > > Hello, >> > > I am trying to solve a system of integral equations using multiroot. I >> > > have >> > > tried asking on stack exchange and reddit without any luck. >> > > Multiroot uses the library(RootSolve). >> > > >> > > I have two integral equations involving constants S[1] and S[2] (which >> > > are >> > > free.) I would like to find what *positive* values of S[1] and S[2] make >> > > the resulting >> > > (Integrals-1) = 0. >> > > (I know that the way I have the parameters set up the equations are very >> > > similar but I am interested in changing the parameters once I have the >> > > code >> > > working.) >> > > My attempt at code: >> > > >> > > ```{r} >> > > a11 <- 1 #alpha_{11} >> > > a12 <- 1 #alpha_{12} >> > > a21 <- 1 #alpha_{21} >> > > a22 <- 1 #alpha_{22} >> > > b1 <- 2 #beta1 >> > > b2 <- 2 #beta2 >> > > d1 <- 1 #delta1 >> > > d2 <- 1 #delta2 >> > > g <- 0.5 #gamma >> > > >> > > >> > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* >> > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} >> > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* >> > > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} >> > > >> > > #defining equation we would like to solve >> > > intfun1<- function(S) {integrate(function(x) integrand1(x, >> > > S),lower=0,upper=Inf)[[1]]-1} >> > > intfun2<- function(S) {integrate(function(x) integrand2(x, >> > > S),lower=0,upper=Inf)[[1]]-1} >> > > >> > > #putting both equations into one term >> > > model <- function(S) c(F1 = intfun1,F2 = intfun2) >> > > >> > > #Solving for roots >> > > (ss <-multiroot(f=model, start=c(0,0))) >> > > ``` >> > > >> > > This gives me the error Error in stode(y, times, func, parms = parms, >> > > ...) : >> > > REAL() can only be applied to a 'numeric', not a 'list' >> > > >> > > However this simpler example works fine: >> > > >> > > ```{r} >> > > #Defining the functions >> > > model <- function(x) c(F1 = x[1]+ 4*x[2] -8,F2 = x[1]-4*x[2]) >> > > >> > > #Solving for the roots >> > > (ss <- multiroot(f = model, start = c(0,0))) >> > > ``` >> > > >> > > Giving me the required x_1= 4 and x_2 =1. >> > > >> > > I was given some code to perform a least squares analysis on the same >> > > system but I neither understand the code, nor believe that it is doing >> > > what >> > > I am looking for as different initial values give wildly different S >> > > values. >> > > >> > > ```{r} >> > > a11 <- 1 #alpha_{11} >> > > a12 <- 1 #alpha_{12} >> > > a21 <- 1 #alpha_{21} >> > > a22 <- 1 #alpha_{22} >> > > b1 <- 2 #beta1 >> > > b2 <- 2 #beta2 >> > > d1 <- 1 #delta1 >> > > d2 <- 1 #delta2 >> > > g <- 0.5 #gamma >> > > >> > > >> > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* >> > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} >> > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* >> > >
Re: [R] calculating area of ellipse
Hi James, If the result contains the major (a) and minor (b) axes of the ellipse, it's easy: area<-pi*a*b try using str() on the result you get. Jim On Fri, May 7, 2021 at 3:51 AM james meyer wrote: > > In doing meta-analysis of diagnostic accuracy I produce ellipses of confidence > and prediction intervals in two dimensions. How can I calculate the area of > the ellipse in ggplot2 or base R? > > thank you > James Meyer > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R-es] Reemplazar con NAs en la columna "esc" condicionado a lo que sucede en otra columna
Hola comunidad, una consulta por favor, de un problema que no puedo resolver ... tengo que asignar NA (missing data) a la columna "esc" cuando el valor de "b2c" sea 99 ... cómo lo puedo hacer en un único paso ? he probado con las funciones: is.na() na_if(x, y) replace_with_na() pero esas funciones siempre reemplazan en la misma columna que establece la condición, me explico ? Aquí un set de datos de ejemplo. folio b2c b2n a16 a19a22 a23 esc 1: 112721 99 99 1 27 Sin pareja Pueblo originario 2: 112741 1 99 1 24 Sin pareja Pueblo originario 3: 115861 99 99 1 40 Con parejaNPANPI 4: 116081 99 99 1 23 Con parejaNPANPI 5: 123761 7 99 1 25 Con parejaNPANPI Muchas gracias por la orientación !! Saludos, Eric. ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] Analysing data with repeated measure variable
This looks like homework. We don't do homework on this list. To see what is done here, read and follow the posting guide linked below. If not homework, I think it is still very much out of bounds anyway, as you appear to be asking us to do your work for you. On Thu, May 6, 2021, 2:32 PM Ahmad Raza wrote: > Dear Experts, > Greetings > > I have the following type of repeated measures data (table 1), and events > data in table 2 (single measure). I want to perform the following tasks (in > R or excel sheet please). > >- To filter subjects who had any response at least 3 days. >- Response should be > 5 in each day. >- Then table 1 should have another column, date first response recorded. >- Then both tables should be merged > > Table 1 – Response Data > > Sub_NoResponse Date1 5 01-Jan1 5 > 02-Jan2 5 01-Jan2 10 02-Jan2 > 10 03-Jan2 10 04-Jan2 10 > 05-Jan3 10 01-Jan3 10 02-Jan3 > 10 03-Jan4 5 01-Jan4 5 > 02-Jan4 10 03-Jan4 10 04-Jan4 > 10 05-Jan > > > Table 2 – Event Data > > Sub_No ResponseDate1 No2 Yes 30 > Jan3 Yes 29 Jan4 No > > Thanks for your help. > Regards, > > [[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] Analysing data with repeated measure variable
Dear Experts, Greetings I have the following type of repeated measures data (table 1), and events data in table 2 (single measure). I want to perform the following tasks (in R or excel sheet please). - To filter subjects who had any response at least 3 days. - Response should be > 5 in each day. - Then table 1 should have another column, date first response recorded. - Then both tables should be merged Table 1 – Response Data Sub_NoResponse Date1 5 01-Jan1 5 02-Jan2 5 01-Jan2 10 02-Jan2 10 03-Jan2 10 04-Jan2 10 05-Jan3 10 01-Jan3 10 02-Jan3 10 03-Jan4 5 01-Jan4 5 02-Jan4 10 03-Jan4 10 04-Jan4 10 05-Jan Table 2 – Event Data Sub_No ResponseDate1 No2 Yes 30 Jan3 Yes 29 Jan4 No Thanks for your help. Regards, [[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] Transforming data
Posting HTML email is a good way to reduce your chances of getting a response. On May 6, 2021 1:13:16 PM PDT, Jeff Reichman wrote: >R-help > >Never mind I figured out a working solution > >- remove duplicate >- mutate a new column == 1 >- spread the data from long to wide >- replace NA with 0's > >Not sure it’s the most elegant but gets the gob done > > >-Original Message- >From: R-help On Behalf Of Jeff Reichman >Sent: Thursday, May 6, 2021 2:37 PM >To: R-help@r-project.org >Subject: [R] Transforming data > >R Help Forum > >I am attempting to transform the data frame in Table 1 to the form >shown in Table 2. Any suggestions. I ve started by removing duplicate >rows > > > >Jeff > > > > > >Table 1 > > >Taxon > >Importer > > >Guarouba guarouba > >AE > > >Acipenser gueldenstaedtii > >AE > > >Caiman crocodilus fuscus > >AE > > >Caiman crocodilus fuscus > >AE > > >Caiman crocodilus fuscus > >AE > > >Ara ararauna > >AG > > >Ara chloropterus > >AG > > >Python reticulatus > >AE > > >Strombus gigas > >AE > > >Strombus gigas > >AE > > >Strombus gigas > >AE > > >Strombus gigas > >AE > > >Strombus gigas > >AE > > >Varanus niloticus > >AE > > >Varanus niloticus > >AE > > >Caiman crocodilus fuscus > >AE > > > > > >Table 2 > > >Country > >Guarouba guarouba > >Acipenser gueldenstaedtii > >Caiman crocodilus fuscus > >Python reticulatus > >Strombus gigas > >Varanus niloticus > >Caiman crocodilus fuscus > >Ara ararauna > >Caiman crocodilus fuscus > > >AE > >1 > >1 > >1 > >1 > >1 > >1 > >1 > >0 > >0 > > >AG > >0 > >0 > >0 > >0 > >0 > >0 > >0 > >1 > >1 > > > > > [[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. -- Sent from my phone. Please excuse my brevity. __ 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] Transforming data
R-help Never mind I figured out a working solution - remove duplicate - mutate a new column == 1 - spread the data from long to wide - replace NA with 0's Not sure it’s the most elegant but gets the gob done -Original Message- From: R-help On Behalf Of Jeff Reichman Sent: Thursday, May 6, 2021 2:37 PM To: R-help@r-project.org Subject: [R] Transforming data R Help Forum I am attempting to transform the data frame in Table 1 to the form shown in Table 2. Any suggestions. I ve started by removing duplicate rows Jeff Table 1 Taxon Importer Guarouba guarouba AE Acipenser gueldenstaedtii AE Caiman crocodilus fuscus AE Caiman crocodilus fuscus AE Caiman crocodilus fuscus AE Ara ararauna AG Ara chloropterus AG Python reticulatus AE Strombus gigas AE Strombus gigas AE Strombus gigas AE Strombus gigas AE Strombus gigas AE Varanus niloticus AE Varanus niloticus AE Caiman crocodilus fuscus AE Table 2 Country Guarouba guarouba Acipenser gueldenstaedtii Caiman crocodilus fuscus Python reticulatus Strombus gigas Varanus niloticus Caiman crocodilus fuscus Ara ararauna Caiman crocodilus fuscus AE 1 1 1 1 1 1 1 0 0 AG 0 0 0 0 0 0 0 1 1 [[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] Transforming data
R Help Forum I am attempting to transform the data frame in Table 1 to the form shown in Table 2. Any suggestions. I�ve started by removing duplicate rows Jeff Table 1 Taxon Importer Guarouba guarouba AE Acipenser gueldenstaedtii AE Caiman crocodilus fuscus AE Caiman crocodilus fuscus AE Caiman crocodilus fuscus AE Ara ararauna AG Ara chloropterus AG Python reticulatus AE Strombus gigas AE Strombus gigas AE Strombus gigas AE Strombus gigas AE Strombus gigas AE Varanus niloticus AE Varanus niloticus AE Caiman crocodilus fuscus AE Table 2 Country Guarouba guarouba Acipenser gueldenstaedtii Caiman crocodilus fuscus Python reticulatus Strombus gigas Varanus niloticus Caiman crocodilus fuscus Ara ararauna Caiman crocodilus fuscus AE 1 1 1 1 1 1 1 0 0 AG 0 0 0 0 0 0 0 1 1 [[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] calculating area of ellipse
Dear James, To mix notation a bit, presumably the (border of the) confidence ellipse is of the form (b - beta)'V(b)^-1 (b - beta) = c, where V(b) is the covariance matrix of b and c is a constant. Then the area of the ellipse is pi*c^2*sqrt(det(V(b))). It shouldn't be hard to translate that into R code. I hope this helps, John John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada web: https://socialsciences.mcmaster.ca/jfox/ John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada web: https://socialsciences.mcmaster.ca/jfox/ On 2021-05-06 7:24 a.m., james meyer wrote: In doing meta-analysis of diagnostic accuracy I produce ellipses of confidence and prediction intervals in two dimensions. How can I calculate the area of the ellipse in ggplot2 or base R? thank you James Meyer __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] calculating area of ellipse
In doing meta-analysis of diagnostic accuracy I produce ellipses of confidence and prediction intervals in two dimensions. How can I calculate the area of the ellipse in ggplot2 or base R? thank you James Meyer __ 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] solving integral equations with undefined parameters using multiroot
Thanks for your reply. Unfortunately the code doesn't work even when I change the parameters to ensure I have "different" equations. Using mathematica I do see that my two equations form planes, intersecting in a line of infinite solutions but it is not very accurate, I was hoping R would be more accurate and tell me what this line is, or at least a set of solutions. On Thu, May 6, 2021 at 5:28 AM Abbs Spurdle wrote: > Just realized five minutes after posting that I misinterpreted your > question, slightly. > However, after comparing the solution sets for *both* equations, I > can't see any obvious difference between the two. > If there is any difference, presumably that difference is extremely small. > > > On Thu, May 6, 2021 at 8:39 PM Abbs Spurdle wrote: > > > > Hi Ursula, > > > > If I'm not mistaken, there's an infinite number of solutions, which > > form a straight (or near straight) line. > > Refer to the following code, and attached plot. > > > > begin code--- > > library (barsurf) > > vF1 <- function (u, v) > > { n <- length (u) > > k <- numeric (n) > > for (i in seq_len (n) ) > > k [i] <- intfun1 (c (u [i], v [i]) ) > > k > > } > > plotf_cfield (vF1, c (0, 0.2), fb = (-2:2) / 10, > > main="(integral_1 - 1)", > > xlab="S[1]", ylab="S[2]", > > n=40, raster=TRUE, theme="heat", contour.labels=TRUE) > > end code > > > > I'm not familiar with the RootSolve package. > > Nor am I quite sure what you're trying to compute, given the apparent > > infinite set of solutions. > > > > So, for now at least, I'll leave comments on the root finding to someone > who is. > > > > > > Abby > > > > > > On Thu, May 6, 2021 at 8:46 AM Ursula Trigos-Raczkowski > > wrote: > > > > > > Hello, > > > I am trying to solve a system of integral equations using multiroot. I > have > > > tried asking on stack exchange and reddit without any luck. > > > Multiroot uses the library(RootSolve). > > > > > > I have two integral equations involving constants S[1] and S[2] (which > are > > > free.) I would like to find what *positive* values of S[1] and S[2] > make > > > the resulting > > > (Integrals-1) = 0. > > > (I know that the way I have the parameters set up the equations are > very > > > similar but I am interested in changing the parameters once I have the > code > > > working.) > > > My attempt at code: > > > > > > ```{r} > > > a11 <- 1 #alpha_{11} > > > a12 <- 1 #alpha_{12} > > > a21 <- 1 #alpha_{21} > > > a22 <- 1 #alpha_{22} > > > b1 <- 2 #beta1 > > > b2 <- 2 #beta2 > > > d1 <- 1 #delta1 > > > d2 <- 1 #delta2 > > > g <- 0.5 #gamma > > > > > > > > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > > > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > > > > > > #defining equation we would like to solve > > > intfun1<- function(S) {integrate(function(x) integrand1(x, > > > S),lower=0,upper=Inf)[[1]]-1} > > > intfun2<- function(S) {integrate(function(x) integrand2(x, > > > S),lower=0,upper=Inf)[[1]]-1} > > > > > > #putting both equations into one term > > > model <- function(S) c(F1 = intfun1,F2 = intfun2) > > > > > > #Solving for roots > > > (ss <-multiroot(f=model, start=c(0,0))) > > > ``` > > > > > > This gives me the error Error in stode(y, times, func, parms = parms, > ...) : > > > REAL() can only be applied to a 'numeric', not a 'list' > > > > > > However this simpler example works fine: > > > > > > ```{r} > > > #Defining the functions > > > model <- function(x) c(F1 = x[1]+ 4*x[2] -8,F2 = x[1]-4*x[2]) > > > > > > #Solving for the roots > > > (ss <- multiroot(f = model, start = c(0,0))) > > > ``` > > > > > > Giving me the required x_1= 4 and x_2 =1. > > > > > > I was given some code to perform a least squares analysis on the same > > > system but I neither understand the code, nor believe that it is doing > what > > > I am looking for as different initial values give wildly different S > values. > > > > > > ```{r} > > > a11 <- 1 #alpha_{11} > > > a12 <- 1 #alpha_{12} > > > a21 <- 1 #alpha_{21} > > > a22 <- 1 #alpha_{22} > > > b1 <- 2 #beta1 > > > b2 <- 2 #beta2 > > > d1 <- 1 #delta1 > > > d2 <- 1 #delta2 > > > g <- 0.5 #gamma > > > > > > > > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > > > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > > > > > > #defining equation we would like to solve > > > intfun1<- function(S) {integrate(function(x)integrand1(x, > > > S),lower=0,upper=Inf)[[1]]-1} > > > intfun2<- function(S) {integrate(function(x)integrand2(x, > > > S),lower=0,upper=Inf)[[1]]-1} > > > > > > #putting both equations into one term > > > model <- function(S) if(any(S<0))NA else intfun1(S)**2+ intfun2(S)**2 > > > > >
Re: [R] solving integral equations with undefined parameters using multiroot
Just realized five minutes after posting that I misinterpreted your question, slightly. However, after comparing the solution sets for *both* equations, I can't see any obvious difference between the two. If there is any difference, presumably that difference is extremely small. On Thu, May 6, 2021 at 8:39 PM Abbs Spurdle wrote: > > Hi Ursula, > > If I'm not mistaken, there's an infinite number of solutions, which > form a straight (or near straight) line. > Refer to the following code, and attached plot. > > begin code--- > library (barsurf) > vF1 <- function (u, v) > { n <- length (u) > k <- numeric (n) > for (i in seq_len (n) ) > k [i] <- intfun1 (c (u [i], v [i]) ) > k > } > plotf_cfield (vF1, c (0, 0.2), fb = (-2:2) / 10, > main="(integral_1 - 1)", > xlab="S[1]", ylab="S[2]", > n=40, raster=TRUE, theme="heat", contour.labels=TRUE) > end code > > I'm not familiar with the RootSolve package. > Nor am I quite sure what you're trying to compute, given the apparent > infinite set of solutions. > > So, for now at least, I'll leave comments on the root finding to someone who > is. > > > Abby > > > On Thu, May 6, 2021 at 8:46 AM Ursula Trigos-Raczkowski > wrote: > > > > Hello, > > I am trying to solve a system of integral equations using multiroot. I have > > tried asking on stack exchange and reddit without any luck. > > Multiroot uses the library(RootSolve). > > > > I have two integral equations involving constants S[1] and S[2] (which are > > free.) I would like to find what *positive* values of S[1] and S[2] make > > the resulting > > (Integrals-1) = 0. > > (I know that the way I have the parameters set up the equations are very > > similar but I am interested in changing the parameters once I have the code > > working.) > > My attempt at code: > > > > ```{r} > > a11 <- 1 #alpha_{11} > > a12 <- 1 #alpha_{12} > > a21 <- 1 #alpha_{21} > > a22 <- 1 #alpha_{22} > > b1 <- 2 #beta1 > > b2 <- 2 #beta2 > > d1 <- 1 #delta1 > > d2 <- 1 #delta2 > > g <- 0.5 #gamma > > > > > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > > > > #defining equation we would like to solve > > intfun1<- function(S) {integrate(function(x) integrand1(x, > > S),lower=0,upper=Inf)[[1]]-1} > > intfun2<- function(S) {integrate(function(x) integrand2(x, > > S),lower=0,upper=Inf)[[1]]-1} > > > > #putting both equations into one term > > model <- function(S) c(F1 = intfun1,F2 = intfun2) > > > > #Solving for roots > > (ss <-multiroot(f=model, start=c(0,0))) > > ``` > > > > This gives me the error Error in stode(y, times, func, parms = parms, ...) : > > REAL() can only be applied to a 'numeric', not a 'list' > > > > However this simpler example works fine: > > > > ```{r} > > #Defining the functions > > model <- function(x) c(F1 = x[1]+ 4*x[2] -8,F2 = x[1]-4*x[2]) > > > > #Solving for the roots > > (ss <- multiroot(f = model, start = c(0,0))) > > ``` > > > > Giving me the required x_1= 4 and x_2 =1. > > > > I was given some code to perform a least squares analysis on the same > > system but I neither understand the code, nor believe that it is doing what > > I am looking for as different initial values give wildly different S values. > > > > ```{r} > > a11 <- 1 #alpha_{11} > > a12 <- 1 #alpha_{12} > > a21 <- 1 #alpha_{21} > > a22 <- 1 #alpha_{22} > > b1 <- 2 #beta1 > > b2 <- 2 #beta2 > > d1 <- 1 #delta1 > > d2 <- 1 #delta2 > > g <- 0.5 #gamma > > > > > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > > > > #defining equation we would like to solve > > intfun1<- function(S) {integrate(function(x)integrand1(x, > > S),lower=0,upper=Inf)[[1]]-1} > > intfun2<- function(S) {integrate(function(x)integrand2(x, > > S),lower=0,upper=Inf)[[1]]-1} > > > > #putting both equations into one term > > model <- function(S) if(any(S<0))NA else intfun1(S)**2+ intfun2(S)**2 > > > > #Solving for roots > > optim(c(0,0), model) > > ``` > > > > I appreciate any tips/help as I have been struggling with this for some > > weeks now. > > thank you, > > -- > > Ursula > > Ph.D. student, University of Michigan > > Applied and Interdisciplinary Mathematics > > utri...@umich.edu > > > > [[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] solving integral equations with undefined parameters using multiroot
Hi Ursula, If I'm not mistaken, there's an infinite number of solutions, which form a straight (or near straight) line. Refer to the following code, and attached plot. begin code--- library (barsurf) vF1 <- function (u, v) { n <- length (u) k <- numeric (n) for (i in seq_len (n) ) k [i] <- intfun1 (c (u [i], v [i]) ) k } plotf_cfield (vF1, c (0, 0.2), fb = (-2:2) / 10, main="(integral_1 - 1)", xlab="S[1]", ylab="S[2]", n=40, raster=TRUE, theme="heat", contour.labels=TRUE) end code I'm not familiar with the RootSolve package. Nor am I quite sure what you're trying to compute, given the apparent infinite set of solutions. So, for now at least, I'll leave comments on the root finding to someone who is. Abby On Thu, May 6, 2021 at 8:46 AM Ursula Trigos-Raczkowski wrote: > > Hello, > I am trying to solve a system of integral equations using multiroot. I have > tried asking on stack exchange and reddit without any luck. > Multiroot uses the library(RootSolve). > > I have two integral equations involving constants S[1] and S[2] (which are > free.) I would like to find what *positive* values of S[1] and S[2] make > the resulting > (Integrals-1) = 0. > (I know that the way I have the parameters set up the equations are very > similar but I am interested in changing the parameters once I have the code > working.) > My attempt at code: > > ```{r} > a11 <- 1 #alpha_{11} > a12 <- 1 #alpha_{12} > a21 <- 1 #alpha_{21} > a22 <- 1 #alpha_{22} > b1 <- 2 #beta1 > b2 <- 2 #beta2 > d1 <- 1 #delta1 > d2 <- 1 #delta2 > g <- 0.5 #gamma > > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > > #defining equation we would like to solve > intfun1<- function(S) {integrate(function(x) integrand1(x, > S),lower=0,upper=Inf)[[1]]-1} > intfun2<- function(S) {integrate(function(x) integrand2(x, > S),lower=0,upper=Inf)[[1]]-1} > > #putting both equations into one term > model <- function(S) c(F1 = intfun1,F2 = intfun2) > > #Solving for roots > (ss <-multiroot(f=model, start=c(0,0))) > ``` > > This gives me the error Error in stode(y, times, func, parms = parms, ...) : > REAL() can only be applied to a 'numeric', not a 'list' > > However this simpler example works fine: > > ```{r} > #Defining the functions > model <- function(x) c(F1 = x[1]+ 4*x[2] -8,F2 = x[1]-4*x[2]) > > #Solving for the roots > (ss <- multiroot(f = model, start = c(0,0))) > ``` > > Giving me the required x_1= 4 and x_2 =1. > > I was given some code to perform a least squares analysis on the same > system but I neither understand the code, nor believe that it is doing what > I am looking for as different initial values give wildly different S values. > > ```{r} > a11 <- 1 #alpha_{11} > a12 <- 1 #alpha_{12} > a21 <- 1 #alpha_{21} > a22 <- 1 #alpha_{22} > b1 <- 2 #beta1 > b2 <- 2 #beta2 > d1 <- 1 #delta1 > d2 <- 1 #delta2 > g <- 0.5 #gamma > > > integrand1 <- function(x,S) {b1*g/d1*exp(-g*x)*(1-exp(-d1* > x))*exp(-a11*b1*S[1]/d1*(1-exp(-d1*x))-a12*b2*S[2]/d2*(1-exp(-d2*x)))} > integrand2 <- function(x,S) {b2*g/d2*exp(-g*x)*(1-exp(-d2* > x))*exp(-a22*b2*S[2]/d2*(1-exp(-d2*x))-a21*b1*S[1]/d1*(1-exp(-d1*x)))} > > #defining equation we would like to solve > intfun1<- function(S) {integrate(function(x)integrand1(x, > S),lower=0,upper=Inf)[[1]]-1} > intfun2<- function(S) {integrate(function(x)integrand2(x, > S),lower=0,upper=Inf)[[1]]-1} > > #putting both equations into one term > model <- function(S) if(any(S<0))NA else intfun1(S)**2+ intfun2(S)**2 > > #Solving for roots > optim(c(0,0), model) > ``` > > I appreciate any tips/help as I have been struggling with this for some > weeks now. > thank you, > -- > Ursula > Ph.D. student, University of Michigan > Applied and Interdisciplinary Mathematics > utri...@umich.edu > > [[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.