Re: [R] help - Package: stats - function ar.ols
The data came through fine, the program was a miss. Can you paste the program into a ".txt" document like a notepad file and send that? You could also paste it into your email IF your email is configured to send text and NOT html. TIm -Original Message- From: R-help On Behalf Of Pedro Gavronski. Sent: Friday, February 23, 2024 5:00 AM To: Rui Barradas Cc: r-help@r-project.org; r-help-requ...@r-project.org Subject: Re: [R] help - Package: stats - function ar.ols [External Email] Hello, Thanks for the reply Rui and for pointing out that I forgot to attach my code. Please find attached in this email my code and data. Thanks in advance. Best regards, Pedro Gerhardt Gavronski. On Fri, Feb 23, 2024 at 5:50 AM Rui Barradas wrote: > > Às 16:34 de 22/02/2024, Pedro Gavronski. escreveu: > > Hello, > > > > My name is Pedro and it is nice to meet you all. I am having trouble > > understanding a message that I receive when use function ar.ols from > > package stats, it says that "Warning message: > > In ar.ols(x = dtb[2:6966, ], demean = FALSE, intercept = TRUE, > > prewhite = TRUE) : > >model order: 2 singularities in the computation of the > > projection matrix results are only valid up to model order 1, which > > I do not know what it means, if someone could clarify it, I would > > really appreciate it. > > > > Attached to this email you will find my code and data I used to run > > this formula. > > > > Thanks in advance. > > > > Best regards, Pedro. > > > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://st/ > > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl > > .edu%7C0afd65836d774adc099408dc349f53c7%7C0d4da0f84a314d76ace60a6233 > > 1e1b84%7C0%7C0%7C638443106144255449%7CUnknown%7CTWFpbGZsb3d8eyJWIjoi > > MC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C > > %7C=12OGc13jk3Lejmcvx5WH8Bko8JSpRxbtW3mzL3OIjyk%3D=0 > > PLEASE do read the posting guide > > http://www/ > > .r-project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu% > > 7C0afd65836d774adc099408dc349f53c7%7C0d4da0f84a314d76ace60a62331e1b8 > > 4%7C0%7C0%7C638443106144262387%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wL > > jAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C > > data=fAVz0iQ0zS5pmrxrzNeazRrrv%2FyjFdWvwMfgjhibdEA%3D=0 > > and provide commented, minimal, self-contained, reproducible code. > Hello, > > Thanks for the data but the code is missing from the attachment. > Can you please post your code? In an attachment or directly in the > e-mail body. > > Rui Barradas > > > -- > Este e-mail foi analisado pelo software antivírus AVG para verificar a > presença de vírus. > http://www.a/ > vg.com%2F=05%7C02%7Ctebert%40ufl.edu%7C0afd65836d774adc099408dc34 > 9f53c7%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638443106144266047 > %7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6I > k1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=rdqEhXdZ5%2FThzbhuKgspNaBPyhf > aC%2BWh7MFq4iq%2BxVE%3D=0 __ 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] help - Package: stats - function ar.ols
Às 16:34 de 22/02/2024, Pedro Gavronski. escreveu: Hello, My name is Pedro and it is nice to meet you all. I am having trouble understanding a message that I receive when use function ar.ols from package stats, it says that "Warning message: In ar.ols(x = dtb[2:6966, ], demean = FALSE, intercept = TRUE, prewhite = TRUE) : model order: 2 singularities in the computation of the projection matrix results are only valid up to model order 1, which I do not know what it means, if someone could clarify it, I would really appreciate it. Attached to this email you will find my code and data I used to run this formula. Thanks in advance. Best regards, Pedro. __ 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. Hello, Thanks for the data but the code is missing from the attachment. Can you please post your code? In an attachment or directly in the e-mail body. Rui Barradas -- Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus. www.avg.com __ 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] Help with spTransform() function and final plot colors
Thank you Ege, I appreciate your response. I have move this to r-sig-geo. WHP Proprietary -Original Message- From: Ege Rubak Sent: Monday, May 18, 2020 6:41 AM To: Poling, William ; r-help@r-project.org Subject: [EXTERNAL] Re: [R] Help with spTransform() function and final plot colors External Email - Use Caution You are more likely to get help with specific problems related to spTransform() on the dedicated list r-sig-geo. You should provide a minimal reproducible example. Your code refers to e.g. the object `tmp1b`, which we don't have. I think the spTransform() part will work with this correction: xy <- SpatialPointsDataFrame(sample[,2:3], sample[,1,drop=FALSE], proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")) xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) The spatial workflow in R has largely moved to the `sf` package and you are probably better off using that in the future if you are new in the game anyway. Hope this helps, Ege On Sun, 2020-05-17 at 09:50 +, Poling, William via R-help wrote: > #RStudio Version Version 1.2.1335 > sessionInfo() > # R version 4.0.0 Patched (2020-05-03 r78349) > #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows 10 > x64 (build 17763) > > Hello. I am running my data through a routine I found that finds > clusters of data points based on distance rule. > https://urldefense.proofpoint.com/v2/url?u=https-3A__gis.stackexchange.com_questions_64392_finding-2Dclusters-2Dof-2Dpoints-2Dbased-2Ddistance-2Drule-2Dusing-2Dr=DwIGaQ=wluqKIiwffOpZ6k5sqMWMBOn0vyYnlulRJmmvOXCFpM=j7MrcIQm2xjHa8v-2mTpmTCtKvneM2ExlYvnUWbsByY=sFygsSovnYNWjYOpjSzuSLI1MUrJN8Lih39pDQN98xI=2FCT-JBdWSB66pNvGLTM7Ec5Q7IbXatXBLsQqYT0gng= > > 1. I get this error when I get to this point in the routine, see > complete routine below? > xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) non finite > transformation detected: > > I tried converting columns from numeric to Integer but did not help. > > However, I continue to run the rest of the routine and the final > sequence, the plot itself, seems to work Can this error be corrected > somehow, despite the fact that it continues to work, just curious what > it is I guess" > > 2. However in the plot at the end of the routine the color key appears > with the colors but the clusters in the plot are black, see plot call > at the end of the routine below? > > Thank you for any advice. > > WHP > > ERROR: > non finite transformation detected: > coords.x1 coords.x2 > [1,] -119.7339 39.53939 Inf Inf > [2,] -119.7665 39.39630 Inf Inf > [3,] -119.7794 39.28768 Inf Inf > [4,] -121.0234 39.20503 Inf Inf > [5,] -122.0047 47.19262 Inf Inf > [6,] -122.0135 47.18883 Inf Inf > [7,] -122.0379 47.52190 Inf Inf > [8,] -122.0578 47.60975 Inf Inf > [9,] -122.1330 47.13669 Inf Inf > [10,] -122.1509 47.55962 Inf Inf > [11,] -122.1706 47.15546 Inf Inf > [12,] -122.1846 47.23485 Inf Inf > [13,] -122.1846 48.15307 Inf Inf > [14,] -122.1851 47.44870 Inf Inf > [15,] -122.1954 47.68485 Inf Inf > [16,] -122.1990 47.51610 Inf Inf > [17,] -122.2014 47.44772 Inf Inf > [18,] -122.2025 47.69815 Inf Inf > [19,] -122.2037 47.67190 Inf Inf > [20,] -122.2090 47.40378 Inf Inf > [21,] -122.2108 47.25336 Inf Inf > [22,] -122.2291 47.63880 Inf Inf > [23,] -122.2419 47.76870 Inf Inf > [24,] -122.2722 48.04803 Inf Inf > [25,] -122.2732 47.87700 Inf Inf > [26,] -122.2804 47.77620 Inf Inf > [27,] -122.2839 47.82103 Inf Inf > [28,] -122.2890 47.86899 Inf Inf > [29,] -122.2993 47.67306 Inf Inf > [30,] -122.3180 47.38217 Inf Inf > [31,] -122.3270 47.40378 Inf Inf > [32,] -122.3474 47.43884 Inf Inf > [33,] -122.3484 47.53083 Inf Inf > [34,] -122.3581 47.27678 Inf Inf > [35,] -122.3618 47.76735 Inf Inf > [36,] -122.3700 47.56567 Inf Inf > [37,] -122.3908 47.54938 Inf Inf > [38,] -122.4128 47.64622 Inf Inf > [39,] -122.4293 47.17660 Inf Inf > [40,] -122.4621 47.44505 Inf Inf > [41,] -122.4904 47.27460 Inf Inf > [42,] -122.5515 46.93979 Inf Inf > [43,] -122.7348 42.37320 Inf Inf > [44,] -122.7827 47.31059 Inf Inf > [45,] -122.7987 47.23475 Inf Inf > [46,] -122.8385 42.35119 Inf Inf > [47,] -122.8537 42.34495 Inf Inf > [48,] -122.8904 42.39555 Inf Inf > [49,] -122.8927 42.33022 Inf Inf > [50,] -122.9451 47.37574 Inf Inf > [51,] -122.9594 42.30376 Inf Inf > [52,] -123.0641 47.16428 Inf Inf > [53,] -123.3413 42.44117 Inf Inf > Error in spTransform(xSP, CRSobj, ...) : > failure in points > 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:4
Re: [R] Help with spTransform() function and final plot colors
You are more likely to get help with specific problems related to spTransform() on the dedicated list r-sig-geo. You should provide a minimal reproducible example. Your code refers to e.g. the object `tmp1b`, which we don't have. I think the spTransform() part will work with this correction: xy <- SpatialPointsDataFrame(sample[,2:3], sample[,1,drop=FALSE], proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")) xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) The spatial workflow in R has largely moved to the `sf` package and you are probably better off using that in the future if you are new in the game anyway. Hope this helps, Ege On Sun, 2020-05-17 at 09:50 +, Poling, William via R-help wrote: > #RStudio Version Version 1.2.1335 > sessionInfo() > # R version 4.0.0 Patched (2020-05-03 r78349) > #Platform: x86_64-w64-mingw32/x64 (64-bit) > #Running under: Windows 10 x64 (build 17763) > > Hello. I am running my data through a routine I found that finds > clusters of data points based on distance rule. > https://gis.stackexchange.com/questions/64392/finding-clusters-of-points-based-distance-rule-using-r > > 1. I get this error when I get to this point in the routine, see > complete routine below? > xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) > non finite transformation detected: > > I tried converting columns from numeric to Integer but did not help. > > However, I continue to run the rest of the routine and the final > sequence, the plot itself, seems to work > Can this error be corrected somehow, despite the fact that it > continues to work, just curious what it is I guess" > > 2. However in the plot at the end of the routine the color key > appears with the colors but the clusters in the plot are black, see > plot call at the end of the routine below? > > Thank you for any advice. > > WHP > > ERROR: > non finite transformation detected: > coords.x1 coords.x2 > [1,] -119.7339 39.53939 Inf Inf > [2,] -119.7665 39.39630 Inf Inf > [3,] -119.7794 39.28768 Inf Inf > [4,] -121.0234 39.20503 Inf Inf > [5,] -122.0047 47.19262 Inf Inf > [6,] -122.0135 47.18883 Inf Inf > [7,] -122.0379 47.52190 Inf Inf > [8,] -122.0578 47.60975 Inf Inf > [9,] -122.1330 47.13669 Inf Inf > [10,] -122.1509 47.55962 Inf Inf > [11,] -122.1706 47.15546 Inf Inf > [12,] -122.1846 47.23485 Inf Inf > [13,] -122.1846 48.15307 Inf Inf > [14,] -122.1851 47.44870 Inf Inf > [15,] -122.1954 47.68485 Inf Inf > [16,] -122.1990 47.51610 Inf Inf > [17,] -122.2014 47.44772 Inf Inf > [18,] -122.2025 47.69815 Inf Inf > [19,] -122.2037 47.67190 Inf Inf > [20,] -122.2090 47.40378 Inf Inf > [21,] -122.2108 47.25336 Inf Inf > [22,] -122.2291 47.63880 Inf Inf > [23,] -122.2419 47.76870 Inf Inf > [24,] -122.2722 48.04803 Inf Inf > [25,] -122.2732 47.87700 Inf Inf > [26,] -122.2804 47.77620 Inf Inf > [27,] -122.2839 47.82103 Inf Inf > [28,] -122.2890 47.86899 Inf Inf > [29,] -122.2993 47.67306 Inf Inf > [30,] -122.3180 47.38217 Inf Inf > [31,] -122.3270 47.40378 Inf Inf > [32,] -122.3474 47.43884 Inf Inf > [33,] -122.3484 47.53083 Inf Inf > [34,] -122.3581 47.27678 Inf Inf > [35,] -122.3618 47.76735 Inf Inf > [36,] -122.3700 47.56567 Inf Inf > [37,] -122.3908 47.54938 Inf Inf > [38,] -122.4128 47.64622 Inf Inf > [39,] -122.4293 47.17660 Inf Inf > [40,] -122.4621 47.44505 Inf Inf > [41,] -122.4904 47.27460 Inf Inf > [42,] -122.5515 46.93979 Inf Inf > [43,] -122.7348 42.37320 Inf Inf > [44,] -122.7827 47.31059 Inf Inf > [45,] -122.7987 47.23475 Inf Inf > [46,] -122.8385 42.35119 Inf Inf > [47,] -122.8537 42.34495 Inf Inf > [48,] -122.8904 42.39555 Inf Inf > [49,] -122.8927 42.33022 Inf Inf > [50,] -122.9451 47.37574 Inf Inf > [51,] -122.9594 42.30376 Inf Inf > [52,] -123.0641 47.16428 Inf Inf > [53,] -123.3413 42.44117 Inf Inf > Error in spTransform(xSP, CRSobj, ...) : > failure in points > 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 > In addition: Warning message: > In spTransform(xSP, CRSobj, ...) : 53 projected point(s) not finite > > Here is the data: > dput(sample) > structure(list(ID = 1:53, Longitude = c(-119.733899, -119.766493, > -119.779416, -121.0234, -122.004736, -122.013456, -122.0379, > -122.0578, -122.132971, -122.150901, -122.170608, -122.18462, > -122.184639, -122.185079, -122.195398, -122.198994, -122.201356, > -122.202507, -122.20371, -122.209047, -122.210797, -122.229095, > -122.2419, -122.27216, -122.273164, -122.280355, -122.28389, > -122.289043, -122.299261, -122.318009, -122.326987, -122.347382, > -122.34844, -122.358115, -122.361839, -122.37003, -122.390815, > -122.41282, -122.429323, -122.462136, -122.490417, -122.551483, > -122.734847, -122.782736, -122.798669, -122.838498, -122.853683, > -122.8904, -122.89271, -122.94511, -122.959407, -123.064087, >
Re: [R] Help with spTransform() function and final plot colors
Hello again. I also found this discussion on non-finite transformation error, however, I am not sure what to look for in the output after I apply my data? https://stackoverflow.com/questions/14880294/non-finite-transformation-detected-in-sptransform-in-rgdal-r-package str(sample) GPS.Points <- sample[,c(2,3)] n_chunks <- 3 #number of pieces you will break you data into n.points <- dim(GPS.Points)[1] breaks <- seq(1,n.points, by=round(n.points/n_chunks)) breaks <- c(breaks, n.points) #make sure to include last points as well i=1 for (i in 1:(length(breaks)-1)){ cat('\n','converting points', breaks[i], "to", breaks[i+1]) temp.GPS.Points=GPS.Points[breaks[i]:breaks[i+1],] temp.GPS.Points.Spatial.Data <- SpatialPoints(temp.GPS.Points, proj4string=CRS("+proj=longlat +ellps=WGS84")) temp.GPS.Points.UTM.Spatial.Data <- spTransform(temp.GPS.Points.Spatial.Data, CRS("+proj=utm +south +zone=34 +ellps=WGS84")) } Thank you for any advice. WHP Proprietary -Original Message- From: Poling, William Sent: Sunday, May 17, 2020 5:20 AM To: r-help@r-project.org Cc: Poling, William Subject: RE: Help with spTransform() function and final plot colors Hello, I have found an additional problem. I should be getting 3 columns back in the xy@data at some point. $ ID : int 1 2 3 4 5 6 7 8 9 10 ... # $ Clust : int 1 1 1 1 1 1 1 1 1 1 ... This is always - 1 # $ Clust.1 : int 1 1 1 1 1 1 1 1 1 1 ... This would be the cluster number, like 1-3, I am not getting this column back? Yet the plot runs and indicates three different clusters? WHP Proprietary -Original Message- From: Poling, William Sent: Sunday, May 17, 2020 4:50 AM To: r-help@r-project.org Cc: Poling, William Subject: Help with spTransform() function and final plot colors #RStudio Version Version 1.2.1335 sessionInfo() # R version 4.0.0 Patched (2020-05-03 r78349) #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows 10 x64 (build 17763) Hello. I am running my data through a routine I found that finds clusters of data points based on distance rule. https://gis.stackexchange.com/questions/64392/finding-clusters-of-points-based-distance-rule-using-r 1. I get this error when I get to this point in the routine, see complete routine below? xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) non finite transformation detected: I tried converting columns from numeric to Integer but did not help. However, I continue to run the rest of the routine and the final sequence, the plot itself, seems to work Can this error be corrected somehow, despite the fact that it continues to work, just curious what it is I guess" 2. However in the plot at the end of the routine the color key appears with the colors but the clusters in the plot are black, see plot call at the end of the routine below? Thank you for any advice. WHP ERROR: non finite transformation detected: coords.x1 coords.x2 [1,] -119.7339 39.53939 Inf Inf [2,] -119.7665 39.39630 Inf Inf [3,] -119.7794 39.28768 Inf Inf [4,] -121.0234 39.20503 Inf Inf [5,] -122.0047 47.19262 Inf Inf [6,] -122.0135 47.18883 Inf Inf [7,] -122.0379 47.52190 Inf Inf [8,] -122.0578 47.60975 Inf Inf [9,] -122.1330 47.13669 Inf Inf [10,] -122.1509 47.55962 Inf Inf [11,] -122.1706 47.15546 Inf Inf [12,] -122.1846 47.23485 Inf Inf [13,] -122.1846 48.15307 Inf Inf [14,] -122.1851 47.44870 Inf Inf [15,] -122.1954 47.68485 Inf Inf [16,] -122.1990 47.51610 Inf Inf [17,] -122.2014 47.44772 Inf Inf [18,] -122.2025 47.69815 Inf Inf [19,] -122.2037 47.67190 Inf Inf [20,] -122.2090 47.40378 Inf Inf [21,] -122.2108 47.25336 Inf Inf [22,] -122.2291 47.63880 Inf Inf [23,] -122.2419 47.76870 Inf Inf [24,] -122.2722 48.04803 Inf Inf [25,] -122.2732 47.87700 Inf Inf [26,] -122.2804 47.77620 Inf Inf [27,] -122.2839 47.82103 Inf Inf [28,] -122.2890 47.86899 Inf Inf [29,] -122.2993 47.67306 Inf Inf [30,] -122.3180 47.38217 Inf Inf [31,] -122.3270 47.40378 Inf Inf [32,] -122.3474 47.43884 Inf Inf [33,] -122.3484 47.53083 Inf Inf [34,] -122.3581 47.27678 Inf Inf [35,] -122.3618 47.76735 Inf Inf [36,] -122.3700 47.56567 Inf Inf [37,] -122.3908 47.54938 Inf Inf [38,] -122.4128 47.64622 Inf Inf [39,] -122.4293 47.17660 Inf Inf [40,] -122.4621 47.44505 Inf Inf [41,] -122.4904 47.27460 Inf Inf [42,] -122.5515 46.93979 Inf Inf [43,] -122.7348 42.37320 Inf Inf [44,] -122.7827 47.31059 Inf Inf [45,] -122.7987 47.23475 Inf Inf [46,] -122.8385 42.35119 Inf Inf [47,] -122.8537 42.34495 Inf Inf [48,] -122.8904 42.39555 Inf Inf [49,] -122.8927 42.33022 Inf Inf [50,] -122.9451 47.37574 Inf Inf [51,] -122.9594 42.30376 Inf Inf [52,] -123.0641 47.16428 Inf Inf [53,] -123.3413 42.44117 Inf Inf Error in spTransform(xSP, CRSobj, ...) : failure in points
Re: [R] Help with spTransform() function and final plot colors
Hello, I have found an additional problem. I should be getting 3 columns back in the xy@data at some point. $ ID : int 1 2 3 4 5 6 7 8 9 10 ... # $ Clust : int 1 1 1 1 1 1 1 1 1 1 ... This is always - 1 # $ Clust.1 : int 1 1 1 1 1 1 1 1 1 1 ... This would be the cluster number, like 1-3, I am not getting this column back? Yet the plot runs and indicates three different clusters? WHP Proprietary -Original Message- From: Poling, William Sent: Sunday, May 17, 2020 4:50 AM To: r-help@r-project.org Cc: Poling, William Subject: Help with spTransform() function and final plot colors #RStudio Version Version 1.2.1335 sessionInfo() # R version 4.0.0 Patched (2020-05-03 r78349) #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows 10 x64 (build 17763) Hello. I am running my data through a routine I found that finds clusters of data points based on distance rule. https://gis.stackexchange.com/questions/64392/finding-clusters-of-points-based-distance-rule-using-r 1. I get this error when I get to this point in the routine, see complete routine below? xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) non finite transformation detected: I tried converting columns from numeric to Integer but did not help. However, I continue to run the rest of the routine and the final sequence, the plot itself, seems to work Can this error be corrected somehow, despite the fact that it continues to work, just curious what it is I guess" 2. However in the plot at the end of the routine the color key appears with the colors but the clusters in the plot are black, see plot call at the end of the routine below? Thank you for any advice. WHP ERROR: non finite transformation detected: coords.x1 coords.x2 [1,] -119.7339 39.53939 Inf Inf [2,] -119.7665 39.39630 Inf Inf [3,] -119.7794 39.28768 Inf Inf [4,] -121.0234 39.20503 Inf Inf [5,] -122.0047 47.19262 Inf Inf [6,] -122.0135 47.18883 Inf Inf [7,] -122.0379 47.52190 Inf Inf [8,] -122.0578 47.60975 Inf Inf [9,] -122.1330 47.13669 Inf Inf [10,] -122.1509 47.55962 Inf Inf [11,] -122.1706 47.15546 Inf Inf [12,] -122.1846 47.23485 Inf Inf [13,] -122.1846 48.15307 Inf Inf [14,] -122.1851 47.44870 Inf Inf [15,] -122.1954 47.68485 Inf Inf [16,] -122.1990 47.51610 Inf Inf [17,] -122.2014 47.44772 Inf Inf [18,] -122.2025 47.69815 Inf Inf [19,] -122.2037 47.67190 Inf Inf [20,] -122.2090 47.40378 Inf Inf [21,] -122.2108 47.25336 Inf Inf [22,] -122.2291 47.63880 Inf Inf [23,] -122.2419 47.76870 Inf Inf [24,] -122.2722 48.04803 Inf Inf [25,] -122.2732 47.87700 Inf Inf [26,] -122.2804 47.77620 Inf Inf [27,] -122.2839 47.82103 Inf Inf [28,] -122.2890 47.86899 Inf Inf [29,] -122.2993 47.67306 Inf Inf [30,] -122.3180 47.38217 Inf Inf [31,] -122.3270 47.40378 Inf Inf [32,] -122.3474 47.43884 Inf Inf [33,] -122.3484 47.53083 Inf Inf [34,] -122.3581 47.27678 Inf Inf [35,] -122.3618 47.76735 Inf Inf [36,] -122.3700 47.56567 Inf Inf [37,] -122.3908 47.54938 Inf Inf [38,] -122.4128 47.64622 Inf Inf [39,] -122.4293 47.17660 Inf Inf [40,] -122.4621 47.44505 Inf Inf [41,] -122.4904 47.27460 Inf Inf [42,] -122.5515 46.93979 Inf Inf [43,] -122.7348 42.37320 Inf Inf [44,] -122.7827 47.31059 Inf Inf [45,] -122.7987 47.23475 Inf Inf [46,] -122.8385 42.35119 Inf Inf [47,] -122.8537 42.34495 Inf Inf [48,] -122.8904 42.39555 Inf Inf [49,] -122.8927 42.33022 Inf Inf [50,] -122.9451 47.37574 Inf Inf [51,] -122.9594 42.30376 Inf Inf [52,] -123.0641 47.16428 Inf Inf [53,] -123.3413 42.44117 Inf Inf Error in spTransform(xSP, CRSobj, ...) : failure in points 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 In addition: Warning message: In spTransform(xSP, CRSobj, ...) : 53 projected point(s) not finite Here is the data: dput(sample) structure(list(ID = 1:53, Longitude = c(-119.733899, -119.766493, -119.779416, -121.0234, -122.004736, -122.013456, -122.0379, -122.0578, -122.132971, -122.150901, -122.170608, -122.18462, -122.184639, -122.185079, -122.195398, -122.198994, -122.201356, -122.202507, -122.20371, -122.209047, -122.210797, -122.229095, -122.2419, -122.27216, -122.273164, -122.280355, -122.28389, -122.289043, -122.299261, -122.318009, -122.326987, -122.347382, -122.34844, -122.358115, -122.361839, -122.37003, -122.390815, -122.41282, -122.429323, -122.462136, -122.490417, -122.551483, -122.734847, -122.782736, -122.798669, -122.838498, -122.853683, -122.8904, -122.89271, -122.94511, -122.959407, -123.064087, -123.341346), Latitude = c(39.53939, 39.396298, 39.287681, 39.205028, 47.192616, 47.188833, 47.5219, 47.609748, 47.13669, 47.559616, 47.155455, 47.234849, 48.15307, 47.448697, 47.684854, 47.516104, 47.447723, 47.698146, 47.6719, 47.403778, 47.253364, 47.638795, 47.768701, 48.048027, 47.876997, 47.776205,
[R] Help with spTransform() function and final plot colors
#RStudio Version Version 1.2.1335 sessionInfo() # R version 4.0.0 Patched (2020-05-03 r78349) #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows 10 x64 (build 17763) Hello. I am running my data through a routine I found that finds clusters of data points based on distance rule. https://gis.stackexchange.com/questions/64392/finding-clusters-of-points-based-distance-rule-using-r 1. I get this error when I get to this point in the routine, see complete routine below? xy <- spTransform(xy, CRS("+init=epsg:27700 +datum=WGS84")) non finite transformation detected: I tried converting columns from numeric to Integer but did not help. However, I continue to run the rest of the routine and the final sequence, the plot itself, seems to work Can this error be corrected somehow, despite the fact that it continues to work, just curious what it is I guess" 2. However in the plot at the end of the routine the color key appears with the colors but the clusters in the plot are black, see plot call at the end of the routine below? Thank you for any advice. WHP ERROR: non finite transformation detected: coords.x1 coords.x2 [1,] -119.7339 39.53939 Inf Inf [2,] -119.7665 39.39630 Inf Inf [3,] -119.7794 39.28768 Inf Inf [4,] -121.0234 39.20503 Inf Inf [5,] -122.0047 47.19262 Inf Inf [6,] -122.0135 47.18883 Inf Inf [7,] -122.0379 47.52190 Inf Inf [8,] -122.0578 47.60975 Inf Inf [9,] -122.1330 47.13669 Inf Inf [10,] -122.1509 47.55962 Inf Inf [11,] -122.1706 47.15546 Inf Inf [12,] -122.1846 47.23485 Inf Inf [13,] -122.1846 48.15307 Inf Inf [14,] -122.1851 47.44870 Inf Inf [15,] -122.1954 47.68485 Inf Inf [16,] -122.1990 47.51610 Inf Inf [17,] -122.2014 47.44772 Inf Inf [18,] -122.2025 47.69815 Inf Inf [19,] -122.2037 47.67190 Inf Inf [20,] -122.2090 47.40378 Inf Inf [21,] -122.2108 47.25336 Inf Inf [22,] -122.2291 47.63880 Inf Inf [23,] -122.2419 47.76870 Inf Inf [24,] -122.2722 48.04803 Inf Inf [25,] -122.2732 47.87700 Inf Inf [26,] -122.2804 47.77620 Inf Inf [27,] -122.2839 47.82103 Inf Inf [28,] -122.2890 47.86899 Inf Inf [29,] -122.2993 47.67306 Inf Inf [30,] -122.3180 47.38217 Inf Inf [31,] -122.3270 47.40378 Inf Inf [32,] -122.3474 47.43884 Inf Inf [33,] -122.3484 47.53083 Inf Inf [34,] -122.3581 47.27678 Inf Inf [35,] -122.3618 47.76735 Inf Inf [36,] -122.3700 47.56567 Inf Inf [37,] -122.3908 47.54938 Inf Inf [38,] -122.4128 47.64622 Inf Inf [39,] -122.4293 47.17660 Inf Inf [40,] -122.4621 47.44505 Inf Inf [41,] -122.4904 47.27460 Inf Inf [42,] -122.5515 46.93979 Inf Inf [43,] -122.7348 42.37320 Inf Inf [44,] -122.7827 47.31059 Inf Inf [45,] -122.7987 47.23475 Inf Inf [46,] -122.8385 42.35119 Inf Inf [47,] -122.8537 42.34495 Inf Inf [48,] -122.8904 42.39555 Inf Inf [49,] -122.8927 42.33022 Inf Inf [50,] -122.9451 47.37574 Inf Inf [51,] -122.9594 42.30376 Inf Inf [52,] -123.0641 47.16428 Inf Inf [53,] -123.3413 42.44117 Inf Inf Error in spTransform(xSP, CRSobj, ...) : failure in points 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 In addition: Warning message: In spTransform(xSP, CRSobj, ...) : 53 projected point(s) not finite Here is the data: dput(sample) structure(list(ID = 1:53, Longitude = c(-119.733899, -119.766493, -119.779416, -121.0234, -122.004736, -122.013456, -122.0379, -122.0578, -122.132971, -122.150901, -122.170608, -122.18462, -122.184639, -122.185079, -122.195398, -122.198994, -122.201356, -122.202507, -122.20371, -122.209047, -122.210797, -122.229095, -122.2419, -122.27216, -122.273164, -122.280355, -122.28389, -122.289043, -122.299261, -122.318009, -122.326987, -122.347382, -122.34844, -122.358115, -122.361839, -122.37003, -122.390815, -122.41282, -122.429323, -122.462136, -122.490417, -122.551483, -122.734847, -122.782736, -122.798669, -122.838498, -122.853683, -122.8904, -122.89271, -122.94511, -122.959407, -123.064087, -123.341346), Latitude = c(39.53939, 39.396298, 39.287681, 39.205028, 47.192616, 47.188833, 47.5219, 47.609748, 47.13669, 47.559616, 47.155455, 47.234849, 48.15307, 47.448697, 47.684854, 47.516104, 47.447723, 47.698146, 47.6719, 47.403778, 47.253364, 47.638795, 47.768701, 48.048027, 47.876997, 47.776205, 47.821029, 47.868987, 47.673056, 47.382165, 47.403785, 47.438836, 47.530831, 47.276776, 47.76735, 47.565667, 47.549377, 47.646222, 47.176596, 47.445053, 47.274599, 46.939789, 42.373195, 47.310595, 47.234748, 42.351189, 42.344953, 42.395547, 42.33022, 47.375736, 42.303755, 47.164278, 42.441172)), class = "data.frame", row.names = c(NA, -53L)) Here is the routine: require(sp) require(rgdal) #25*1609.34 = 40233.5 dis <- 40233.5 #Distance threshold 25miles converted to meters x <- tmp1b[,c(6)] head(x) y <- tmp1b[,c(5)] head(y) str(y) xy <- SpatialPointsDataFrame(matrix(c(x,y), ncol=2), data.frame(ID=seq(1:length(x))),
Re: [R] Help with gsub function
Good morning Steve. Terrific, so kind of you to follow-up. I will add that to my ever growing R bag of tips and tricks. Cheers. WHP William H. Poling, Ph.D., MPH | Manager, Revenue Development Data Intelligence & Analytics Zelis Healthcare -Original Message- From: S Ellison Sent: Monday, March 18, 2019 8:32 AM To: Bill Poling ; r-help (r-help@r-project.org) Subject: RE: Help with gsub function > tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "") Just to add something on why this didn't work ... It looks like you were trying to match a hyphen followed by a number up to seven digits. by mistake(?) you gave the digit range as [0-0] so it would repmatch a hyphen followed by between none and seven zeroes. When it met "-0" it matched that. And because it was gsub, it replaced what it matched. If you'd given it the right digit range it would have replaced the whole of the number. If you _really_ wanted to do that kind of thing (control the following pattern), you'd have needed something like (untested) gsub("-([0-0]{0,7})", "\\1", tb2a$TID) #The () means 'remember this bit"; the "\\1" means "put the first thing you remember here". And it needs to be "\\1" because that becomes "\1" for the grep parser. Steve E *** This email and any attachments are confidential. Any use...{{dropped:22}} __ 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] Help with gsub function
> tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "") Just to add something on why this didn't work ... It looks like you were trying to match a hyphen followed by a number up to seven digits. by mistake(?) you gave the digit range as [0-0] so it would repmatch a hyphen followed by between none and seven zeroes. When it met "-0" it matched that. And because it was gsub, it replaced what it matched. If you'd given it the right digit range it would have replaced the whole of the number. If you _really_ wanted to do that kind of thing (control the following pattern), you'd have needed something like (untested) gsub("-([0-0]{0,7})", "\\1", tb2a$TID) #The () means 'remember this bit"; the "\\1" means "put the first thing you remember here". And it needs to be "\\1" because that becomes "\1" for the grep parser. Steve E *** This email and any attachments are confidential. Any use...{{dropped:8}} __ 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] Help with gsub function
Yep, thank you Jeff, consequence of the first url I landed on asking how to do it and rushing off. All set now. Appreciate your help. WHP From: Jeff Newmiller Sent: Friday, March 15, 2019 4:00 PM To: r-help@r-project.org; Bill Poling ; r-help (r-help@r-project.org) Subject: Re: [R] Help with gsub function Your pattern seems ... way overboard? Why not gsub("-", "", tb2a$TID) On March 15, 2019 12:45:27 PM PDT, Bill Poling <mailto:bill.pol...@zelis.com> wrote: >Good afternoon. > >sessionInfo() >#R version 3.5.3 (2019-03-11) >#Platform: x86_64-w64-mingw32/x64 (64-bit) >#Running under: Windows >= 8 x64 (build 9200) > >I am using gsub function to remove a hyphen in a 9 character column of >values in order to convert it to integer. > >Works fine except where the second segment has a leading 0, then it is >eliminating the 0 > >Example "73-0700090" becomes " 73700090" > "77-0633896" becomes "77633896" > >Is there a remedy for this? > >tb2a$TID2 <- > >head(tb2a$TID,n=10) >[1] "11-1352310" "45-2711804" "35-6001540" "77-0633896" "62-1762545" >"61-1029768" "73-0700090" "47-0376604" "47-0486026" "38-3833117" >> head(tb2a$TID2,n=10) >[1] "111352310" "452711804" "356001540" "77633896" "621762545" >"611029768" "73700090" "47376604" "47486026" "383833117" > >I have googled the problem and have not found a solution. > >http://www.endmemo.com/program/R/gsub.php >http://r.789695.n4.nabble.com/extracting-characters-from-string-td3298971.html > > >Thank you. > >WHP > >Confidentiality Notice This message is sent from Zelis. >...{{dropped:13}} > >__ >mailto: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. Confidentiality Notice This message is sent from Zelis. This transmission may contain information which is privileged and confidential and is intended for the personal and confidential use of the named recipient only. Such information may be protected by applicable State and Federal laws from this disclosure or unauthorized use. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, review, discussion, copying, or taking any action in reliance on the contents of this transmission is strictly prohibited. If you have received this transmission in error, please contact the sender immediately. Zelis, 2018. __ 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] Help with gsub function
Good morning Peter, yes that works fine. My attempt was based on a google search that looked promising but was obviously more complicated than it needed to be. Thank you. WHP From: Peter Langfelder Sent: Friday, March 15, 2019 3:53 PM To: Bill Poling Cc: r-help (r-help@r-project.org) Subject: Re: [R] Help with gsub function If you want to remove just the hyphen, why not do sub("-", "", tb2a$TID) sub("-", "", "73-017323") [1] "73017323" Am I missing something? Peter On Fri, Mar 15, 2019 at 12:46 PM Bill Poling <mailto:bill.pol...@zelis.com> wrote: > > Good afternoon. > > sessionInfo() > #R version 3.5.3 (2019-03-11) > #Platform: x86_64-w64-mingw32/x64 (64-bit) > #Running under: Windows >= 8 x64 (build 9200) > > I am using gsub function to remove a hyphen in a 9 character column of values > in order to convert it to integer. > > Works fine except where the second segment has a leading 0, then it is > eliminating the 0 > > Example "73-0700090" becomes " 73700090" > "77-0633896" becomes "77633896" > > Is there a remedy for this? > > tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "") > > head(tb2a$TID,n=10) > [1] "11-1352310" "45-2711804" "35-6001540" "77-0633896" "62-1762545" > "61-1029768" "73-0700090" "47-0376604" "47-0486026" "38-3833117" > > head(tb2a$TID2,n=10) > [1] "111352310" "452711804" "356001540" "77633896" "621762545" "611029768" > "73700090" "47376604" "47486026" "383833117" > > I have googled the problem and have not found a solution. > > http://www.endmemo.com/program/R/gsub.php > http://r.789695.n4.nabble.com/extracting-characters-from-string-td3298971.html > > > Thank you. > > WHP > > Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}} > > __ > mailto: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. Confidentiality Notice This message is sent from Zelis. This transmission may contain information which is privileged and confidential and is intended for the personal and confidential use of the named recipient only. Such information may be protected by applicable State and Federal laws from this disclosure or unauthorized use. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, review, discussion, copying, or taking any action in reliance on the contents of this transmission is strictly prohibited. If you have received this transmission in error, please contact the sender immediately. Zelis, 2018. __ 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] Help with gsub function
Your pattern seems ... way overboard? Why not gsub("-", "", tb2a$TID) On March 15, 2019 12:45:27 PM PDT, Bill Poling wrote: >Good afternoon. > >sessionInfo() >#R version 3.5.3 (2019-03-11) >#Platform: x86_64-w64-mingw32/x64 (64-bit) >#Running under: Windows >= 8 x64 (build 9200) > >I am using gsub function to remove a hyphen in a 9 character column of >values in order to convert it to integer. > >Works fine except where the second segment has a leading 0, then it is >eliminating the 0 > >Example "73-0700090" becomes " 73700090" > "77-0633896" becomes "77633896" > >Is there a remedy for this? > >tb2a$TID2 <- > >head(tb2a$TID,n=10) >[1] "11-1352310" "45-2711804" "35-6001540" "77-0633896" "62-1762545" >"61-1029768" "73-0700090" "47-0376604" "47-0486026" "38-3833117" >> head(tb2a$TID2,n=10) >[1] "111352310" "452711804" "356001540" "77633896" "621762545" >"611029768" "73700090" "47376604" "47486026" "383833117" > >I have googled the problem and have not found a solution. > >http://www.endmemo.com/program/R/gsub.php >http://r.789695.n4.nabble.com/extracting-characters-from-string-td3298971.html > > >Thank you. > >WHP > >Confidentiality Notice This message is sent from Zelis. >...{{dropped:13}} > >__ >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] Help with gsub function
If you want to remove just the hyphen, why not do sub("-", "", tb2a$TID) sub("-", "", "73-017323") [1] "73017323" Am I missing something? Peter On Fri, Mar 15, 2019 at 12:46 PM Bill Poling wrote: > > Good afternoon. > > sessionInfo() > #R version 3.5.3 (2019-03-11) > #Platform: x86_64-w64-mingw32/x64 (64-bit) > #Running under: Windows >= 8 x64 (build 9200) > > I am using gsub function to remove a hyphen in a 9 character column of values > in order to convert it to integer. > > Works fine except where the second segment has a leading 0, then it is > eliminating the 0 > > Example "73-0700090" becomes " 73700090" > "77-0633896" becomes "77633896" > > Is there a remedy for this? > > tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "") > > head(tb2a$TID,n=10) > [1] "11-1352310" "45-2711804" "35-6001540" "77-0633896" "62-1762545" > "61-1029768" "73-0700090" "47-0376604" "47-0486026" "38-3833117" > > head(tb2a$TID2,n=10) > [1] "111352310" "452711804" "356001540" "77633896" "621762545" "611029768" > "73700090" "47376604" "47486026" "383833117" > > I have googled the problem and have not found a solution. > > http://www.endmemo.com/program/R/gsub.php > http://r.789695.n4.nabble.com/extracting-characters-from-string-td3298971.html > > > Thank you. > > WHP > > Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}} > > __ > 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] Help with gsub function
On Fri, 15 Mar 2019 19:45:27 + Bill Poling wrote: Hello Bill, > tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "") Is the pattern supposed to mean something besides the "-" you want to remove? For the problem you describe, pattern="-" should be enough. It should locate all hyphens in the string and replace them with empty strings, i.e. remove them. -- Best regards, Ivan __ 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 with gsub function
Good afternoon. sessionInfo() #R version 3.5.3 (2019-03-11) #Platform: x86_64-w64-mingw32/x64 (64-bit) #Running under: Windows >= 8 x64 (build 9200) I am using gsub function to remove a hyphen in a 9 character column of values in order to convert it to integer. Works fine except where the second segment has a leading 0, then it is eliminating the 0 Example "73-0700090" becomes " 73700090" "77-0633896" becomes "77633896" Is there a remedy for this? tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "") head(tb2a$TID,n=10) [1] "11-1352310" "45-2711804" "35-6001540" "77-0633896" "62-1762545" "61-1029768" "73-0700090" "47-0376604" "47-0486026" "38-3833117" > head(tb2a$TID2,n=10) [1] "111352310" "452711804" "356001540" "77633896" "621762545" "611029768" "73700090" "47376604" "47486026" "383833117" I have googled the problem and have not found a solution. http://www.endmemo.com/program/R/gsub.php http://r.789695.n4.nabble.com/extracting-characters-from-string-td3298971.html Thank you. WHP Confidentiality Notice This message is sent from Zelis. ...{{dropped:13}} __ 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 with read function in Azure Data Lake Remote environment
Hi, Locally I am using windows: R version 3.5.1 (2018-07-02) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 Locally on my laptop using RStudio I normally set my working directory and read in my csv files. setwd("C:/WHP/Appeals") getwd()#-Check to verify read.csv("WHP_EditAppealRev_Tmp V1.csv",nrows=1, head=FALSE)#N=61 columns appdf1 <- fread("WHP_EditAppealRev_Tmp V1.csv",select=c(5,9,10,11,14,15,16,17,18,25,31,28,32,42,45,46,47,48,49,53,54,58,59,61),header=TRUE, stringsAsFactors=TRUE) This all works fine locally. Now I am working in RStudio in a Remote desk top AZURE Data Lake environment (DSVM) however the remote file path out there is does not appear to be that different. Remotley I am using windows: R version 3.5.1 (2018-07-02) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows Server >= 2012 x64 (build 9200) setwd("C:/Users/sysAdmin/Documents/WHP/NJDemo082018") getwd()#-Check to verify The directory gets set but the csv does not get read for some reason? read.csv("C:/Users/sysAdmin/Documents/WHP/NJDemo082018/TestDemoV1.csv",nrows=1, head=FALSE)#N= columns Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'C:/Users/sysAdmin/Documents/WHP/NJDemo082018/TestDemoV1.csv': No such file or directory demo1 <- fread("C:/Users/sysAdmin/Documents/WHP/NJDemo082018/TestDemoV1.csv",select=c(1,2,3),header=TRUE, stringsAsFactors=TRUE) Error in fread("C:/Users/sysAdmin/Documents/WHP/NJDemo082018/TestDemoV1.csv", : File 'C:/Users/sysAdmin/Documents/WHP/NJDemo082018/TestDemoV1.csv' does not exist; getwd()=='C:/Users/sysAdmin/Documents/WHP/NJDemo082018'. Include correct full path, or one or more spaces to consider the input a system command. I have tried moving the csv file further forward to the root C: but still no luck? read.csv("C:/TestDemoV1.csv",nrows=1, head=FALSE)#N= columns Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'C:/TestDemoV1.csv': No such file or directory Any suggestions would be appreciated, thank you. WHP Confidentiality Notice This message is sent from Zelis. ...{{dropped:15}} __ 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] help with recursive function
recursive_funlp <- function(dataset = dat1, func = funlp2) { ... if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() # GIVE SOME ARGUMENTS HERE return(df2) } } When you recurse into recursve_funip(), give it at least a new dataset argument. Otherwise it just processes the entire dat1 again and the recursion never stops. Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Eric: I will try and see if I can figure out the issue by debugging as > you suggested. I don’t know why my code after stopifnot is not getting > executed where I like the code to run the funlp2 function when the if > statement is TRUE but when it is false, I like it to keep running until the > stopifnot condition is met. > > When the stopifnot condition is met, I like to get the output from if > statement saved. > Anyway, I will keep trying. > Again, Thanks for your help! > Nilesh > > From: Eric Berger [mailto:ericjber...@gmail.com] > Sent: Thursday, December 14, 2017 10:29 AM > To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> > Cc: r-help <r-help@r-project.org> > Subject: Re: [R] help with recursive function > > If you are trying to understand why the "stopifnot" condition is met you > can replace it by something like: > > if ( any(dat2$norm_sd >= 1) ) >browser() > > This will put you in a debugging session where you can examine your > variables, e.g. > > > dat$norm_sd > > HTH, > Eric > > > > On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com > <mailto:ericjber...@gmail.com>> wrote: > The message is coming from your stopifnot() condition being met. > > > On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: > Hi, I accidently left out few lines of code from the calclp function. > Updated function is pasted below. > I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is > not TRUE“ > > I would appreciate any help. > Nilesh > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > names(.)), funs(norm = round(scale(.), 3))) > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, sd, na.rm = TRUE), 3) > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, function(x) { > max(abs(x), na.rm = TRUE) > }), 3) > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > "\\.") %>% spread(treatment, value) %>% > mutate(outlier = NA) > stopifnot(!(any(data1$norm_sd >= 1))) > if (!(any(data1$norm_sd >= 1))) { > df1 <- dat1 > return(df1) > } > else { > df2 <- recursive_funlp() > return(df2) > } > } > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > df3 > } > > > From: DIGHE, NILESH [AG/2362] > Sent: Thursday, December 14, 2017 9:01 AM > To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> > Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> > Subject: RE: [R] help with recursive function > > Eric: Thanks for taking time to look into my problem. Despite of making > the change you suggested, I am still getting the same error. I am > wondering if the logic I am using in the stopifnot and if functions is a > problem. > I like the recursive function to stop whenever the norm_sd column has zero > values that are above or equal to 1. Below is the calclp function after the > changes you suggested. > Thanks. Nilesh > > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >
Re: [R] help with recursive function
When I run the code without stopifnot, the code takes 5 min to run and then it throws an error listed below without producing any results. Error: node stack overflow In addition: There were 50 or more warnings (use warnings() to see the first 50) Error during wrapup: node stack overflow Thanks. Nilesh From: William Dunlap [mailto:wdun...@tibco.com] Sent: Thursday, December 14, 2017 11:26 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> Cc: Eric Berger <ericjber...@gmail.com>; r-help <r-help@r-project.org> Subject: Re: [R] help with recursive function Your code contains the lines stopifnot(!(any(data1$norm_sd >= 1))) if(!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } stop() "throws an error", causing the current function and all functions in the call stack to abort and return nothing. It does not mean to stop now and return a result. Does the function give the correct results if you just leave out the stopifnot line? Bill Dunlap TIBCO Software wdunlap tibco.com<http://tibco.com> On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Eric: I will try and see if I can figure out the issue by debugging as you suggested. I don’t know why my code after stopifnot is not getting executed where I like the code to run the funlp2 function when the if statement is TRUE but when it is false, I like it to keep running until the stopifnot condition is met. When the stopifnot condition is met, I like to get the output from if statement saved. Anyway, I will keep trying. Again, Thanks for your help! Nilesh From: Eric Berger [mailto:ericjber...@gmail.com<mailto:ericjber...@gmail.com>] Sent: Thursday, December 14, 2017 10:29 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: Re: [R] help with recursive function If you are trying to understand why the "stopifnot" condition is met you can replace it by something like: if ( any(dat2$norm_sd >= 1) ) browser() This will put you in a debugging session where you can examine your variables, e.g. > dat$norm_sd HTH, Eric On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com><mailto:ericjber...@gmail.com<mailto:ericjber...@gmail.com>>> wrote: The message is coming from your stopifnot() condition being met. On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com><mailto:nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>>> wrote: Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below. I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not TRUE“ I would appreciate any help. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) stopifnot(!(any(data1$norm_sd >= 1))) if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: DIGHE, NILESH [AG/2362] Sent: Thursday, December 14, 2017 9:01 AM To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com><mailto:ericjber...@gmail.com<mailto:ericjber...@gmail.com>>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org><mailto:r-help@r-project.org<mailto:r-help@r-project.org>>> Subject: RE: [R] help with recursive function Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the l
Re: [R] help with recursive function
Your code contains the lines stopifnot(!(any(data1$norm_sd >= 1))) if(!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } stop() "throws an error", causing the current function and all functions in the call stack to abort and return nothing. It does not mean to stop now and return a result. Does the function give the correct results if you just leave out the stopifnot line? Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Eric: I will try and see if I can figure out the issue by debugging as > you suggested. I don’t know why my code after stopifnot is not getting > executed where I like the code to run the funlp2 function when the if > statement is TRUE but when it is false, I like it to keep running until the > stopifnot condition is met. > > When the stopifnot condition is met, I like to get the output from if > statement saved. > Anyway, I will keep trying. > Again, Thanks for your help! > Nilesh > > From: Eric Berger [mailto:ericjber...@gmail.com] > Sent: Thursday, December 14, 2017 10:29 AM > To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> > Cc: r-help <r-help@r-project.org> > Subject: Re: [R] help with recursive function > > If you are trying to understand why the "stopifnot" condition is met you > can replace it by something like: > > if ( any(dat2$norm_sd >= 1) ) >browser() > > This will put you in a debugging session where you can examine your > variables, e.g. > > > dat$norm_sd > > HTH, > Eric > > > > On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com > <mailto:ericjber...@gmail.com>> wrote: > The message is coming from your stopifnot() condition being met. > > > On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: > Hi, I accidently left out few lines of code from the calclp function. > Updated function is pasted below. > I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is > not TRUE“ > > I would appreciate any help. > Nilesh > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > names(.)), funs(norm = round(scale(.), 3))) > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, sd, na.rm = TRUE), 3) > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > 1, function(x) { > max(abs(x), na.rm = TRUE) > }), 3) > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > "\\.") %>% spread(treatment, value) %>% > mutate(outlier = NA) > stopifnot(!(any(data1$norm_sd >= 1))) > if (!(any(data1$norm_sd >= 1))) { > df1 <- dat1 > return(df1) > } > else { > df2 <- recursive_funlp() > return(df2) > } > } > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > df3 > } > > > From: DIGHE, NILESH [AG/2362] > Sent: Thursday, December 14, 2017 9:01 AM > To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> > Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> > Subject: RE: [R] help with recursive function > > Eric: Thanks for taking time to look into my problem. Despite of making > the change you suggested, I am still getting the same error. I am > wondering if the logic I am using in the stopifnot and if functions is a > problem. > I like the recursive function to stop whenever the norm_sd column has zero > values that are above or equal to 1. Below is the calclp function after the > changes you suggested. > Thanks. Nilesh > > dput(calclp) > function (dataset) > { > dat1 <- funlp1(dataset) > recursive_funlp <- function(dataset = dat1, func = funlp2) { > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >
Re: [R] help with recursive function
Eric: I will try and see if I can figure out the issue by debugging as you suggested. I don’t know why my code after stopifnot is not getting executed where I like the code to run the funlp2 function when the if statement is TRUE but when it is false, I like it to keep running until the stopifnot condition is met. When the stopifnot condition is met, I like to get the output from if statement saved. Anyway, I will keep trying. Again, Thanks for your help! Nilesh From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 10:29 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> Cc: r-help <r-help@r-project.org> Subject: Re: [R] help with recursive function If you are trying to understand why the "stopifnot" condition is met you can replace it by something like: if ( any(dat2$norm_sd >= 1) ) browser() This will put you in a debugging session where you can examine your variables, e.g. > dat$norm_sd HTH, Eric On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> wrote: The message is coming from your stopifnot() condition being met. On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below. I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not TRUE“ I would appreciate any help. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) stopifnot(!(any(data1$norm_sd >= 1))) if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: DIGHE, NILESH [AG/2362] Sent: Thursday, December 14, 2017 9:01 AM To: 'Eric Berger' <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: RE: [R] help with recursive function Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the logic I am using in the stopifnot and if functions is a problem. I like the recursive function to stop whenever the norm_sd column has zero values that are above or equal to 1. Below is the calclp function after the changes you suggested. Thanks. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(!(any(dat2$norm_sd >= 1))) if (!(any(dat2$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 8:17 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: Re: [R] help with recursive function My own typo ... whoops ...
Re: [R] help with recursive function
If you are trying to understand why the "stopifnot" condition is met you can replace it by something like: if ( any(dat2$norm_sd >= 1) ) browser() This will put you in a debugging session where you can examine your variables, e.g. > dat$norm_sd HTH, Eric On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjber...@gmail.com> wrote: > The message is coming from your stopifnot() condition being met. > > > On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com> wrote: > >> Hi, I accidently left out few lines of code from the calclp function. >> Updated function is pasted below. >> >> I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is >> not TRUE“ >> >> >> >> I would appreciate any help. >> >> Nilesh >> >> dput(calclp) >> >> function (dataset) >> >> { >> >> dat1 <- funlp1(dataset) >> >> recursive_funlp <- function(dataset = dat1, func = funlp2) { >> >> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% >> >> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >> >> spread(key = field_rep, value = lp) %>% mutate_at(.vars = >> grep("_", >> >> names(.)), funs(norm = round(scale(.), 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", >> names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, >> >> -norm_sd) %>% separate(key, c("field_rep", "treatment"), >> >> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) >> >> stopifnot(!(any(data1$norm_sd >= 1))) >> >> if (!(any(data1$norm_sd >= 1))) { >> >> df1 <- dat1 >> >> return(df1) >> >> } >> >>else { >> >> df2 <- recursive_funlp() >> >> return(df2) >> >> } >> >> } >> >> df3 <- recursive_funlp(dataset = dat1, func = funlp2) >> >> df3 >> >> } >> >> >> >> >> >> *From:* DIGHE, NILESH [AG/2362] >> *Sent:* Thursday, December 14, 2017 9:01 AM >> *To:* 'Eric Berger' <ericjber...@gmail.com> >> *Cc:* r-help <r-help@r-project.org> >> *Subject:* RE: [R] help with recursive function >> >> >> >> Eric: Thanks for taking time to look into my problem. Despite of making >> the change you suggested, I am still getting the same error. I am >> wondering if the logic I am using in the stopifnot and if functions is a >> problem. >> >> I like the recursive function to stop whenever the norm_sd column has >> zero values that are above or equal to 1. Below is the calclp function >> after the changes you suggested. >> >> Thanks. Nilesh >> >> >> >> dput(calclp) >> >> function (dataset) >> >> { >> >> dat1 <- funlp1(dataset) >> >> recursive_funlp <- function(dataset = dat1, func = funlp2) { >> >> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% >> >> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >> >> spread(key = field_rep, value = lp) %>% mutate_at(.vars = >> grep("_", >> >> names(.)), funs(norm = round(scale(.), 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", >> names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> stopifnot(!(any(dat2$norm_sd >= 1))) >> >> if (!(any(dat2$norm_sd >= 1))) { >> >> df1 <- dat1 >> >> return(df1) >> >> } >> >> else { >> >> df2 <- r
Re: [R] help with recursive function
The message is coming from your stopifnot() condition being met. On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Hi, I accidently left out few lines of code from the calclp function. > Updated function is pasted below. > > I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is > not TRUE“ > > > > I would appreciate any help. > > Nilesh > > dput(calclp) > > function (dataset) > > { > > dat1 <- funlp1(dataset) > > recursive_funlp <- function(dataset = dat1, func = funlp2) { > > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > > names(.)), funs(norm = round(scale(.), 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > > "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) > > stopifnot(!(any(data1$norm_sd >= 1))) > > if (!(any(data1$norm_sd >= 1))) { > > df1 <- dat1 > > return(df1) > > } > >else { > > df2 <- recursive_funlp() > > return(df2) > > } > > } > > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > > df3 > > } > > > > > > *From:* DIGHE, NILESH [AG/2362] > *Sent:* Thursday, December 14, 2017 9:01 AM > *To:* 'Eric Berger' <ericjber...@gmail.com> > *Cc:* r-help <r-help@r-project.org> > *Subject:* RE: [R] help with recursive function > > > > Eric: Thanks for taking time to look into my problem. Despite of making > the change you suggested, I am still getting the same error. I am > wondering if the logic I am using in the stopifnot and if functions is a > problem. > > I like the recursive function to stop whenever the norm_sd column has zero > values that are above or equal to 1. Below is the calclp function after the > changes you suggested. > > Thanks. Nilesh > > > > dput(calclp) > > function (dataset) > > { > > dat1 <- funlp1(dataset) > > recursive_funlp <- function(dataset = dat1, func = funlp2) { > > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > > names(.)), funs(norm = round(scale(.), 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > stopifnot(!(any(dat2$norm_sd >= 1))) > > if (!(any(dat2$norm_sd >= 1))) { > > df1 <- dat1 > > return(df1) > > } > > else { > > df2 <- recursive_funlp() > > return(df2) > > } > > } > > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > > df3 > > } > > > > > > *From:* Eric Berger [mailto:ericjber...@gmail.com <ericjber...@gmail.com>] > > *Sent:* Thursday, December 14, 2017 8:17 AM > *To:* DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> > *Cc:* r-help <r-help@r-project.org> > *Subject:* Re: [R] help with recursive function > > > > My own typo ... whoops ... > > > > !( any(dat2$norm_sd >= 1 )) > > > > > > > > On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjber...@gmail.com> > wrote: > > You seem to have a typo at this expression (and some others like it) > > > > Namely, you write > > > > any(!dat2$norm_sd) >= 1 > > > > when you possibly meant to write > > > > !
Re: [R] help with recursive function
Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below. I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not TRUE“ I would appreciate any help. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) stopifnot(!(any(data1$norm_sd >= 1))) if (!(any(data1$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: DIGHE, NILESH [AG/2362] Sent: Thursday, December 14, 2017 9:01 AM To: 'Eric Berger' <ericjber...@gmail.com> Cc: r-help <r-help@r-project.org> Subject: RE: [R] help with recursive function Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the logic I am using in the stopifnot and if functions is a problem. I like the recursive function to stop whenever the norm_sd column has zero values that are above or equal to 1. Below is the calclp function after the changes you suggested. Thanks. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(!(any(dat2$norm_sd >= 1))) if (!(any(dat2$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 8:17 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> Cc: r-help <r-help@r-project.org<mailto:r-help@r-project.org>> Subject: Re: [R] help with recursive function My own typo ... whoops ... !( any(dat2$norm_sd >= 1 )) On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> wrote: You seem to have a typo at this expression (and some others like it) Namely, you write any(!dat2$norm_sd) >= 1 when you possibly meant to write !( any(dat2$norm_sd) >= 1 ) i.e. I think your ! seems to be in the wrong place. HTH, Eric On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Hi, I need some help with running a recursive function. I like to run funlp2 recursively. When I try to run recursive function in another function named "calclp" I get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". I have never built a recursive function before so having trouble executing it in this case. I would appreciate any help or guidance to resolve this issue. Please see my data and the three functions that I am using below. Please note that calclp is the function I am running and the other two functions are within this calclp function. # code: Test<- calclp(dataset = dat) # calclp function calclp<- function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
Re: [R] help with recursive function
Eric: Thanks for taking time to look into my problem. Despite of making the change you suggested, I am still getting the same error. I am wondering if the logic I am using in the stopifnot and if functions is a problem. I like the recursive function to stop whenever the norm_sd column has zero values that are above or equal to 1. Below is the calclp function after the changes you suggested. Thanks. Nilesh dput(calclp) function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(!(any(dat2$norm_sd >= 1))) if (!(any(dat2$norm_sd >= 1))) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } From: Eric Berger [mailto:ericjber...@gmail.com] Sent: Thursday, December 14, 2017 8:17 AM To: DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com> Cc: r-help <r-help@r-project.org> Subject: Re: [R] help with recursive function My own typo ... whoops ... !( any(dat2$norm_sd >= 1 )) On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjber...@gmail.com<mailto:ericjber...@gmail.com>> wrote: You seem to have a typo at this expression (and some others like it) Namely, you write any(!dat2$norm_sd) >= 1 when you possibly meant to write !( any(dat2$norm_sd) >= 1 ) i.e. I think your ! seems to be in the wrong place. HTH, Eric On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <nilesh.di...@monsanto.com<mailto:nilesh.di...@monsanto.com>> wrote: Hi, I need some help with running a recursive function. I like to run funlp2 recursively. When I try to run recursive function in another function named "calclp" I get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". I have never built a recursive function before so having trouble executing it in this case. I would appreciate any help or guidance to resolve this issue. Please see my data and the three functions that I am using below. Please note that calclp is the function I am running and the other two functions are within this calclp function. # code: Test<- calclp(dataset = dat) # calclp function calclp<- function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(any(!dat2$norm_sd) >= 1) if (any(!dat2$norm_sd) >= 1) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } # funlp1 function funlp1<- function (dataset) { dat2 <- dataset %>% select(field, set, ent_num, rep_num, lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% unite(field_rep, field, rep_num) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spr
Re: [R] help with recursive function
My own typo ... whoops ... !( any(dat2$norm_sd >= 1 )) On Thu, Dec 14, 2017 at 3:43 PM, Eric Bergerwrote: > You seem to have a typo at this expression (and some others like it) > > Namely, you write > > any(!dat2$norm_sd) >= 1 > > when you possibly meant to write > > !( any(dat2$norm_sd) >= 1 ) > > i.e. I think your ! seems to be in the wrong place. > > HTH, > Eric > > > On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] < > nilesh.di...@monsanto.com> wrote: > >> Hi, I need some help with running a recursive function. I like to run >> funlp2 recursively. >> When I try to run recursive function in another function named "calclp" I >> get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". >> >> I have never built a recursive function before so having trouble >> executing it in this case. I would appreciate any help or guidance to >> resolve this issue. Please see my data and the three functions that I am >> using below. >> Please note that calclp is the function I am running and the other two >> functions are within this calclp function. >> >> # code: >> Test<- calclp(dataset = dat) >> >> # calclp function >> >> calclp<- function (dataset) >> >> { >> >> dat1 <- funlp1(dataset) >> >> recursive_funlp <- function(dataset = dat1, func = funlp2) { >> >> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% >> >> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% >> >> spread(key = field_rep, value = lp) %>% mutate_at(.vars = >> grep("_", >> >> names(.)), funs(norm = round(scale(.), 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> stopifnot(any(!dat2$norm_sd) >= 1) >> >> if (any(!dat2$norm_sd) >= 1) { >> >> df1 <- dat1 >> >> return(df1) >> >> } >> >> else { >> >> df2 <- recursive_funlp() >> >> return(df2) >> >> } >> >> } >> >> df3 <- recursive_funlp(dataset = dat1, func = funlp2) >> >> df3 >> >> } >> >> >> # funlp1 function >> >> funlp1<- function (dataset) >> >> { >> >> dat2 <- dataset %>% select(field, set, ent_num, rep_num, >> >> lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% >> >> unite(field_rep, field, rep_num) %>% mutate(field_rep = >> paste(field_rep, >> >> "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% >> >> mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), >> >> 3))) >> >> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, sd, na.rm = TRUE), 3) >> >> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], >> >> 1, function(x) { >> >> max(abs(x), na.rm = TRUE) >> >> }), 3) >> >> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, >> >> -norm_sd) %>% separate(key, c("field_rep", "treatment"), >> >> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) >> >> df_clean <- with(data1, data1[norm_sd < 1, ]) >> >> datD <- with(data1, data1[norm_sd >= 1, ]) >> >> s <- split(datD, datD$uniqueid) >> >> sdf <- lapply(s, function(x) { >> >> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, >> >> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), >> >> x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) >> >> x >> >> }) >> >> sdf2 <- bind_rows(sdf) >> >> all_dat <- bind_rows(df_clean, sdf2) >> >> all_dat >> >> } >> >> >> # funlp2 function >> >> funlp2<-function (dataset) >> >> { >> >> data1 <- dataset >> >> df_clean <- with(data1, data1[norm_sd < 1, ]) >> >> datD <- with(data1, data1[norm_sd >= 1, ]) >> >> s <- split(datD, datD$uniqueid) >> >> sdf <- lapply(s, function(x) { >> >> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, >> >> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), >> >> x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) >> >> x >> >> }) >> >> sdf2 <- bind_rows(sdf) >> >> all_dat <- bind_rows(df_clean, sdf2) >> >> all_dat >> >> } >> >> >> # dataset >> dput(dat) >> structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", >> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
Re: [R] help with recursive function
You seem to have a typo at this expression (and some others like it) Namely, you write any(!dat2$norm_sd) >= 1 when you possibly meant to write !( any(dat2$norm_sd) >= 1 ) i.e. I think your ! seems to be in the wrong place. HTH, Eric On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] < nilesh.di...@monsanto.com> wrote: > Hi, I need some help with running a recursive function. I like to run > funlp2 recursively. > When I try to run recursive function in another function named "calclp" I > get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". > > I have never built a recursive function before so having trouble executing > it in this case. I would appreciate any help or guidance to resolve this > issue. Please see my data and the three functions that I am using below. > Please note that calclp is the function I am running and the other two > functions are within this calclp function. > > # code: > Test<- calclp(dataset = dat) > > # calclp function > > calclp<- function (dataset) > > { > > dat1 <- funlp1(dataset) > > recursive_funlp <- function(dataset = dat1, func = funlp2) { > > dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% > > mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% > > spread(key = field_rep, value = lp) %>% mutate_at(.vars = > grep("_", > > names(.)), funs(norm = round(scale(.), 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > stopifnot(any(!dat2$norm_sd) >= 1) > > if (any(!dat2$norm_sd) >= 1) { > > df1 <- dat1 > > return(df1) > > } > > else { > > df2 <- recursive_funlp() > > return(df2) > > } > > } > > df3 <- recursive_funlp(dataset = dat1, func = funlp2) > > df3 > > } > > > # funlp1 function > > funlp1<- function (dataset) > > { > > dat2 <- dataset %>% select(field, set, ent_num, rep_num, > > lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% > > unite(field_rep, field, rep_num) %>% mutate(field_rep = > paste(field_rep, > > "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% > > mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), > > 3))) > > dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, sd, na.rm = TRUE), 3) > > dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], > > 1, function(x) { > > max(abs(x), na.rm = TRUE) > > }), 3) > > data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, > > -norm_sd) %>% separate(key, c("field_rep", "treatment"), > > "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) > > df_clean <- with(data1, data1[norm_sd < 1, ]) > > datD <- with(data1, data1[norm_sd >= 1, ]) > > s <- split(datD, datD$uniqueid) > > sdf <- lapply(s, function(x) { > > data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, > > ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), > > x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) > > x > > }) > > sdf2 <- bind_rows(sdf) > > all_dat <- bind_rows(df_clean, sdf2) > > all_dat > > } > > > # funlp2 function > > funlp2<-function (dataset) > > { > > data1 <- dataset > > df_clean <- with(data1, data1[norm_sd < 1, ]) > > datD <- with(data1, data1[norm_sd >= 1, ]) > > s <- split(datD, datD$uniqueid) > > sdf <- lapply(s, function(x) { > > data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, > > ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), > > x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) > > x > > }) > > sdf2 <- bind_rows(sdf) > > all_dat <- bind_rows(df_clean, sdf2) > > all_dat > > } > > > # dataset > dput(dat) > structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", > "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", > "OL01",
[R] help with recursive function
Hi, I need some help with running a recursive function. I like to run funlp2 recursively. When I try to run recursive function in another function named "calclp" I get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE". I have never built a recursive function before so having trouble executing it in this case. I would appreciate any help or guidance to resolve this issue. Please see my data and the three functions that I am using below. Please note that calclp is the function I am running and the other two functions are within this calclp function. # code: Test<- calclp(dataset = dat) # calclp function calclp<- function (dataset) { dat1 <- funlp1(dataset) recursive_funlp <- function(dataset = dat1, func = funlp2) { dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) stopifnot(any(!dat2$norm_sd) >= 1) if (any(!dat2$norm_sd) >= 1) { df1 <- dat1 return(df1) } else { df2 <- recursive_funlp() return(df2) } } df3 <- recursive_funlp(dataset = dat1, func = funlp2) df3 } # funlp1 function funlp1<- function (dataset) { dat2 <- dataset %>% select(field, set, ent_num, rep_num, lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>% unite(field_rep, field, rep_num) %>% mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.), 3))) dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, sd, na.rm = TRUE), 3) dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))], 1, function(x) { max(abs(x), na.rm = TRUE) }), 3) data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max, -norm_sd) %>% separate(key, c("field_rep", "treatment"), "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA) df_clean <- with(data1, data1[norm_sd < 1, ]) datD <- with(data1, data1[norm_sd >= 1, ]) s <- split(datD, datD$uniqueid) sdf <- lapply(s, function(x) { data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) x }) sdf2 <- bind_rows(sdf) all_dat <- bind_rows(df_clean, sdf2) all_dat } # funlp2 function funlp2<-function (dataset) { data1 <- dataset df_clean <- with(data1, data1[norm_sd < 1, ]) datD <- with(data1, data1[norm_sd >= 1, ]) s <- split(datD, datD$uniqueid) sdf <- lapply(s, function(x) { data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA, ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")), x$lp <- with(x, ifelse(outlier == "yes", NA, lp))) x }) sdf2 <- bind_rows(sdf) all_dat <- bind_rows(df_clean, sdf2) all_dat } # dataset dput(dat) structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1",
Re: [R] Help with optim function in R, please?
I haven't tried your code, but I've seen far too many attempts like this where there is no simple call to the function with starting parameters to see if a sensible answer is returned. If you get NaN or Inf or ... that isn't sensible, then you know it is not a good idea to try further. Beyond this, you don't have gradients. I think I would try nmkb (an updated Nelder-Mead style code that allows for bounds) from dfoptim package. Note that you cannot start on the bounds with this code. And it won't be terribly efficient, but it will -- if you ask for intermediate results -- give you some idea about your function. If everything then working reasonably, you can try L-BFGS-B, possibly with "better" starting parameters. However, I'm fairly certain you will find something to fix before then. It really does pay to be very pedestrian in using optimization software. I've been at this game (and note I wrote 3 of the 5 routines in optim(), which now all have replacements!) and I am constantly reminded in my own work to ALWAYS run some simple tests with the objective function and, if I have them, the gradients. JN On 2017-08-06 11:11 AM, fadeh Alanazi wrote: Hi all, Many thank in advance for helping me. I tried to fit Expectation Maximization algorithm for mixture data. I must used one of numerical method to maximize my function. I built my code but I do not know how to make the optim function run over a different value of the parameters. That is, For E-step I need to get the value of mixture weights based on the current (initial) values of the parameter of the density. Then, multiple the weight by the logliklihood function and maximize it (M-step) Then, I would like to take the new values of the parameter (from M-step) and plug it in the weight, to get a new value of the weight. Then, iterate till converges. I tried the following code, but it does not work. library(copula) library(VineCopula) ## to generate the data set.seed(123) cp <- mixCopula(list(frankCopula(4),claytonCopula(2))) cop <- rCopula(100,cp) x <- pobs(cop) ## this is the data ## my function including optim function myfunc <- function(data,copula=list(frankCopula(4,dim=2), claytonCopula(0.5,dim=2)),maxit=200){ # copula[[1]]@parameters <- par[1] # copula[[2]]@parameters <- par[2] optim_1 <- function(par, data.=data, copula.=copula){ copula[[1]]@parameters <- par[1] copula[[2]]@parameters <- par[2] tau_1 <- par[3]*dCopula(data,copula[[1]],log = F) tau_2 <- par[4]*dCopula(data,copula[[2]],log=F) Tau_1 <- tau_1 / sum(tau_1 + tau_2) Tau_2 <- tau_2 / sum(tau_1 + tau_2) up_pi1 <- sum(Tau_1)/ 100 up_pi2 <- sum(Tau_2) / 100 # Tau <- c(Tau_1, Tau_2) ll <- (-sum(Tau_1*dCopula(data,copula[[1]],log=T))) #ll_1 <- -sum(Tau_2*dCopula(data,copula[[2]],log=T)) if (is.finite(ll)) { return(ll) } else { if (is.na(ll)) { message(par) } message(ll) return(-3^305) } } xy <- optim(par=c(2,0.5,0.2,0.2),fn=optim_1,method="L-BFGS-B",control = list(maxit= 200, trace=1),lower= c(copula[[1]]@param.lowbnd, copula[[2]]@param.lowbnd,0,0), upper = c(copula[[1]]@param.upbnd, copula[[2]]@param.upbnd,1,1)) return(xy$par) } Any help please? Regards, Fadhah [[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.
[R] Help with optim function in R, please?
Hi all, Many thank in advance for helping me. I tried to fit Expectation Maximization algorithm for mixture data. I must used one of numerical method to maximize my function. I built my code but I do not know how to make the optim function run over a different value of the parameters. That is, For E-step I need to get the value of mixture weights based on the current (initial) values of the parameter of the density. Then, multiple the weight by the logliklihood function and maximize it (M-step) Then, I would like to take the new values of the parameter (from M-step) and plug it in the weight, to get a new value of the weight. Then, iterate till converges. I tried the following code, but it does not work. library(copula) library(VineCopula) ## to generate the data set.seed(123) cp <- mixCopula(list(frankCopula(4),claytonCopula(2))) cop <- rCopula(100,cp) x <- pobs(cop) ## this is the data ## my function including optim function myfunc <- function(data,copula=list(frankCopula(4,dim=2), claytonCopula(0.5,dim=2)),maxit=200){ # copula[[1]]@parameters <- par[1] # copula[[2]]@parameters <- par[2] optim_1 <- function(par, data.=data, copula.=copula){ copula[[1]]@parameters <- par[1] copula[[2]]@parameters <- par[2] tau_1 <- par[3]*dCopula(data,copula[[1]],log = F) tau_2 <- par[4]*dCopula(data,copula[[2]],log=F) Tau_1 <- tau_1 / sum(tau_1 + tau_2) Tau_2 <- tau_2 / sum(tau_1 + tau_2) up_pi1 <- sum(Tau_1)/ 100 up_pi2 <- sum(Tau_2) / 100 # Tau <- c(Tau_1, Tau_2) ll <- (-sum(Tau_1*dCopula(data,copula[[1]],log=T))) #ll_1 <- -sum(Tau_2*dCopula(data,copula[[2]],log=T)) if (is.finite(ll)) { return(ll) } else { if (is.na(ll)) { message(par) } message(ll) return(-3^305) } } xy <- optim(par=c(2,0.5,0.2,0.2),fn=optim_1,method="L-BFGS-B",control = list(maxit= 200, trace=1),lower= c(copula[[1]]@param.lowbnd, copula[[2]]@param.lowbnd,0,0), upper = c(copula[[1]]@param.upbnd, copula[[2]]@param.upbnd,1,1)) return(xy$par) } Any help please? Regards, Fadhah [[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] Help with improveProb function in Hmisc in R
Please note that Kirsten is cross-posting to stats.stackexchange.com creating extra work for everyone. -- Frank E Harrell Jr Professor and Chairman School of Medicine Department of *Biostatistics* *Vanderbilt University* [[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 with improveProb function in Hmisc in R
Please bear with me, I am very new to R. My question is regarding the use of the improveProb function in the Hmisc package. I have two logistic models, the only difference being that the second model contains my novel marker of interest. I am trying to calculate NRI and IDI to compare models. I have the PredRisks for both models - PredRisk1 and PredRisk2, and my outcome is disease 0/1. How do I define this in R in order to run improveProb(x1, x2, y)? Many thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Help-with-improveProb-function-in-Hmisc-in-R-tp4713004.html Sent from the R help mailing list archive at Nabble.com. __ 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] Help with improveProb function in Hmisc in R
On Oct 1, 2015, at 2:26 AM, kirsada wrote: > Please bear with me, I am very new to R. > > My question is regarding the use of the improveProb function in the Hmisc > package. I have two logistic models, the only difference being that the > second model contains my novel marker of interest. I am trying to calculate > NRI and IDI to compare models. > > I have the PredRisks for both models - PredRisk1 and PredRisk2, and my > outcome is disease 0/1. How do I define this in R in order to run > > improveProb(x1, x2, y)? If you are saying that you have two numerical values each of length one, than I do not think you can pass those to a function that expects raw data. If your "PredRisk" variables are vectors of the same length as y and having a range of [0,1] as befits a probability, then what problems are you experiencing? -- 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.
Re: [R] Help with abs function
Hi, Just do the following: tran-c(7.2) tgrid-c(7.1,7.4,7.3,7.1,7.3) tgrid-tgrid-tran tgrid [1] -0.1 0.2 0.1 -0.1 0.1 abs(tgrid[tgrid0.1]) [1] 0.2 Andrés El 12/06/2015, a las 11:01, Jeff Newmiller jdnew...@dcn.davis.ca.us escribió: FAQ 7.31 --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On June 12, 2015 8:39:48 AM PDT, Nelson, Gary (MISC) gary.nel...@state.ma.us wrote: I have come across some odd behavior (to me) using the abs() function that I wonder if anyone can explain. Using R version 3.2.0, I created a vector of absolute values using the following code: tran-c(7.2) tgrid-c(7.1,7.4,7.3,7.1,7.3) dgrid-abs(tgrid-tran) dgrid [1] 0.1 0.2 0.1 0.1 0.1 When I tried to extract just the rows with values0.1, I get dgrid[dgrid0.1] [1] 0.1 0.2 0.1 There should be only 1 value extracted. However, if I enter the values by hand bgrid-c(0.1,0.2,0.1,0.1,0.1) bgrid [1] 0.1 0.2 0.1 0.1 0.1 bgrid[bgrid0.1] [1] 0.2 The result is correct. So why is this happening? I did explore a little bit and found as.character(dgrid) [1] 0.101 0.2 [3] 0.0996 0.101 [5] 0.0996 which shows the absolute values of the negative differences are slightly greater than the difference of 0.1 Is this normal behavior for the abs() function? Thanks, Gary Nelson [[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. __ 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] Help with abs function
FAQ 7.31 --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On June 12, 2015 8:39:48 AM PDT, Nelson, Gary (MISC) gary.nel...@state.ma.us wrote: I have come across some odd behavior (to me) using the abs() function that I wonder if anyone can explain. Using R version 3.2.0, I created a vector of absolute values using the following code: tran-c(7.2) tgrid-c(7.1,7.4,7.3,7.1,7.3) dgrid-abs(tgrid-tran) dgrid [1] 0.1 0.2 0.1 0.1 0.1 When I tried to extract just the rows with values0.1, I get dgrid[dgrid0.1] [1] 0.1 0.2 0.1 There should be only 1 value extracted. However, if I enter the values by hand bgrid-c(0.1,0.2,0.1,0.1,0.1) bgrid [1] 0.1 0.2 0.1 0.1 0.1 bgrid[bgrid0.1] [1] 0.2 The result is correct. So why is this happening? I did explore a little bit and found as.character(dgrid) [1] 0.101 0.2 [3] 0.0996 0.101 [5] 0.0996 which shows the absolute values of the negative differences are slightly greater than the difference of 0.1 Is this normal behavior for the abs() function? Thanks, Gary Nelson [[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] Help with abs function
Please read R FAQ 7.31. On Fri, Jun 12, 2015 at 11:39 AM, Nelson, Gary (MISC) gary.nel...@state.ma.us wrote: I have come across some odd behavior (to me) using the abs() function that I wonder if anyone can explain. Using R version 3.2.0, I created a vector of absolute values using the following code: tran-c(7.2) tgrid-c(7.1,7.4,7.3,7.1,7.3) dgrid-abs(tgrid-tran) dgrid [1] 0.1 0.2 0.1 0.1 0.1 When I tried to extract just the rows with values0.1, I get dgrid[dgrid0.1] [1] 0.1 0.2 0.1 There should be only 1 value extracted. However, if I enter the values by hand bgrid-c(0.1,0.2,0.1,0.1,0.1) bgrid [1] 0.1 0.2 0.1 0.1 0.1 bgrid[bgrid0.1] [1] 0.2 The result is correct. So why is this happening? I did explore a little bit and found as.character(dgrid) [1] 0.101 0.2 [3] 0.0996 0.101 [5] 0.0996 which shows the absolute values of the negative differences are slightly greater than the difference of 0.1 Is this normal behavior for the abs() function? Thanks, Gary Nelson -- Sarah Goslee http://www.functionaldiversity.org __ 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 with abs function
I have come across some odd behavior (to me) using the abs() function that I wonder if anyone can explain. Using R version 3.2.0, I created a vector of absolute values using the following code: tran-c(7.2) tgrid-c(7.1,7.4,7.3,7.1,7.3) dgrid-abs(tgrid-tran) dgrid [1] 0.1 0.2 0.1 0.1 0.1 When I tried to extract just the rows with values0.1, I get dgrid[dgrid0.1] [1] 0.1 0.2 0.1 There should be only 1 value extracted. However, if I enter the values by hand bgrid-c(0.1,0.2,0.1,0.1,0.1) bgrid [1] 0.1 0.2 0.1 0.1 0.1 bgrid[bgrid0.1] [1] 0.2 The result is correct. So why is this happening? I did explore a little bit and found as.character(dgrid) [1] 0.101 0.2 [3] 0.0996 0.101 [5] 0.0996 which shows the absolute values of the negative differences are slightly greater than the difference of 0.1 Is this normal behavior for the abs() function? Thanks, Gary Nelson [[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] Help with pedigree() function in kinship2
Your problem is that PatientID, FatherID, MotherID are factors. The authors of kinship2 (myself and Jason) simply never thought of someone doing this. Yes, that is an oversight. We will correct it by adding some more checks and balances. For now, turn your id variables into character or numeric. Terry Therneau On 05/13/2015 05:00 AM, r-help-requ...@r-project.org wrote: Dear R-help, I am interested in plotting some pedigrees and came across the kinship2 package. What follows is an example of the pedigrees I am working with Now, when running ## check package availability if(!require(kinship2)) install.packages('kinship2') require(kinship2) ## data to plot d - structure(list(FamilyID = c(1, 1, 1, 1, 1, 1, 1, 1, 1), PatientID = structure(c(2L, 3L, 5L, 11L, 12L, 15L, 16L, 17L, 6L), .Label = c( 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 18, 20, 23, 24, 25, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 43, 45, 50, 62, 63, 64, 65, 66, 67, 85, 88), class = factor), FatherID = structure(c(1L, 1L, 6L, 1L, 5L, 6L, 1L, 7L, 6L), .Label = c(0, 1, 10, 11, 13, 2, 23, 27, 28, 3, 33, 34, 35, 38, 5, 62, 64, 66, 9), class = factor), MotherID = structure(c(1L, 1L, 7L, 1L, 14L, 7L, 1L, 5L, 7L), .Label = c(0, 10, 18, 2, 24, 29, 3, 30, 33, 34, 39, 4, 43, 5, 6, 63, 65, 9), class = factor), Sex = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c(Female, Male), class = factor), AffectionStatus = structure(c(1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c(1, 2), class = factor)), .Names = c(FamilyID, PatientID, FatherID, MotherID, Sex, AffectionStatus ), row.names = c(NA, 9L), class = data.frame) ## plotting ped - with(d, pedigree(PatientID, FatherID, MotherID, Sex, affected = AffectionStatus, famid = FamilyID)) ## Error in pedigree(PatientID, FatherID, MotherID, Sex, affected = AffectionStatus, : ##Value of 'dadid' not found in the id list 1/0 1/0 1/2 1/0 1/2 I get an error. My sessionInfo() is at the end. I was wondering if someone could help me to dissect what the cause of this error is and how it can be fixed. Thank you very much for your help. Best regards, Jorge Velez.- __ 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] help with by function
Jean: Thanks a lot!! The changes you made to the code gave me what I needed. I truly appreciate your time in correcting the code. Nilesh From: Adams, Jean [mailto:jvad...@usgs.gov] Sent: Tuesday, May 12, 2015 2:14 PM To: DIGHE, NILESH [AG/2362] Cc: r-help@r-project.org Subject: Re: [R] help with by function Nilesh, I found a couple errors in your code. First, in your by() statement you have a function to operate on the selected subset of data, which you refer to as x but then, in your aov statement you refer to data_set not x Second, your funC() statement is a function of trait_names but to make sure that the name of the variable is included in the formula, I changed this to a character variable. Give the code below a try and see if it works for you. Jean library(agricolae) library(dplyr) funC - function(trait_names){ by(data_set, data_set$Isopair, function(x) { mod - aov(formula(paste(trait_names, ~ STGgroup*Field + Rep%in%Field)), data=x) out - HSD.test(mod, STGgroup, group=TRUE, console=TRUE) dfout - arrange(data.frame(out$groups), desc(trt)) }) } funC(Trait1) On Tue, May 12, 2015 at 1:03 PM, DIGHE, NILESH [AG/2362] nilesh.di...@monsanto.commailto:nilesh.di...@monsanto.com wrote: Hi, I have an anonymous function called function(x) that will run anova, run HSD.test on the model, and then sort the results. I am passing this anonymous function to the by function to get results by Isopair factor which is my index variable. Since I want to run the anova on multiple dependent variables including Trait1 Trait2, I am calling the dependent variable, trait_names in the model and then passing the by function to another function called funC which takes the trait_names as an argument to execute. After I execute the funC(data_set$trait1), I am getting the results by Isopair but the results for the two Isopairs (Isopair-A Isopair-B) are the same which I know is not correct. It looks like the data is not getting split by Isopairs and so ALL data is used in anova for both Isopairs. Any help in modifying function, funC or any other ways to achieve the desired outcome will be highly appreciated. Thanks. Nilesh R code, data set, and session info is pasted below. R code: library(agricolae) funC- function(trait_names){ by(data_set, data_set$Isopair,function(x){ mod- aov(trait_names~ STGgroup*Field + Rep%in%Field, data=data_set) out-HSD.test(mod,STGgroup,group=TRUE,console=TRUE) dfout- arrange(data.frame(out$groups),desc(trt)) }) } Results: ##execute funC function for Trait1 Trait2 funC(data_set$Trait1) data_set$Isopair: Isopair-A trtmeans M 1 STG 776.9167 a 2 Non-STG 779.0833 a --- data_set$Isopair: Isopair-B trtmeans M 1 STG 776.9167 a 2 Non-STG 779.0833 a Data: data_set- structure(list(Field = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(LML6, TZL2), class = factor), Isopair = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(Isopair-A, Isopair-B ), class = factor), STGgroup = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c(Non-STG, STG), class = factor), Rep = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c(Rep1, Rep2, Rep3), class = factor), Trait1 = c(686L, 641L, 642L, 727L, 619L, 562L, 808L, 739L, 744L, 873L, 797L, 868L, 782L, 783L, 675L, 713L, 762L, 641L, 1009L, 995L, 845L, 1186L, 912L, 663L), Trait2 = c(45L, 65L, 70L, 35L, 20L, 80L, 70L, 65L, 70L, 20L, 30L, 35L, 40L, 55L, 35L, 40L, 35L, 40L, 40L, 35L, 25L, 40L, 35L, 25L)), .Names = c(Field, Isopair, STGgroup, Rep, Trait1, Trait2), class = data.frame, row.names = c(NA, -24L)) Session info: R version 3.1.3 (2015-03-09) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] gridExtra_0.9.1 Hmisc_3.16-0Formula_1.2-1 survival_2.38-1 caret_6.0-41ggplot2_1.0.1 [7] lattice_0.20-30 MASS_7.3-39 dplyr_0.4.1 agricolae_1.2-1 loaded via a namespace (and not attached): [1] acepack_1.3-3.3 assertthat_0.1 boot_1.3-15 BradleyTerry2_1.0-6 [5] brglm_0.5-9 car_2.0-25 cluster_2.0.1 coda_0.17-1 [9] codetools_0.2-10colorspace_1.2-6combinat_0.0-8 DBI_0.3.1 [13] deldir_0.1-9digest_0.6.8foreach_1.4.2 foreign_0.8
[R] help with by function
Hi, I have an anonymous function called function(x) that will run anova, run HSD.test on the model, and then sort the results. I am passing this anonymous function to the by function to get results by Isopair factor which is my index variable. Since I want to run the anova on multiple dependent variables including Trait1 Trait2, I am calling the dependent variable, trait_names in the model and then passing the by function to another function called funC which takes the trait_names as an argument to execute. After I execute the funC(data_set$trait1), I am getting the results by Isopair but the results for the two Isopairs (Isopair-A Isopair-B) are the same which I know is not correct. It looks like the data is not getting split by Isopairs and so ALL data is used in anova for both Isopairs. Any help in modifying function, funC or any other ways to achieve the desired outcome will be highly appreciated. Thanks. Nilesh R code, data set, and session info is pasted below. R code: library(agricolae) funC- function(trait_names){ by(data_set, data_set$Isopair,function(x){ mod- aov(trait_names~ STGgroup*Field + Rep%in%Field, data=data_set) out-HSD.test(mod,STGgroup,group=TRUE,console=TRUE) dfout- arrange(data.frame(out$groups),desc(trt)) }) } Results: ##execute funC function for Trait1 Trait2 funC(data_set$Trait1) data_set$Isopair: Isopair-A trtmeans M 1 STG 776.9167 a 2 Non-STG 779.0833 a --- data_set$Isopair: Isopair-B trtmeans M 1 STG 776.9167 a 2 Non-STG 779.0833 a Data: data_set- structure(list(Field = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(LML6, TZL2), class = factor), Isopair = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(Isopair-A, Isopair-B ), class = factor), STGgroup = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c(Non-STG, STG), class = factor), Rep = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c(Rep1, Rep2, Rep3), class = factor), Trait1 = c(686L, 641L, 642L, 727L, 619L, 562L, 808L, 739L, 744L, 873L, 797L, 868L, 782L, 783L, 675L, 713L, 762L, 641L, 1009L, 995L, 845L, 1186L, 912L, 663L), Trait2 = c(45L, 65L, 70L, 35L, 20L, 80L, 70L, 65L, 70L, 20L, 30L, 35L, 40L, 55L, 35L, 40L, 35L, 40L, 40L, 35L, 25L, 40L, 35L, 25L)), .Names = c(Field, Isopair, STGgroup, Rep, Trait1, Trait2), class = data.frame, row.names = c(NA, -24L)) Session info: R version 3.1.3 (2015-03-09) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] gridExtra_0.9.1 Hmisc_3.16-0Formula_1.2-1 survival_2.38-1 caret_6.0-41ggplot2_1.0.1 [7] lattice_0.20-30 MASS_7.3-39 dplyr_0.4.1 agricolae_1.2-1 loaded via a namespace (and not attached): [1] acepack_1.3-3.3 assertthat_0.1 boot_1.3-15 BradleyTerry2_1.0-6 [5] brglm_0.5-9 car_2.0-25 cluster_2.0.1 coda_0.17-1 [9] codetools_0.2-10colorspace_1.2-6combinat_0.0-8 DBI_0.3.1 [13] deldir_0.1-9digest_0.6.8foreach_1.4.2 foreign_0.8-63 [17] gtable_0.1.2gtools_3.4.1iterators_1.0.7 klaR_0.6-12 [21] latticeExtra_0.6-26 lazyeval_0.1.10 LearnBayes_2.15 lme4_1.1-7 [25] magrittr_1.5Matrix_1.1-5mgcv_1.8-4 minqa_1.2.4 [29] munsell_0.4.2 nlme_3.1-120nloptr_1.0.4nnet_7.3-9 [33] parallel_3.1.3 pbkrtest_0.4-2 plyr_1.8.1 proto_0.3-10 [37] quantreg_5.11 RColorBrewer_1.1-2 Rcpp_0.11.6 reshape2_1.4.1 [41] rpart_4.1-9 scales_0.2.4sp_1.0-17 SparseM_1.6 [45] spdep_0.5-88splines_3.1.3 stringr_0.6.2 tools_3.1.3 This e-mail message may contain privileged and/or confidential information, and is intended to be received only by persons entitled to receive such information. If you have received this e-mail in error, please notify the sender immediately. Please delete it and all attachments from any servers, hard drives or any other media. Other use of this e-mail by you is strictly prohibited. All e-mails and attachments sent and received are subject to monitoring, reading and archival by Monsanto, including its subsidiaries. The recipient of this e-mail is solely responsible for checking for the presence of Viruses or
Re: [R] help with by function
Nilesh, I found a couple errors in your code. First, in your by() statement you have a function to operate on the selected subset of data, which you refer to as x but then, in your aov statement you refer to data_set not x Second, your funC() statement is a function of trait_names but to make sure that the name of the variable is included in the formula, I changed this to a character variable. Give the code below a try and see if it works for you. Jean library(agricolae) library(dplyr) funC - function(trait_names){ by(data_set, data_set$Isopair, function(x) { mod - aov(formula(paste(trait_names, ~ STGgroup*Field + Rep%in%Field)), data=x) out - HSD.test(mod, STGgroup, group=TRUE, console=TRUE) dfout - arrange(data.frame(out$groups), desc(trt)) }) } funC(Trait1) On Tue, May 12, 2015 at 1:03 PM, DIGHE, NILESH [AG/2362] nilesh.di...@monsanto.com wrote: Hi, I have an anonymous function called function(x) that will run anova, run HSD.test on the model, and then sort the results. I am passing this anonymous function to the by function to get results by Isopair factor which is my index variable. Since I want to run the anova on multiple dependent variables including Trait1 Trait2, I am calling the dependent variable, trait_names in the model and then passing the by function to another function called funC which takes the trait_names as an argument to execute. After I execute the funC(data_set$trait1), I am getting the results by Isopair but the results for the two Isopairs (Isopair-A Isopair-B) are the same which I know is not correct. It looks like the data is not getting split by Isopairs and so ALL data is used in anova for both Isopairs. Any help in modifying function, funC or any other ways to achieve the desired outcome will be highly appreciated. Thanks. Nilesh R code, data set, and session info is pasted below. R code: library(agricolae) funC- function(trait_names){ by(data_set, data_set$Isopair,function(x){ mod- aov(trait_names~ STGgroup*Field + Rep%in%Field, data=data_set) out-HSD.test(mod,STGgroup,group=TRUE,console=TRUE) dfout- arrange(data.frame(out$groups),desc(trt)) }) } Results: ##execute funC function for Trait1 Trait2 funC(data_set$Trait1) data_set$Isopair: Isopair-A trtmeans M 1 STG 776.9167 a 2 Non-STG 779.0833 a --- data_set$Isopair: Isopair-B trtmeans M 1 STG 776.9167 a 2 Non-STG 779.0833 a Data: data_set- structure(list(Field = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(LML6, TZL2), class = factor), Isopair = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(Isopair-A, Isopair-B ), class = factor), STGgroup = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c(Non-STG, STG), class = factor), Rep = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c(Rep1, Rep2, Rep3), class = factor), Trait1 = c(686L, 641L, 642L, 727L, 619L, 562L, 808L, 739L, 744L, 873L, 797L, 868L, 782L, 783L, 675L, 713L, 762L, 641L, 1009L, 995L, 845L, 1186L, 912L, 663L), Trait2 = c(45L, 65L, 70L, 35L, 20L, 80L, 70L, 65L, 70L, 20L, 30L, 35L, 40L, 55L, 35L, 40L, 35L, 40L, 40L, 35L, 25L, 40L, 35L, 25L)), .Names = c(Field, Isopair, STGgroup, Rep, Trait1, Trait2), class = data.frame, row.names = c(NA, -24L)) Session info: R version 3.1.3 (2015-03-09) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] gridExtra_0.9.1 Hmisc_3.16-0Formula_1.2-1 survival_2.38-1 caret_6.0-41ggplot2_1.0.1 [7] lattice_0.20-30 MASS_7.3-39 dplyr_0.4.1 agricolae_1.2-1 loaded via a namespace (and not attached): [1] acepack_1.3-3.3 assertthat_0.1 boot_1.3-15 BradleyTerry2_1.0-6 [5] brglm_0.5-9 car_2.0-25 cluster_2.0.1 coda_0.17-1 [9] codetools_0.2-10colorspace_1.2-6combinat_0.0-8 DBI_0.3.1 [13] deldir_0.1-9digest_0.6.8foreach_1.4.2 foreign_0.8-63 [17] gtable_0.1.2gtools_3.4.1iterators_1.0.7 klaR_0.6-12 [21] latticeExtra_0.6-26 lazyeval_0.1.10 LearnBayes_2.15 lme4_1.1-7 [25] magrittr_1.5Matrix_1.1-5mgcv_1.8-4 minqa_1.2.4 [29] munsell_0.4.2 nlme_3.1-120nloptr_1.0.4
[R] Help with pedigree() function in kinship2
Dear R-help, I am interested in plotting some pedigrees and came across the kinship2 package. What follows is an example of the pedigrees I am working with. Now, when running ## check package availability if(!require(kinship2)) install.packages('kinship2') require(kinship2) ## data to plot d - structure(list(FamilyID = c(1, 1, 1, 1, 1, 1, 1, 1, 1), PatientID = structure(c(2L, 3L, 5L, 11L, 12L, 15L, 16L, 17L, 6L), .Label = c( 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 18, 20, 23, 24, 25, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 43, 45, 50, 62, 63, 64, 65, 66, 67, 85, 88), class = factor), FatherID = structure(c(1L, 1L, 6L, 1L, 5L, 6L, 1L, 7L, 6L), .Label = c(0, 1, 10, 11, 13, 2, 23, 27, 28, 3, 33, 34, 35, 38, 5, 62, 64, 66, 9), class = factor), MotherID = structure(c(1L, 1L, 7L, 1L, 14L, 7L, 1L, 5L, 7L), .Label = c(0, 10, 18, 2, 24, 29, 3, 30, 33, 34, 39, 4, 43, 5, 6, 63, 65, 9), class = factor), Sex = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c(Female, Male), class = factor), AffectionStatus = structure(c(1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c(1, 2), class = factor)), .Names = c(FamilyID, PatientID, FatherID, MotherID, Sex, AffectionStatus ), row.names = c(NA, 9L), class = data.frame) ## plotting ped - with(d, pedigree(PatientID, FatherID, MotherID, Sex, affected = AffectionStatus, famid = FamilyID)) ## Error in pedigree(PatientID, FatherID, MotherID, Sex, affected = AffectionStatus, : ##Value of 'dadid' not found in the id list 1/0 1/0 1/2 1/0 1/2 I get an error. My sessionInfo() is at the end. I was wondering if someone could help me to dissect what the cause of this error is and how it can be fixed. Thank you very much for your help. Best regards, Jorge Velez.- R sessionInfo() R version 3.2.0 Patched (2015-05-04 r68320) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.10.3 (Yosemite) locale: [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets parallel compiler methods [9] base other attached packages: [1] kinship2_1.6.0 quadprog_1.5-5 Matrix_1.2-0 readxl_0.1.0 loaded via a namespace (and not attached): [1] Rcpp_0.11.6 grid_3.2.0 lattice_0.20-31 [[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] help please metro_hastings function
Hi, I have used this function before successfully. I could help you if you could provide your code. Thanks Regards,Arnab From: hms Dreams cute_loo...@hotmail.com To: r-help@r-project.org r-help@r-project.org Sent: Saturday, February 14, 2015 6:27 AM Subject: [R] help please metro_hastings function Hi :)anybody can help me please I'm trying to use Metro_Hastings ( MHadaptive package)the proplem is: How can I know the covariance matrix( prop_sigma ) to enter it in Metro_Hastings: mcmc_r=Metro_Hastings(li_func=baysianlog, pars=c(1,1,1), prop_sigma =NULL,par_names=c('alpha','gamma','delta'),data=x ) its gave me an error , I must enter the cov matrix but I don't know how to calculate it, somebody told me to wrote the function without prop_sigma but its also gave me an error what can I do?? Thank you,Sara [[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] help please metro_hastings function
It looks like you posted in HTML and the result are garbbled. ONly post in plain text. Also it might help to read one or both of these https://github.com/hadley/devtools/wiki/Reproducibility http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example John Kane Kingston ON Canada -Original Message- From: cute_loo...@hotmail.com Sent: Sat, 14 Feb 2015 15:27:58 +0300 To: r-help@r-project.org Subject: [R] help please metro_hastings function Hi :)anybody can help me please I'm trying to use Metro_Hastings ( MHadaptive package)the proplem is: How can I know the covariance matrix( prop_sigma ) to enter it in Metro_Hastings: mcmc_r=Metro_Hastings(li_func=baysianlog, pars=c(1,1,1), prop_sigma =NULL,par_names=c('alpha','gamma','delta'),data=x ) its gave me an error , I must enter the cov matrix but I don't know how to calculate it, somebody told me to wrote the function without prop_sigma but its also gave me an error what can I do?? Thank you,Sara [[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. FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks orcas on your desktop! __ 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 please metro_hastings function
Hi :)anybody can help me please I'm trying to use Metro_Hastings ( MHadaptive package)the proplem is: How can I know the covariance matrix( prop_sigma ) to enter it in Metro_Hastings: mcmc_r=Metro_Hastings(li_func=baysianlog, pars=c(1,1,1), prop_sigma =NULL,par_names=c('alpha','gamma','delta'),data=x ) its gave me an error , I must enter the cov matrix but I don't know how to calculate it, somebody told me to wrote the function without prop_sigma but its also gave me an error what can I do?? Thank you,Sara [[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] Help with a function [along columns]
On Mon, 13 Oct 2014 08:54:51 PM Kate Ignatius wrote: Just an update to this: gtal - function(d) { alleles - sapply(d, function(.) strsplit(as.character(.), /)) gt - unlist(lapply(alleles, function(x) ifelse(identical(x[[1]], vcf[,3]) identical(x[[2]], vcf[,3]), 'RR', ifelse(identical(x[[1]], vcf[,4]) identical(x[[2]], vcf[,4]), 'AA', ifelse(identical(x[[1]], vcf[,3]) identical(x[[2]], vcf[,4]), 'RA', ifelse(identical(x[[1]], vcf[,4]) identical(x[[2]], vcf[,3]), 'RA', '')) } I've got something working but I'm having trouble with the gt part... I'm getting the error: object of type 'closure' is not subsettable. The vcf is my original file that I want to match with so not sure whether this a problem. Hi Kate, Unless you have passed vcf to your function, it is unlikely to recognize it. As you are working with genome data, I suspect that there is a function named vcf somewhere in the parent environment and you can't subset a function. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Help with a function [along columns]
Hi all, I need help with a function. I'm trying to write a function to apply to varying number of columns in a lot of files - hence the function... but I'm getting stuck. Here it is: gt- function(x) { alleles - sapply(x, function(.) strsplit(as.character(.), /)) gt - apply(x, function(.) ifelse(x[1] == vcf[3] x[2] == vcf[3], 'RR', ifelse(x[1] == vcf[4] x[2] == vcf[4], 'AA', ifelse(x[1] == vcf[3] x[2] == vcf[4], 'RA', ifelse(x[1] == vcf[4] x[2] == vcf[3], 'RA', '') } I have different sized family genetic files and at the end of the day I want to see whether the alleles of each person in the family match the ref and/or the alt and if so, give AA, RA or RR. Like so: REF ALT Sample_1 GT_1 Sample_2 GT_2 A G A/A RR A/G RA T G G/G AA T/T RR A T T/T AA A/A RR G A G/A RA G/G RR G A G/G RR G/A RA T C C/C AA C/C AA T C C/C AA C/C AA C T C/T RA T/T AA G A A/A AA A/A AA T G T/G RA G/G AA Is there an easy way to do this? Thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with a function [along columns]
Just an update to this: gtal - function(d) { alleles - sapply(d, function(.) strsplit(as.character(.), /)) gt - unlist(lapply(alleles, function(x) ifelse(identical(x[[1]], vcf[,3]) identical(x[[2]], vcf[,3]), 'RR', ifelse(identical(x[[1]], vcf[,4]) identical(x[[2]], vcf[,4]), 'AA', ifelse(identical(x[[1]], vcf[,3]) identical(x[[2]], vcf[,4]), 'RA', ifelse(identical(x[[1]], vcf[,4]) identical(x[[2]], vcf[,3]), 'RA', '')) } I've got something working but I'm having trouble with the gt part... I'm getting the error: object of type 'closure' is not subsettable. The vcf is my original file that I want to match with so not sure whether this a problem. On Mon, Oct 13, 2014 at 4:46 PM, Kate Ignatius kate.ignat...@gmail.com wrote: Hi all, I need help with a function. I'm trying to write a function to apply to varying number of columns in a lot of files - hence the function... but I'm getting stuck. Here it is: gt- function(x) { alleles - sapply(x, function(.) strsplit(as.character(.), /)) gt - apply(x, function(.) ifelse(x[1] == vcf[3] x[2] == vcf[3], 'RR', ifelse(x[1] == vcf[4] x[2] == vcf[4], 'AA', ifelse(x[1] == vcf[3] x[2] == vcf[4], 'RA', ifelse(x[1] == vcf[4] x[2] == vcf[3], 'RA', '') } I have different sized family genetic files and at the end of the day I want to see whether the alleles of each person in the family match the ref and/or the alt and if so, give AA, RA or RR. Like so: REF ALT Sample_1 GT_1 Sample_2 GT_2 A G A/A RR A/G RA T G G/G AA T/T RR A T T/T AA A/A RR G A G/A RA G/G RR G A G/G RR G/A RA T C C/C AA C/C AA T C C/C AA C/C AA C T C/T RA T/T AA G A A/A AA A/A AA T G T/G RA G/G AA Is there an easy way to do this? Thanks! __ 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 with Cast Function
Hi, This is the input data frame: ### df.1 = read.table(header=T,text= id gender WMC_alcohol WMC_caffeine WMC_no.drug RT_alcohol RT_caffeine RT_no.drug 1 1 female 3.7 3.7 3.9 488 236 371 2 2 female 6.4 7.3 7.9 607 376 349 3 3 female 4.6 7.4 7.3 643 226 412 4 4 male 6.4 7.8 8.2 684 206 252 5 5 female 4.9 5.2 7.0 593 262 439 6 6 male 5.4 6.6 7.2 492 230 464 7 7 male 7.9 7.9 8.9 690 259 327 8 8 male 4.1 5.9 4.5 486 230 305 9 9 female 5.2 6.2 7.2 686 273 327 10 10 female 6.2 7.4 7.8 645 240 498 ) ### This is the desired output: ### id gender drug WMC RT 1 1 female alcohol 3.7 488 2 2 female alcohol 6.4 607 3 3 female alcohol 4.6 643 4 4 male alcohol 6.4 684 5 5 female alcohol 4.9 593 6 6 male alcohol 5.4 492 7 7 male alcohol 7.9 690 8 8 male alcohol 4.1 486 9 9 female alcohol 5.2 686 10 10 female alcohol 6.2 645 11 1 female caffeine 3.7 236 12 2 female caffeine 7.3 376 ### I know some melt and cast magic is required. But I was unable to sort it myself. Here are the dput versions: Input Data Frame ### dput(df.1) structure(list(id = 1:10, gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c(female, male), class = factor), WMC_alcohol = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2), WMC_caffeine = c(3.7, 7.3, 7.4, 7.8, 5.2, 6.6, 7.9, 5.9, 6.2, 7.4), WMC_no.drug = c(3.9, 7.9, 7.3, 8.2, 7, 7.2, 8.9, 4.5, 7.2, 7.8), RT_alcohol = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L), RT_caffeine = c(236L, 376L, 226L, 206L, 262L, 230L, 259L, 230L, 273L, 240L), RT_no.drug = c(371L, 349L, 412L, 252L, 439L, 464L, 327L, 305L, 327L, 498L)), .Names = c(id, gender, WMC_alcohol, WMC_caffeine, WMC_no.drug, RT_alcohol, RT_caffeine, RT_no.drug), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) Output Data Frame ### dput(df.output) structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L), gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c(female, male), class = factor), drug = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c(alcohol, caffeine), class = factor), WMC = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2, 3.7, 7.3), RT = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L, 236L, 376L)), .Names = c(id, gender, drug, WMC, RT), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)) Cheers ! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with Cast Function
I see your desired output has rather fewer data than the input data frame. Instead of making us pore over a bunch of numbers, can you explain exactly what filtering you wish to do to get the specific subset of {male/female} {alcohol/caffeine} you're trying to get? BHM wrote Hi, This is the input data frame: ### df.1 = read.table(header=T,text= id gender WMC_alcohol WMC_caffeine WMC_no.drug RT_alcohol RT_caffeine RT_no.drug 1 1 female 3.7 3.7 3.9 488 236 371 2 2 female 6.4 7.3 7.9 607 376 349 3 3 female 4.6 7.4 7.3 643 226 412 4 4 male 6.4 7.8 8.2 684 206 252 5 5 female 4.9 5.2 7.0 593 262 439 6 6 male 5.4 6.6 7.2 492 230 464 7 7 male 7.9 7.9 8.9 690 259 327 8 8 male 4.1 5.9 4.5 486 230 305 9 9 female 5.2 6.2 7.2 686 273 327 10 10 female 6.2 7.4 7.8 645 240 498 ) ### This is the desired output: ### id gender drug WMC RT 1 1 female alcohol 3.7 488 2 2 female alcohol 6.4 607 3 3 female alcohol 4.6 643 4 4 male alcohol 6.4 684 5 5 female alcohol 4.9 593 6 6 male alcohol 5.4 492 7 7 male alcohol 7.9 690 8 8 male alcohol 4.1 486 9 9 female alcohol 5.2 686 10 10 female alcohol 6.2 645 11 1 female caffeine 3.7 236 12 2 female caffeine 7.3 376 ### I know some melt and cast magic is required. But I was unable to sort it myself. Here are the dput versions: Input Data Frame ### dput(df.1) structure(list(id = 1:10, gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c(female, male), class = factor), WMC_alcohol = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2), WMC_caffeine = c(3.7, 7.3, 7.4, 7.8, 5.2, 6.6, 7.9, 5.9, 6.2, 7.4), WMC_no.drug = c(3.9, 7.9, 7.3, 8.2, 7, 7.2, 8.9, 4.5, 7.2, 7.8), RT_alcohol = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L), RT_caffeine = c(236L, 376L, 226L, 206L, 262L, 230L, 259L, 230L, 273L, 240L), RT_no.drug = c(371L, 349L, 412L, 252L, 439L, 464L, 327L, 305L, 327L, 498L)), .Names = c(id, gender, WMC_alcohol, WMC_caffeine, WMC_no.drug, RT_alcohol, RT_caffeine, RT_no.drug), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) Output Data Frame ### dput(df.output) structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L), gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c(female, male), class = factor), drug = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c(alcohol, caffeine), class = factor), WMC = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2, 3.7, 7.3), RT = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L, 236L, 376L)), .Names = c(id, gender, drug, WMC, RT), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)) Cheers ! [[alternative HTML version deleted]] __ R-help@ 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. -- View this message in context: http://r.789695.n4.nabble.com/Help-with-Cast-Function-tp4681381p4681384.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with Cast Function
On Nov 29, 2013, at 9:42 AM, Burhan ul haq wrote: Hi, This is the input data frame: ### df.1 = read.table(header=T,text= id gender WMC_alcohol WMC_caffeine WMC_no.drug RT_alcohol RT_caffeine RT_no.drug 1 1 female 3.7 3.7 3.9 488 236 371 2 2 female 6.4 7.3 7.9 607 376 349 3 3 female 4.6 7.4 7.3 643 226 412 4 4 male 6.4 7.8 8.2 684 206 252 5 5 female 4.9 5.2 7.0 593 262 439 6 6 male 5.4 6.6 7.2 492 230 464 7 7 male 7.9 7.9 8.9 690 259 327 8 8 male 4.1 5.9 4.5 486 230 305 9 9 female 5.2 6.2 7.2 686 273 327 10 10 female 6.2 7.4 7.8 645 240 498 ) ### This is the desired output: ### id gender drug WMC RT 1 1 female alcohol 3.7 488 2 2 female alcohol 6.4 607 3 3 female alcohol 4.6 643 4 4 male alcohol 6.4 684 5 5 female alcohol 4.9 593 6 6 male alcohol 5.4 492 7 7 male alcohol 7.9 690 8 8 male alcohol 4.1 486 9 9 female alcohol 5.2 686 10 10 female alcohol 6.2 645 11 1 female caffeine 3.7 236 12 2 female caffeine 7.3 376 ### I know some melt and cast magic is required. But I was unable to sort it myself. # this is base::reshape reshape(df.1, idvar=1:2, sep=_, direction=long, varying=names(df.1)[3:8]) Here are the dput versions: Input Data Frame ### dput(df.1) structure(list(id = 1:10, gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c(female, male), class = factor), WMC_alcohol = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2), WMC_caffeine = c(3.7, 7.3, 7.4, 7.8, 5.2, 6.6, 7.9, 5.9, 6.2, 7.4), WMC_no.drug = c(3.9, 7.9, 7.3, 8.2, 7, 7.2, 8.9, 4.5, 7.2, 7.8), RT_alcohol = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L), RT_caffeine = c(236L, 376L, 226L, 206L, 262L, 230L, 259L, 230L, 273L, 240L), RT_no.drug = c(371L, 349L, 412L, 252L, 439L, 464L, 327L, 305L, 327L, 498L)), .Names = c(id, gender, WMC_alcohol, WMC_caffeine, WMC_no.drug, RT_alcohol, RT_caffeine, RT_no.drug), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) Output Data Frame ### dput(df.output) structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L), gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c(female, male), class = factor), drug = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c(alcohol, caffeine), class = factor), WMC = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2, 3.7, 7.3), RT = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L, 236L, 376L)), .Names = c(id, gender, drug, WMC, RT), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)) Cheers ! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with Cast Function
Hi, First, a big thanks to all those who replied. I am including all the replies in one email for easier reference later: # Input from David # reshape(df.1, idvar=1:2, sep=_, direction=long, varying=names(df.1)[3:8]) # # Input from Dennis # dfr1 - reshape(df.1, idvar = c(id, gender), v.names = c(WMC, RT), timevar = type, times = c(alcohol, caffeine, no.drug), varying = list(3:5, 6:8), direction = long) rownames(dfr1) - NULL dfr # # Input from Arun # library(reshape2) library(plyr) join_all(lapply(c(WMC,RT),function(x) transform(melt(df.1[,c(1:2,grep(x,names(df.1)))],id.vars=c(id,gender),var=drug),drug=gsub(.*\\_,,drug))),by=c (id,gender,drug)) # Cheers ! On Sat, Nov 30, 2013 at 1:20 AM, David Winsemius dwinsem...@comcast.netwrote: On Nov 29, 2013, at 9:42 AM, Burhan ul haq wrote: Hi, This is the input data frame: ### df.1 = read.table(header=T,text= id gender WMC_alcohol WMC_caffeine WMC_no.drug RT_alcohol RT_caffeine RT_no.drug 1 1 female 3.7 3.7 3.9 488 236 371 2 2 female 6.4 7.3 7.9 607 376 349 3 3 female 4.6 7.4 7.3 643 226 412 4 4 male 6.4 7.8 8.2 684 206 252 5 5 female 4.9 5.2 7.0 593 262 439 6 6 male 5.4 6.6 7.2 492 230 464 7 7 male 7.9 7.9 8.9 690 259 327 8 8 male 4.1 5.9 4.5 486 230 305 9 9 female 5.2 6.2 7.2 686 273 327 10 10 female 6.2 7.4 7.8 645 240 498 ) ### This is the desired output: ### id gender drug WMC RT 1 1 female alcohol 3.7 488 2 2 female alcohol 6.4 607 3 3 female alcohol 4.6 643 4 4 male alcohol 6.4 684 5 5 female alcohol 4.9 593 6 6 male alcohol 5.4 492 7 7 male alcohol 7.9 690 8 8 male alcohol 4.1 486 9 9 female alcohol 5.2 686 10 10 female alcohol 6.2 645 11 1 female caffeine 3.7 236 12 2 female caffeine 7.3 376 ### I know some melt and cast magic is required. But I was unable to sort it myself. # this is base::reshape reshape(df.1, idvar=1:2, sep=_, direction=long, varying=names(df.1)[3:8]) Here are the dput versions: Input Data Frame ### dput(df.1) structure(list(id = 1:10, gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c(female, male), class = factor), WMC_alcohol = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2), WMC_caffeine = c(3.7, 7.3, 7.4, 7.8, 5.2, 6.6, 7.9, 5.9, 6.2, 7.4), WMC_no.drug = c(3.9, 7.9, 7.3, 8.2, 7, 7.2, 8.9, 4.5, 7.2, 7.8), RT_alcohol = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L), RT_caffeine = c(236L, 376L, 226L, 206L, 262L, 230L, 259L, 230L, 273L, 240L), RT_no.drug = c(371L, 349L, 412L, 252L, 439L, 464L, 327L, 305L, 327L, 498L)), .Names = c(id, gender, WMC_alcohol, WMC_caffeine, WMC_no.drug, RT_alcohol, RT_caffeine, RT_no.drug), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) Output Data Frame ### dput(df.output) structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L), gender = structure(c(1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c(female, male), class = factor), drug = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c(alcohol, caffeine), class = factor), WMC = c(3.7, 6.4, 4.6, 6.4, 4.9, 5.4, 7.9, 4.1, 5.2, 6.2, 3.7, 7.3), RT = c(488L, 607L, 643L, 684L, 593L, 492L, 690L, 486L, 686L, 645L, 236L, 376L)), .Names = c(id, gender, drug, WMC, RT), class = data.frame, row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)) Cheers ! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for a function
2 1488 4038 12 4 48 2009-07-01 2 2 1587 4090 15 6 49 2009-07-03 5 2 1587 4568 15 6 50 2009-07-06 5 2 2485 5298 19 7 ... # I transform the date in numeric t1-derdata[[DATE]] t - as.numeric(t1) t #i add the numeric date to the dataset Mydata-data.frame(derdata,t) Mydata # I select each country ARGENTINA- subset(Mydata,select = c(DATE,t,Canada)) names(ARGENTINA)[3] - Symptomes ARGENTINA # I create two functions because i want to create two variables by country :Incubation and CONTAGIEUX # 1. Here is the incubation function (a person getting a virus ,incub the virus for a certain time : 2 days) incub - function(x){ x$Incubation - 0 x[order(x$t),] x$Incubation[1] - x$Symptomes[1] if(nrow(x) 1) x$Incubation[2] - sum(x$Symptomes[1:2]) for(i in seq_len(nrow(x))[-(1:2)]) x$Incubation[i] - sum(x$Symptomes[i - (0:1)]) x } 2. Contagion function (after incubing the virus, the subject can spread now the virus for the 7 coming days) contag - function(x){ x$CONTAGIEUX - 0 for(i in 1:min(nrow(x), 7)) x$CONTAGIEUX[i] - sum(x$Symptomes[1:i], na.rm = TRUE) for (i in seq_len(nrow(x))[-(1:7)]) { x$CONTAGIEUX[i] - x$Symptomes[i] + x$CONTAGIEUX[i-1] - x$Symptomes[i-7] } x } # and So i obtain by country what i need. incub_ARGENTINA -incub(ARGENTINA) incub_ARGENTINA contag_ARGENTINA -contag(ARGENTINA) contag_ARGENTINA derdata_ARGENTINA -merge(contag_ARGENTINA, incub_ARGENTINA) derdata_ARGENTINA Now my problem is that i want to update those functions incub and contag so that i can easily change the incubation and contagion period. they will not be fixe. the incubation period can be 2 or 1 or 3 or 4 or 5 and so. also the contagion period can be 4 or 5 or 6 or 8 or 9,.. And i want to not select the country before launch the the two funcions (incub and contag) so that i have all the datasets for all country in the same dataset Maurice De : Rui Barradas ruipbarra...@sapo.pt À : Jim Lemon j...@bitwrit.com.au Cc : anoumou teko_maur...@yahoo.fr; r-help@r-project.org Envoyé le : Mercredi 5 décembre 2012 16h26 Objet : Re: [R] Help for a function Hello, Also, t1 and min(xt) do not vary inside the loop so if it enters the loop it never exits. And res1[j] -(a*h) res2 -sum( res1[j]) is equivalent to res2 - a*h so the inner-most loop is not needed at all. Hope this helps, Rui Barradas Em 05-12-2012 04:20, Jim Lemon escreveu: On 12/05/2012 01:01 AM, anoumou wrote: Hello all, I need a help. I am modeling a disease and a create a R function like that: ... But i do not get the results,i try by all means but i d'ont understant the problem. Hi anoumou, Your function provides almost no indication of what two of its five arguments are supposed to be. If, with a certain degree of optimistic inference, we suppose x to be a data frame organized as shown below the function. t, i and CONTAGIEUX must be the appropriately named columns of the data frame. This leaves two columns, Symptomes and Incubation. Say we flip a coin to decide which of these to assign to r, and with all of our degrees of freedom gone, we assume that the other is h. We are forced to the conclusion that a is a nuisance argument, added to throw us off the scent. Peering within the function, we notice that the date format is wrong, braces are unmatched and that our data frame is alphabetically ordered by name of country to no purpose whatsoever. The best I can do here is to make the function potentially able to do something if you can work out what to do with it. Lambda-function (x,date1,r,h,a) { ndate1 - as.Date(date1, %Y-%m-%d) t1 - as.numeric(ndate1) x[order(x$i),] xt -x[,t] xi -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(xt) ){ for (i in 1:length(xi) ){ for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } x-data.frame(x,lambda) x } Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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 with dataEllipse function
Hi Everyone, I am working with the R function dataEllipse. I plot the 95% confidence ellipses for several different samples in the same plot and I color-code the ellipse of each sample, but I do not know how to specify a different line pattern for each ellipse. I can only modify the pattern for all ellipses with the lty argument. Any help will be highly appreciated. Thanks in advance! Jana -- Jana Makedonska, B.Sc. Biology, Universite Paul Sabatier Toulouse III M.Sc. Paleontology, Paleobiology and Phylogeny, Universite de Montpellier II Ph.D. candidate in Physical Anthropology and Part-time lecturer Department of Anthropology College of Arts Sciences State University of New York at Albany 1400 Washington Avenue 1 Albany, NY Office phone: 518-442-4699 http://electricsongs.academia.edu/JanaMakedonska [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with dataEllipse function
Dear Jana, The lty argument to dataEllipse() (in the car package) isn't vectorized. It could be, and I'll add that as a feature request. Actually, lty isn't an explicit argument to dataEllipse(); it's simply passed through to the lines() function, which draws the ellipses. You should be able to do what you want by adding the ellipses one at a time to your plot (see the argument add in ?dataEllipse) or by using the coordinates of the ellipses, returned by dataEllipse(), to a customized graph. I hope that this helps, John John Fox Sen. William McMaster Prof. of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/ On Thu, 25 Apr 2013 20:00:20 -0400 Jana Makedonska jmakedon...@gmail.com wrote: Hi Everyone, I am working with the R function dataEllipse. I plot the 95% confidence ellipses for several different samples in the same plot and I color-code the ellipse of each sample, but I do not know how to specify a different line pattern for each ellipse. I can only modify the pattern for all ellipses with the lty argument. Any help will be highly appreciated. Thanks in advance! Jana -- Jana Makedonska, B.Sc. Biology, Universite Paul Sabatier Toulouse III M.Sc. Paleontology, Paleobiology and Phylogeny, Universite de Montpellier II Ph.D. candidate in Physical Anthropology and Part-time lecturer Department of Anthropology College of Arts Sciences State University of New York at Albany 1400 Washington Avenue 1 Albany, NY Office phone: 518-442-4699 http://electricsongs.academia.edu/JanaMakedonska [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with dataEllipse function
On 4/25/2013 8:00 PM, Jana Makedonska wrote: Hi Everyone, I am working with the R function dataEllipse. I plot the 95% confidence ellipses for several different samples in the same plot and I color-code the ellipse of each sample, but I do not know how to specify a different line pattern for each ellipse. I can only modify the pattern for all ellipses with the lty argument. Any help will be highly appreciated. lty is not an argument of car::dataEllipse, but is passed via ... So, when you use the groups= argument, only a single value gets used. You would have to modify the function to allow what you want. -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ 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 with merge function
Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova ScotiaAnnapolis 0 0 1 Nova ScotiaAntigonish0 0 0 Nova ScotiaGly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0Nova ScotiaAnnapolis 0 0 10 1Nova ScotiaAntigonish0 0 1 2Nova ScotiaGly 0 0 1 So when I do x3 - merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE) it should do the trick. The thing is that it works for the columns (it adds all the new columns not common to both dataframes), but it also adds the rows. This is what I get (x3): FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 10NA NA Nova ScotiaAnnapolis NA NA NA 1 1Nova ScotiaAntigonish0 0 1 NA NA Nova ScotiaAntigonishNA NA NA 0 2Nova ScotiaGly 0 0 1 NA NA Nova ScotiaGly NA NA NA NA What I want to get is a true merge, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 101 1Nova ScotiaAntigonish0 0 1 0 2Nova ScotiaGly 0 0 1 NA Can anybody please help me to understand what I'm doing wrong. Any help will be much appreciated!! -- Catarina C. Ferreira, PhD [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with merge function
Hello, The following seems to do the trick. x1 - structure(list(State_prov = c(Nova Scotia, Nova Scotia, Nova Scotia ), Shape_name = c(Annapolis, Antigonish, Gly), bob2009 = c(0L, 0L, NA), bob2010 = c(0L, 0L, NA), bob2011 = c(1L, 0L, NA)), .Names = c(State_prov, Shape_name, bob2009, bob2010, bob2011), class = data.frame, row.names = c(NA, -3L)) x2 - structure(list(FID = 0:2, State_prov = c(Nova Scotia, Nova Scotia, Nova Scotia), Shape_name = c(Annapolis, Antigonish, Gly ), bob2009 = c(0L, 0L, 0L), bob2010 = c(0L, 0L, 0L), coy2009 = c(10L, 1L, 1L)), .Names = c(FID, State_prov, Shape_name, bob2009, bob2010, coy2009), class = data.frame, row.names = c(NA, -3L)) x3 - merge(x1, x2, all.y = TRUE) Note also that since by = intersect(names(x1), names(x2)), you really don't need it, it's the default behavior. Hope this helps, Rui Barradas Em 26-04-2013 18:10, Catarina Ferreira escreveu: Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova ScotiaAnnapolis 0 0 1 Nova ScotiaAntigonish0 0 0 Nova ScotiaGly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0Nova ScotiaAnnapolis 0 0 10 1Nova ScotiaAntigonish0 0 1 2Nova ScotiaGly 0 0 1 So when I do x3 - merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE) it should do the trick. The thing is that it works for the columns (it adds all the new columns not common to both dataframes), but it also adds the rows. This is what I get (x3): FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 10NA NA Nova ScotiaAnnapolis NA NA NA 1 1Nova ScotiaAntigonish0 0 1 NA NA Nova ScotiaAntigonishNA NA NA 0 2Nova ScotiaGly 0 0 1 NA NA Nova ScotiaGly NA NA NA NA What I want to get is a true merge, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 101 1Nova ScotiaAntigonish0 0 1 0 2Nova ScotiaGly 0 0 1 NA Can anybody please help me to understand what I'm doing wrong. Any help will be much appreciated!! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with merge function
Hello, Thank you for your help. However the dataframes I gave you were only examples, the actual dataframes are very big. Does this mean I have to write every range of data for each variable?? On Fri, Apr 26, 2013 at 2:25 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, The following seems to do the trick. x1 - structure(list(State_prov = c(Nova Scotia, Nova Scotia, Nova Scotia ), Shape_name = c(Annapolis, Antigonish, Gly), bob2009 = c(0L, 0L, NA), bob2010 = c(0L, 0L, NA), bob2011 = c(1L, 0L, NA)), .Names = c(State_prov, Shape_name, bob2009, bob2010, bob2011), class = data.frame, row.names = c(NA, -3L)) x2 - structure(list(FID = 0:2, State_prov = c(Nova Scotia, Nova Scotia, Nova Scotia), Shape_name = c(Annapolis, Antigonish, Gly ), bob2009 = c(0L, 0L, 0L), bob2010 = c(0L, 0L, 0L), coy2009 = c(10L, 1L, 1L)), .Names = c(FID, State_prov, Shape_name, bob2009, bob2010, coy2009), class = data.frame, row.names = c(NA, -3L)) x3 - merge(x1, x2, all.y = TRUE) Note also that since by = intersect(names(x1), names(x2)), you really don't need it, it's the default behavior. Hope this helps, Rui Barradas Em 26-04-2013 18:10, Catarina Ferreira escreveu: Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova ScotiaAnnapolis 0 0 1 Nova ScotiaAntigonish0 0 0 Nova ScotiaGly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0Nova ScotiaAnnapolis 0 0 10 1Nova ScotiaAntigonish0 0 1 2Nova ScotiaGly 0 0 1 So when I do x3 - merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE) it should do the trick. The thing is that it works for the columns (it adds all the new columns not common to both dataframes), but it also adds the rows. This is what I get (x3): FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 10NA NA Nova ScotiaAnnapolis NA NA NA 1 1Nova ScotiaAntigonish0 0 1 NA NA Nova ScotiaAntigonishNA NA NA 0 2Nova ScotiaGly 0 0 1 NA NA Nova ScotiaGly NA NA NA NA What I want to get is a true merge, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 101 1Nova ScotiaAntigonish0 0 1 0 2Nova ScotiaGly 0 0 1 NA Can anybody please help me to understand what I'm doing wrong. Any help will be much appreciated!! -- Catarina C. Ferreira, PhD Post-doctoral Research Fellow Department of Biology Trent University Peterborough, ON Canada URL: http://www.researcherid.com/rid/A-3898-2011 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with merge function
Hi, The format is bit messed up. So, not sure this is what you wanted. x1- read.table(text=State_prov,Shape_name,bob2009,bob2010,bob2011 Nova Scotia,Annapolis,0,0,1 Nova Scotia,Antigonish,0,0,0 Nova Scotia,Gly,NA,NA,NA ,sep=,,header=TRUE,stringsAsFactors=FALSE) x2- read.table(text= FID,State_prov,Shape_name,bob2009,bob2010,coy2009 0,Nova Scotia,Annapolis,0,0,10 1,Nova Scotia,Antigonish,0,0,1 2,Nova Scotia,Gly,0,0,1 ,sep=,,header=TRUE,stringsAsFactors=FALSE) merge(x1,x2,all=TRUE) # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 #4 Nova Scotia Gly NA NA NA NA NA - Original Message - From: Catarina Ferreira catferre...@gmail.com To: r-help@r-project.org Cc: Sent: Friday, April 26, 2013 1:10 PM Subject: [R] Help with merge function Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova Scotia Annapolis 0 0 1 Nova Scotia Antigonish 0 0 0 Nova Scotia Gly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0 Nova Scotia Annapolis 0 0 10 1 Nova Scotia Antigonish 0 0 1 2 Nova Scotia Gly 0 0 1 So when I do x3 - merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE) it should do the trick. The thing is that it works for the columns (it adds all the new columns not common to both dataframes), but it also adds the rows. This is what I get (x3): FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0 Nova Scotia Annapolis 0 0 10 NA NA Nova Scotia Annapolis NA NA NA 1 1 Nova Scotia Antigonish 0 0 1 NA NA Nova Scotia Antigonish NA NA NA 0 2 Nova Scotia Gly 0 0 1 NA NA Nova Scotia Gly NA NA NA NA What I want to get is a true merge, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0 Nova Scotia Annapolis 0 0 10 1 1 Nova Scotia Antigonish 0 0 1 0 2 Nova Scotia Gly 0 0 1 NA Can anybody please help me to understand what I'm doing wrong. Any help will be much appreciated!! -- Catarina C. Ferreira, PhD [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with merge function
Hello, I don't understand the question, what range? I've just changed the 'all' argument to 'all.y', without doing anything special to the variables. Can you explain what you mean? Rui Barradas Em 26-04-2013 19:30, Catarina Ferreira escreveu: Hello, Thank you for your help. However the dataframes I gave you were only examples, the actual dataframes are very big. Does this mean I have to write every range of data for each variable?? On Fri, Apr 26, 2013 at 2:25 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, The following seems to do the trick. x1 - structure(list(State_prov = c(Nova Scotia, Nova Scotia, Nova Scotia ), Shape_name = c(Annapolis, Antigonish, Gly), bob2009 = c(0L, 0L, NA), bob2010 = c(0L, 0L, NA), bob2011 = c(1L, 0L, NA)), .Names = c(State_prov, Shape_name, bob2009, bob2010, bob2011), class = data.frame, row.names = c(NA, -3L)) x2 - structure(list(FID = 0:2, State_prov = c(Nova Scotia, Nova Scotia, Nova Scotia), Shape_name = c(Annapolis, Antigonish, Gly ), bob2009 = c(0L, 0L, 0L), bob2010 = c(0L, 0L, 0L), coy2009 = c(10L, 1L, 1L)), .Names = c(FID, State_prov, Shape_name, bob2009, bob2010, coy2009), class = data.frame, row.names = c(NA, -3L)) x3 - merge(x1, x2, all.y = TRUE) Note also that since by = intersect(names(x1), names(x2)), you really don't need it, it's the default behavior. Hope this helps, Rui Barradas Em 26-04-2013 18:10, Catarina Ferreira escreveu: Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova ScotiaAnnapolis 0 0 1 Nova ScotiaAntigonish0 0 0 Nova ScotiaGly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0Nova ScotiaAnnapolis 0 0 10 1Nova ScotiaAntigonish0 0 1 2Nova ScotiaGly 0 0 1 So when I do x3 - merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE) it should do the trick. The thing is that it works for the columns (it adds all the new columns not common to both dataframes), but it also adds the rows. This is what I get (x3): FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 10NA NA Nova ScotiaAnnapolis NA NA NA 1 1Nova ScotiaAntigonish0 0 1 NA NA Nova ScotiaAntigonishNA NA NA 0 2Nova ScotiaGly 0 0 1 NA NA Nova ScotiaGly NA NA NA NA What I want to get is a true merge, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0Nova ScotiaAnnapolis 0 0 101 1Nova ScotiaAntigonish0 0 1 0 2Nova ScotiaGly 0 0 1 NA Can anybody please help me to understand what I'm doing wrong. Any help will be much appreciated!! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with merge function
Hi, From the output you wanted, it looks like: library(plyr) join(x1,x2,type=right) #Joining by: State_prov, Shape_name, bob2009, bob2010 # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 merge(x1,x2,all.y=TRUE) # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 A.K. From: Catarina Ferreira catferre...@gmail.com To: arun smartpink...@yahoo.com Sent: Friday, April 26, 2013 2:23 PM Subject: Re: [R] Help with merge function Hello, I didn't realize that the format had been changed after I sent the email. I'm sending you the original mail in attach in a word with the correct format, since I don't think your answer is the one I'm looking for, likely due to the erroneous format. Thank you again for your help. On Fri, Apr 26, 2013 at 2:11 PM, arun smartpink...@yahoo.com wrote: Hi, The format is bit messed up. So, not sure this is what you wanted. x1- read.table(text=State_prov,Shape_name,bob2009,bob2010,bob2011 Nova Scotia,Annapolis,0,0,1 Nova Scotia,Antigonish,0,0,0 Nova Scotia,Gly,NA,NA,NA ,sep=,,header=TRUE,stringsAsFactors=FALSE) x2- read.table(text= FID,State_prov,Shape_name,bob2009,bob2010,coy2009 0,Nova Scotia,Annapolis,0,0,10 1,Nova Scotia,Antigonish,0,0,1 2,Nova Scotia,Gly,0,0,1 ,sep=,,header=TRUE,stringsAsFactors=FALSE) merge(x1,x2,all=TRUE) # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 #4 Nova Scotia Gly NA NA NA NA NA - Original Message - From: Catarina Ferreira catferre...@gmail.com To: r-help@r-project.org Cc: Sent: Friday, April 26, 2013 1:10 PM Subject: [R] Help with merge function Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova Scotia Annapolis 0 0 1 Nova Scotia Antigonish 0 0 0 Nova Scotia Gly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0 Nova Scotia Annapolis 0 0 10 1 Nova Scotia Antigonish 0 0 1 2 Nova Scotia Gly 0 0 1 So when I do x3 - merge(x1, x2, by=intersect(names(x1), names(x2)), all=TRUE) it should do the trick. The thing is that it works for the columns (it adds all the new columns not common to both dataframes), but it also adds the rows. This is what I get (x3): FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0 Nova Scotia Annapolis 0 0 10 NA NA Nova Scotia Annapolis NA NA NA 1 1 Nova Scotia Antigonish 0 0 1 NA NA Nova Scotia Antigonish NA NA NA 0 2 Nova Scotia Gly 0 0 1 NA NA Nova Scotia Gly NA NA NA NA What I want to get is a true merge, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 bob2011 0 Nova Scotia Annapolis 0 0 10 1 1 Nova Scotia Antigonish 0 0 1 0 2 Nova Scotia Gly 0 0 1 NA Can anybody please help me to understand what I'm doing wrong. Any help will be much appreciated!! -- Catarina C. Ferreira, PhD [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Catarina C. Ferreira, PhD Post-doctoral Research Fellow Department of Biology Trent University Peterborough, ON Canada URL: http://www.researcherid.com/rid/A-3898-2011 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do
Re: [R] Help with merge function
Hi, Check whether this works. Lines1-readLines(NS_update.txt) x1-read.table(text=gsub('\',,Lines1),sep=,,header=TRUE,stringsAsFactors=FALSE) x2- read.table(data.txt,sep=,header=TRUE,stringsAsFactors=FALSE,fill=TRUE) dim(x2) #[1] 34577 189 library(plyr) res- join(x1,x2,type=right) #Joining by: State_Prov, Shape_name, bob2009, bob2010, red2009, red2010, coy2009, coy2010, lyn2009, lyn2010 dim(res) #[1] 34577 193 res2- merge(x1,x2,all.y=TRUE) dim(res2) #[1] 34577 193 A.K. From: Catarina Ferreira catferre...@gmail.com To: arun smartpink...@yahoo.com Sent: Friday, April 26, 2013 4:20 PM Subject: Re: [R] Help with merge function here they are. As you see the NS_update is data for only 1 province and I want it to add this data to the bigger file (data), merging the common columns and adding the new columns. But what it is doing is duplicating the rows in the bigger file that correspond to NS_update, as well as creating the new columns (this is ok). On Fri, Apr 26, 2013 at 4:16 PM, arun smartpink...@yahoo.com wrote: You can send the files. From: Catarina Ferreira catferre...@gmail.com To: arun smartpink...@yahoo.com Sent: Friday, April 26, 2013 4:15 PM Subject: Re: [R] Help with merge function is it ok if I send you the files, it's probably better for you to understand me. It didn't work on my files. On Fri, Apr 26, 2013 at 4:12 PM, arun smartpink...@yahoo.com wrote: Hi, I am not sure what is the problem. I used the datasets your provided x1 and x2. I got the result that was shown in the output your desired. Are you saying that this didn't worked in your original dataset or the one your provided? In that case, could you dput(dataset,20)? From: Catarina Ferreira catferre...@gmail.com To: arun smartpink...@yahoo.com Sent: Friday, April 26, 2013 4:01 PM Subject: Re: [R] Help with merge function Thank you. It still isn't working. Thank you in any case. On Fri, Apr 26, 2013 at 2:31 PM, arun smartpink...@yahoo.com wrote: Hi, From the output you wanted, it looks like: library(plyr) join(x1,x2,type=right) #Joining by: State_prov, Shape_name, bob2009, bob2010 # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 merge(x1,x2,all.y=TRUE) # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 A.K. From: Catarina Ferreira catferre...@gmail.com To: arun smartpink...@yahoo.com Sent: Friday, April 26, 2013 2:23 PM Subject: Re: [R] Help with merge function Hello, I didn't realize that the format had been changed after I sent the email. I'm sending you the original mail in attach in a word with the correct format, since I don't think your answer is the one I'm looking for, likely due to the erroneous format. Thank you again for your help. On Fri, Apr 26, 2013 at 2:11 PM, arun smartpink...@yahoo.com wrote: Hi, The format is bit messed up. So, not sure this is what you wanted. x1- read.table(text=State_prov,Shape_name,bob2009,bob2010,bob2011 Nova Scotia,Annapolis,0,0,1 Nova Scotia,Antigonish,0,0,0 Nova Scotia,Gly,NA,NA,NA ,sep=,,header=TRUE,stringsAsFactors=FALSE) x2- read.table(text= FID,State_prov,Shape_name,bob2009,bob2010,coy2009 0,Nova Scotia,Annapolis,0,0,10 1,Nova Scotia,Antigonish,0,0,1 2,Nova Scotia,Gly,0,0,1 ,sep=,,header=TRUE,stringsAsFactors=FALSE) merge(x1,x2,all=TRUE) # State_prov Shape_name bob2009 bob2010 bob2011 FID coy2009 #1 Nova Scotia Annapolis 0 0 1 0 10 #2 Nova Scotia Antigonish 0 0 0 1 1 #3 Nova Scotia Gly 0 0 NA 2 1 #4 Nova Scotia Gly NA NA NA NA NA - Original Message - From: Catarina Ferreira catferre...@gmail.com To: r-help@r-project.org Cc: Sent: Friday, April 26, 2013 1:10 PM Subject: [R] Help with merge function Dear all, I'm trying to merge 2 dataframes, but I'm not being entirely successful and I can't understand why. Dataframe x1 State_prov Shape_name bob2009 bob 2010 bob2011 Nova Scotia Annapolis 0 0 1 Nova Scotia Antigonish 0 0 0 Nova Scotia Gly NA NA NA Dataframe x2 - has 2 rows and 193 variables, contains one important field which is FID that is a link to a shapefile (this is not in x1) and shares common columns with x1, like this: FID State_prov Shape_name bob2009 bob 2010 coy 2009 0 Nova
[R] Help with lmRob function
Hi, I am fairly new to R and have encountered an issue with the lmRob function that I have been unable to resolve. I am trying to run a robust regression using the lmRob function which runs successfully, but the results are rather strange. I'm not sure it's important, but my model has 3 dichotomous categorical variables and 2 continuous variables in it. When I look at a summary of my model, 1 of my continuous variables and 1 of my categorical variables both have an Estimate value of 1. My other 2 categorical variables and 1 other continuous variables all have Estimate values of 0. These results seem strange to me. When I run a simple rlm function the model runs fine and produces the results I'd expect to see. Another potentially useful piece of information is that when I run a model with just the two variables that have an Estimate value of 1 (one categorical and one continuous), I get the following error message: 1: In lmRob.fit.compute(x, y, x1.idx = x1.idx, nrep = nrep, robust.control = robust.control, : Sum(psi.weight(wi)) less than 1e-06 in lmRob.ucovcoef. during initial estimation. 2: In lmRob.fit.compute(x, y, x1.idx = x1.idx, nrep = nrep, robust.control = robust.control, : Sum(psi.weight(wi)) less than 1e-06 in lmRob.ucovcoef. during final scale estimation. I do not understand what this error message means and have been unable to resolve it. I tried several different permutations of my five variables in varying combinations and sometimes the models work and sometimes I get this error message. There seems to be no pattern as to when the error message occurs and when it does not. If anyone has encountered this or has any help I'd appreciate it. Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] help with simple function
hello all I am writing a quite simple script to study dental wear patterns in humans and I wrote this function sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this error message Error: attempt to apply non-function alternatively I wrote this sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))] but this error message appear [1] NA Warning message: NAs introduced by coercion some suggestion to solve this?? thank you so much for your help Miguel [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with simple function
On 17-03-2013, at 16:47, Miguel Eduardo Delgado Burbano mdelgadoburb...@gmail.com wrote: hello all I am writing a quite simple script to study dental wear patterns in humans and I wrote this function sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this error message Error: attempt to apply non-function Write 4*( instead of 4( assuming you want to multiply. alternatively I wrote this sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))] but this error message appear [1] NA Warning message: NAs introduced by coercion [ is an indexing operator. You are trying to index the number 4. Which is impossible. Berend some suggestion to solve this?? thank you so much for your help Miguel [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with simple function
Hi, Y1- 1:4 Y2- 5:8 sqrt(var(Y1)+var(Y2)^2)-4*((var(Y1)*(var(Y2)-cov(Y1,Y2)^2))) #[1] 9.515593 A.K. - Original Message - From: Miguel Eduardo Delgado Burbano mdelgadoburb...@gmail.com To: r-help@r-project.org Cc: Sent: Sunday, March 17, 2013 11:47 AM Subject: [R] help with simple function hello all I am writing a quite simple script to study dental wear patterns in humans and I wrote this function sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this error message Error: attempt to apply non-function alternatively I wrote this sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))] but this error message appear [1] NA Warning message: NAs introduced by coercion some suggestion to solve this?? thank you so much for your help Miguel [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with simple function
Hello, You are missing a '*' in your first try. (As for the second, in R parenthesis are round, not []) sqrt(var(Y1) + var(Y2))^2 - 4*(var(Y1)*(var(Y2) - cov(Y1, Y2)^2)) This written as a function becomes fun - function(Y1, Y2) sqrt(var(Y1) + var(Y2))^2 - 4*(var(Y1)*(var(Y2) - cov(Y1, Y2)^2)) x1 - rnorm(10) x2 - rnorm(10) fun(x1, x2) Hope this helps, Rui Barradas Em 17-03-2013 15:47, Miguel Eduardo Delgado Burbano escreveu: hello all I am writing a quite simple script to study dental wear patterns in humans and I wrote this function sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this error message Error: attempt to apply non-function alternatively I wrote this sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))] but this error message appear [1] NA Warning message: NAs introduced by coercion some suggestion to solve this?? thank you so much for your help Miguel [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with a function and text
Thanks for that. Eliano 2013/3/7 David Winsemius [via R] ml-node+s789695n4660565...@n4.nabble.com On Mar 6, 2013, at 3:44 PM, Eliano wrote: Thanks. Btw are you able to help with my issue? Thanks, Eliano I'm sorry, I was too busy answering the question from 'Eliano' over on StackOverflow. I didn't have time to address this one. (Please do note that cross-posting questions to Rhelp is contrary to advice in the Posting Guide.) You might also do further searching in the Archives with the search terms: substitute text eval and perhaps narrow it down further with contributors named: grothendeick, dunlap, ligges, venables Sent from my iPhone On 6 Mar 2013, at 23:41, David Winsemius [via R] [hidden email] http://user/SendEmail.jtp?type=nodenode=4660565i=0 wrote: On Mar 6, 2013, at 11:25 AM, Eliano Marques wrote: Hi, can I understand why this message was rejected ? Thanks, Eliano First hit on a Markmail search: http://markmail.org/message/5xog3ayx4amprsdx?q=list:org%2Er-project%2Er-help+nabble+rejected -- David. Sent from my iPhone On 6 Mar 2013, at 19:18, Eliano [hidden email]/user/SendEmail.jtp?type=nodenode=4660547i=0 wrote: Hi everyone, I am writing some code to generate a function. I am passing that code to a dataset which i'm importing in R, e.g. Test=read.table('C:/test.txt', header=F, sep='\t', na.strings='NA', dec='.', strip.white=TRUE) Test V1 (if(nclusters0){OptmizationInputs[3,3]*beta[1]}else{0})+ (if(nclusters1){OptmizationInputs[3,3]*beta[1]}else{0})+ V1 has inside a code for a function. I'm having problems with 2 things: 1 - I need to take out from V1 all that appears in the text, i tried a replace but did not work. Test=replace(Test,' ', ' ') , did not work. 2 - Writing a function like this : nlog=function(par) { beta=par[1:n] Measure=Test[1] # would this read the text? return(Measure) } So i need to use that code inside the function as above. Any suggestion on how you would do this? Kind Regards, Eliano -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] /user/SendEmail.jtp?type=nodenode=4660547i=1 mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ [hidden email] /user/SendEmail.jtp?type=nodenode=4660547i=2 mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660547.html To unsubscribe from Help with a function and text, click here . NAML http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660548.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4660565i=1mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4660565i=2mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660565.html To unsubscribe from Help with a function and text,
Re: [R] Help with a function and text
Hi, can I understand why this message was rejected ? Thanks, Eliano Sent from my iPhone On 6 Mar 2013, at 19:18, Eliano eliano.m.marq...@gmail.com wrote: Hi everyone, I am writing some code to generate a function. I am passing that code to a dataset which i'm importing in R, e.g. Test=read.table('C:/test.txt', header=F, sep='\t', na.strings='NA', dec='.', strip.white=TRUE) Test V1 (if(nclusters0){OptmizationInputs[3,3]*beta[1]}else{0})+ (if(nclusters1){OptmizationInputs[3,3]*beta[1]}else{0})+ V1 has inside a code for a function. I'm having problems with 2 things: 1 - I need to take out from V1 all that appears in the text, i tried a replace but did not work. Test=replace(Test,' ', ' ') , did not work. 2 - Writing a function like this : nlog=function(par) { beta=par[1:n] Measure=Test[1] # would this read the text? return(Measure) } So i need to use that code inside the function as above. Any suggestion on how you would do this? Kind Regards, Eliano -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with a function and text
On Mar 6, 2013, at 11:25 AM, Eliano Marques wrote: Hi, can I understand why this message was rejected ? Thanks, Eliano First hit on a Markmail search: http://markmail.org/message/5xog3ayx4amprsdx?q=list:org%2Er-project%2Er-help+nabble+rejected -- David. Sent from my iPhone On 6 Mar 2013, at 19:18, Eliano eliano.m.marq...@gmail.com wrote: Hi everyone, I am writing some code to generate a function. I am passing that code to a dataset which i'm importing in R, e.g. Test=read.table('C:/test.txt', header=F, sep='\t', na.strings='NA', dec='.', strip.white=TRUE) Test V1 (if(nclusters0){OptmizationInputs[3,3]*beta[1]}else{0})+ (if(nclusters1){OptmizationInputs[3,3]*beta[1]}else{0})+ V1 has inside a code for a function. I'm having problems with 2 things: 1 - I need to take out from V1 all that appears in the text, i tried a replace but did not work. Test=replace(Test,' ', ' ') , did not work. 2 - Writing a function like this : nlog=function(par) { beta=par[1:n] Measure=Test[1] # would this read the text? return(Measure) } So i need to use that code inside the function as above. Any suggestion on how you would do this? Kind Regards, Eliano -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with a function and text
Thanks. Btw are you able to help with my issue? Thanks, Eliano Sent from my iPhone On 6 Mar 2013, at 23:41, David Winsemius [via R] ml-node+s789695n4660547...@n4.nabble.com wrote: On Mar 6, 2013, at 11:25 AM, Eliano Marques wrote: Hi, can I understand why this message was rejected ? Thanks, Eliano First hit on a Markmail search: http://markmail.org/message/5xog3ayx4amprsdx?q=list:org%2Er-project%2Er-help+nabble+rejected -- David. Sent from my iPhone On 6 Mar 2013, at 19:18, Eliano [hidden email]/user/SendEmail.jtp?type=nodenode=4660547i=0 wrote: Hi everyone, I am writing some code to generate a function. I am passing that code to a dataset which i'm importing in R, e.g. Test=read.table('C:/test.txt', header=F, sep='\t', na.strings='NA', dec='.', strip.white=TRUE) Test V1 (if(nclusters0){OptmizationInputs[3,3]*beta[1]}else{0})+ (if(nclusters1){OptmizationInputs[3,3]*beta[1]}else{0})+ V1 has inside a code for a function. I'm having problems with 2 things: 1 - I need to take out from V1 all that appears in the text, i tried a replace but did not work. Test=replace(Test,' ', ' ') , did not work. 2 - Writing a function like this : nlog=function(par) { beta=par[1:n] Measure=Test[1] # would this read the text? return(Measure) } So i need to use that code inside the function as above. Any suggestion on how you would do this? Kind Regards, Eliano -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] /user/SendEmail.jtp?type=nodenode=4660547i=1 mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ [hidden email] /user/SendEmail.jtp?type=nodenode=4660547i=2 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. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660547.html To unsubscribe from Help with a function and text, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4660523code=ZWxpYW5vLm0ubWFycXVlc0BnbWFpbC5jb218NDY2MDUyM3wtMTk0ODk5MDYy . NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660548.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with a function and text
On Mar 6, 2013, at 3:44 PM, Eliano wrote: Thanks. Btw are you able to help with my issue? Thanks, Eliano I'm sorry, I was too busy answering the question from 'Eliano' over on StackOverflow. I didn't have time to address this one. (Please do note that cross-posting questions to Rhelp is contrary to advice in the Posting Guide.) You might also do further searching in the Archives with the search terms: substitute text eval and perhaps narrow it down further with contributors named: grothendeick, dunlap, ligges, venables Sent from my iPhone On 6 Mar 2013, at 23:41, David Winsemius [via R] ml-node+s789695n4660547...@n4.nabble.com wrote: On Mar 6, 2013, at 11:25 AM, Eliano Marques wrote: Hi, can I understand why this message was rejected ? Thanks, Eliano First hit on a Markmail search: http://markmail.org/message/5xog3ayx4amprsdx?q=list:org%2Er-project%2Er-help+nabble+rejected -- David. Sent from my iPhone On 6 Mar 2013, at 19:18, Eliano [hidden email]/user/SendEmail.jtp?type=nodenode=4660547i=0 wrote: Hi everyone, I am writing some code to generate a function. I am passing that code to a dataset which i'm importing in R, e.g. Test=read.table('C:/test.txt', header=F, sep='\t', na.strings='NA', dec='.', strip.white=TRUE) Test V1 (if(nclusters0){OptmizationInputs[3,3]*beta[1]}else{0})+ (if(nclusters1){OptmizationInputs[3,3]*beta[1]}else{0})+ V1 has inside a code for a function. I'm having problems with 2 things: 1 - I need to take out from V1 all that appears in the text, i tried a replace but did not work. Test=replace(Test,' ', ' ') , did not work. 2 - Writing a function like this : nlog=function(par) { beta=par[1:n] Measure=Test[1] # would this read the text? return(Measure) } So i need to use that code inside the function as above. Any suggestion on how you would do this? Kind Regards, Eliano -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] /user/SendEmail.jtp?type=nodenode=4660547i=1 mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ [hidden email] /user/SendEmail.jtp?type=nodenode=4660547i=2 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. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660547.html To unsubscribe from Help with a function and text, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4660523code=ZWxpYW5vLm0ubWFycXVlc0BnbWFpbC5jb218NDY2MDUyM3wtMTk0ODk5MDYy . NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/Help-with-a-function-and-text-tp4660523p4660548.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for a function
Hello, Also, t1 and min(xt) do not vary inside the loop so if it enters the loop it never exits. And res1[j] -(a*h) res2 -sum( res1[j]) is equivalent to res2 - a*h so the inner-most loop is not needed at all. Hope this helps, Rui Barradas Em 05-12-2012 04:20, Jim Lemon escreveu: On 12/05/2012 01:01 AM, anoumou wrote: Hello all, I need a help. I am modeling a disease and a create a R function like that: ... But i do not get the results,i try by all means but i d'ont understant the problem. Hi anoumou, Your function provides almost no indication of what two of its five arguments are supposed to be. If, with a certain degree of optimistic inference, we suppose x to be a data frame organized as shown below the function. t, i and CONTAGIEUX must be the appropriately named columns of the data frame. This leaves two columns, Symptomes and Incubation. Say we flip a coin to decide which of these to assign to r, and with all of our degrees of freedom gone, we assume that the other is h. We are forced to the conclusion that a is a nuisance argument, added to throw us off the scent. Peering within the function, we notice that the date format is wrong, braces are unmatched and that our data frame is alphabetically ordered by name of country to no purpose whatsoever. The best I can do here is to make the function potentially able to do something if you can work out what to do with it. Lambda-function (x,date1,r,h,a) { ndate1 - as.Date(date1, %Y-%m-%d) t1 - as.numeric(ndate1) x[order(x$i),] xt -x[,t] xi -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(xt) ){ for (i in 1:length(xi) ){ for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } x-data.frame(x,lambda) x } Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for a function
Thanks you all Maurice TEKO Biostatisticien,Doctorant. Université de Liège Département des Sciences et Gestion de l'environnement 185 avenue de Longwy 6700 Arlon (Belgique) Mail :teko_maur...@yahoo.fr :anoumou.tekoahate...@ulg.ac.be http://www.ulg.ac.be De : Rui Barradas ruipbarra...@sapo.pt À : Jim Lemon j...@bitwrit.com.au Envoyé le : Mercredi 5 décembre 2012 16h26 Objet : Re: [R] Help for a function Hello, Also, t1 and min(xt) do not vary inside the loop so if it enters the loop it never exits. And res1[j] -(a*h) res2 -sum( res1[j]) is equivalent to res2 - a*h so the inner-most loop is not needed at all. Hope this helps, Rui Barradas Em 05-12-2012 04:20, Jim Lemon escreveu: On 12/05/2012 01:01 AM, anoumou wrote: Hello all, I need a help. I am modeling a disease and a create a R function like that: ... But i do not get the results,i try by all means but i d'ont understant the problem. Hi anoumou, Your function provides almost no indication of what two of its five arguments are supposed to be. If, with a certain degree of optimistic inference, we suppose x to be a data frame organized as shown below the function. t, i and CONTAGIEUX must be the appropriately named columns of the data frame. This leaves two columns, Symptomes and Incubation. Say we flip a coin to decide which of these to assign to r, and with all of our degrees of freedom gone, we assume that the other is h. We are forced to the conclusion that a is a nuisance argument, added to throw us off the scent. Peering within the function, we notice that the date format is wrong, braces are unmatched and that our data frame is alphabetically ordered by name of country to no purpose whatsoever. The best I can do here is to make the function potentially able to do something if you can work out what to do with it. Lambda-function (x,date1,r,h,a) { ndate1 - as.Date(date1, %Y-%m-%d) t1 - as.numeric(ndate1) x[order(x$i),] xt -x[,t] xi -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(xt) ){ for (i in 1:length(xi) ){ for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } x-data.frame(x,lambda) x } Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for a function
Rui , I discover all this when running and rerunning my function one and thousand times and per line. Now it's perfect, and work so well. Thanks. À : Rui Barradas ruipbarra...@sapo.pt; Jim Lemon j...@bitwrit.com.au Cc : r-help@r-project.org r-help@r-project.org Envoyé le : Mercredi 5 décembre 2012 16h45 Objet : Re: [R] Help for a function Thanks you all Maurice TEKO Biostatisticien,Doctorant. Université de Liège Département des Sciences et Gestion de l'environnement 185 avenue de Longwy 6700 Arlon (Belgique) :anoumou.tekoahate...@ulg.ac.be http://www.ulg.ac.be De : Rui Barradas ruipbarra...@sapo.pt À : Jim Lemon j...@bitwrit.com.au Envoyé le : Mercredi 5 décembre 2012 16h26 Objet : Re: [R] Help for a function Hello, Also, t1 and min(xt) do not vary inside the loop so if it enters the loop it never exits. And res1[j] -(a*h) res2 -sum( res1[j]) is equivalent to res2 - a*h so the inner-most loop is not needed at all. Hope this helps, Rui Barradas Em 05-12-2012 04:20, Jim Lemon escreveu: On 12/05/2012 01:01 AM, anoumou wrote: Hello all, I need a help. I am modeling a disease and a create a R function like that: ... But i do not get the results,i try by all means but i d'ont understant the problem. Hi anoumou, Your function provides almost no indication of what two of its five arguments are supposed to be. If, with a certain degree of optimistic inference, we suppose x to be a data frame organized as shown below the function. t, i and CONTAGIEUX must be the appropriately named columns of the data frame. This leaves two columns, Symptomes and Incubation. Say we flip a coin to decide which of these to assign to r, and with all of our degrees of freedom gone, we assume that the other is h. We are forced to the conclusion that a is a nuisance argument, added to throw us off the scent. Peering within the function, we notice that the date format is wrong, braces are unmatched and that our data frame is alphabetically ordered by name of country to no purpose whatsoever. The best I can do here is to make the function potentially able to do something if you can work out what to do with it. Lambda-function (x,date1,r,h,a) { ndate1 - as.Date(date1, %Y-%m-%d) t1 - as.numeric(ndate1) x[order(x$i),] xt -x[,t] xi -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(xt) ){ for (i in 1:length(xi) ){ for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } x-data.frame(x,lambda) x } Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Help for a function
Hello all, I need a help. I am modeling a disease and a create a R function like that: Lambda-function (x,date1,r,h,a){ ndate1 - as.Date(date1, %d/%m/%Y) t1 - as.numeric(ndate1) x[order(x$i),] t -x[,t] i -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(t) ){ for (i in 1:length(i) ){ {for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } } x-data.frame(x,lambda) x } on such data : DATEi Symptomes t Incubation CONTAGIEUX 1 2009-04-29 Canada 13 14363 13 13 2 2009-05-01 Israel 2 14365 2 2 3 2009-05-09 argentina 1 14373 1 1 5 2009-05-09 australia 1 14373 1 1 6 2009-05-10 australia 1 14374 2 2 7 2009-04-29 Austria 1 14363 1 1 8 2009-04-30 Austria 1 14364 2 2 9 2009-05-01 Austria 1 14365 2 3 10 2009-05-02 Austria 1 14366 2 4 11 2009-05-03 Austria 1 14367 2 5 17 2009-05-09 Austria 1 14373 2 7 18 2009-05-10 Austria 1 14374 2 7 19 2009-05-08 brasil 4 14372 4 4 20 2009-05-09 brazil 6 14373 6 6 21 2009-05-10 brazil 6 14374 12 12 22 2009-05-02 canada 51 14366 51 51 23 2009-05-03 canada 85 14367 136 136 24 2009-05-04 canada 101 14368 186 237 31 2009-04-27 Canada 6 14361 6 6 32 2009-04-28 Canada 6 14362 6 6 33 2009-04-30 Canada 19 14364 25 25 34 2009-05-01 Canada 34 14365 53 59 35 2009-05-01 China,HongKong, SAR 1 14365 1 1 36 2009-05-02 China,HongKong, SAR 1 14366 2 2 37 2009-05-03 China,HongKong, SAR 1 14367 2 3 38 2009-05-04 China,HongKong, SAR 1 14368 2 4 44 2009-05-10 China,HongKong, SAR 1 14374 2 7 45 2009-05-04 colombia1 14368 1 1 46 2009-05-05 colombia1 14369 2 2 47 2009-05-06 colombia But i do not get the results,i try by all means but i d'ont understant the problem. Thanks for your help. -- View this message in context: http://r.789695.n4.nabble.com/Help-for-a-function-tp4652054.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for a function
What are you expecting? What do you get? What is the problem? J On 4 December 2012 06:01, anoumou teko_maur...@yahoo.fr wrote: Hello all, I need a help. I am modeling a disease and a create a R function like that: Lambda-function (x,date1,r,h,a){ ndate1 - as.Date(date1, %d/%m/%Y) t1 - as.numeric(ndate1) x[order(x$i),] t -x[,t] i -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(t) ){ for (i in 1:length(i) ){ {for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } } x-data.frame(x,lambda) x } on such data : DATEi Symptomes t Incubation CONTAGIEUX 1 2009-04-29 Canada 13 14363 13 13 2 2009-05-01 Israel 2 14365 2 2 3 2009-05-09 argentina 1 14373 1 1 5 2009-05-09 australia 1 14373 1 1 6 2009-05-10 australia 1 14374 2 2 7 2009-04-29 Austria 1 14363 1 1 8 2009-04-30 Austria 1 14364 2 2 9 2009-05-01 Austria 1 14365 2 3 10 2009-05-02 Austria 1 14366 2 4 11 2009-05-03 Austria 1 14367 2 5 17 2009-05-09 Austria 1 14373 2 7 18 2009-05-10 Austria 1 14374 2 7 19 2009-05-08 brasil 4 14372 4 4 20 2009-05-09 brazil 6 14373 6 6 21 2009-05-10 brazil 6 14374 12 12 22 2009-05-02 canada 51 14366 51 51 23 2009-05-03 canada 85 14367 136 136 24 2009-05-04 canada 101 14368 186 237 31 2009-04-27 Canada 6 14361 6 6 32 2009-04-28 Canada 6 14362 6 6 33 2009-04-30 Canada 19 14364 25 25 34 2009-05-01 Canada 34 14365 53 59 35 2009-05-01 China,HongKong, SAR 1 14365 1 1 36 2009-05-02 China,HongKong, SAR 1 14366 2 2 37 2009-05-03 China,HongKong, SAR 1 14367 2 3 38 2009-05-04 China,HongKong, SAR 1 14368 2 4 44 2009-05-10 China,HongKong, SAR 1 14374 2 7 45 2009-05-04 colombia1 14368 1 1 46 2009-05-05 colombia1 14369 2 2 47 2009-05-06 colombia But i do not get the results,i try by all means but i d'ont understant the problem. Thanks for your help. -- View this message in context: http://r.789695.n4.nabble.com/Help-for-a-function-tp4652054.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for a function
On 12/05/2012 01:01 AM, anoumou wrote: Hello all, I need a help. I am modeling a disease and a create a R function like that: ... But i do not get the results,i try by all means but i d'ont understant the problem. Hi anoumou, Your function provides almost no indication of what two of its five arguments are supposed to be. If, with a certain degree of optimistic inference, we suppose x to be a data frame organized as shown below the function. t, i and CONTAGIEUX must be the appropriately named columns of the data frame. This leaves two columns, Symptomes and Incubation. Say we flip a coin to decide which of these to assign to r, and with all of our degrees of freedom gone, we assume that the other is h. We are forced to the conclusion that a is a nuisance argument, added to throw us off the scent. Peering within the function, we notice that the date format is wrong, braces are unmatched and that our data frame is alphabetically ordered by name of country to no purpose whatsoever. The best I can do here is to make the function potentially able to do something if you can work out what to do with it. Lambda-function (x,date1,r,h,a) { ndate1 - as.Date(date1, %Y-%m-%d) t1 - as.numeric(ndate1) x[order(x$i),] xt -x[,t] xi -x[,i] CONTAGIEUX -x[,CONTAGIEUX] while ( t1 min(xt) ){ for (i in 1:length(xi) ){ for (j in 1:CONTAGIEUX[length(CONTAGIEUX)]){ res1[j] -(a*h) res2 -sum( res1[j]) } } lambda[i] - r*res2 } x-data.frame(x,lambda) x } Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with predict function in glm
Hello, Why mail a question just to me? Post to the list and the odds of getting more answers (and better) are bigger. As for your question, the problem is in the call to glm, you don't need the prefix 'train$' in the formula, the argument 'data' solves that and when predicting R will look for the columns with names in the formula and is unable to find columns called train$Outcome and train$Weight in the new data.frame 'test'. Corrected: mylogit - glm(Outcome ~ Weight, data=train, family = binomial(logit)) predictions - predict(mylogit, newdata = test, type= response) Hope this helps, Rui Barradas Em 26-11-2012 01:42, somnath bandyopadhyay escreveu: Hi, I am trying some basic logistic regression analysis using glm. I just have one dependent variable (Outcome) which is binary in nature and one independent variable (Weight). I fit a model using a training data set (train) which has 85 observations and try to apply it on an independent dataset (test) which has 55 observations. When I apply the predict function on the fitted model for the new dataset, I get the following warning Warning message: 'newdata' had 55 rows but variable(s) found have 85 rows and the predict works on the training observations and not on the test observations. Following is he session info, code and the training and test datasets I am using. What am I doing wrong? Any help would be greatly appreciated. Thanks, S. train - read.table(train_data.txt, header=T, row.names=1, sep=\t) test- read.table(test_data.txt, header=T, row.names=1, sep=\t) mylogit - glm(train$Outcome ~ train$Weight, data=train, family = binomial(logit)) predictions - predict(mylogit, newdata = test, type= response) Warning message: 'newdata' had 55 rows but variable(s) found have 85 rows sessionInfo() R version 2.15.0 (2012-03-30) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base train Outcome Weight AB256939_21 0 0.331 AB257076_21 0 0.308 AB257079_21 0 0.453 AB415508_21 0 0.303 AB700497_21 0 0.354 AB904508_21 0 0.336 AC048719_21 0 0.420 AC185939_21 0 0.249 AC185940_21 0 1.525 AC445840_21 0 0.261 E7490523_21 0 0.269 E7490524_21 0 0.213 E7659579_21 0 0.360 E7661528_21 0 0.271 E7781094_21 0 0.156 E7781095_21 0 0.221 E7781096_21 0 0.098 E7969081_21 0 0.430 E8117594_21 0 0.321 E8133295_21 0 0.166 E8161578_22 0 0.269 E8483037_21 0 0.162 E8559720_21 0 0.226 L1065550_18 0 0.396 L1065607_17 0 0.541 L1065944_24 0 0.131 L1066017_20 0 0.421 L1069261_12 0 0.357 L1069262_14 0 0.309 L1069263_27 0 0.283 L1069297_24 0 0.620 L1081528_21 0 0.561 L1084066_21 0 0.564 L1086090_21 0 0.649 L1104280_17 0 0.181 L362_22 0 0.199 L1118063_15 0 0.369 L1133550_21 0 0.302 L1144201_14 0 0.249 L1155023_7 0 0.257 L1158386_21 0 0.470 L1163051_4 0 0.446 ... ... ... test Weight AB256870_21 0.364 AB256873_21 0.329 AB415518_21 0.219 AB460669_21 0.481 AB609036_21 0.313 AB609038_21 0.196 AB700495_21 0.402 AB700498_21 0.343 AC112834_21 0.372 AC185937_21 0.270 AC269527_21 0.285 E7352023_21 0.358 E7661554_21 0.471 E7750502_21 0.437 E7845183_21 0.232 E7854155_21 0.474 E7854156_21 0.121 E7924877_21 0.312 E7969079_21 0.423 E8139256_21 0.329 E8161577_22 1.060 E8161580_21 0.157 E8364473_21 0.227 E8364474_21 0.069 L1065940_14 0.256 L1065946_10 0.184 L1066018_25 0.282 L1069260_15 1.094 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with predict function in glm
That did it! Thanks so much as always. I emailed the question to you because I think you are an R expert based on all the suggestions, feedback and codes I have received from you in the past. Yes, I do look for answers in the open forum but when it comes to a question for which the answers out there are not very clear I prefer to ask somebody whom I can trust. I hope I donât bother you with my questions. Thanks once again. Cheers! -Som. Date: Mon, 26 Nov 2012 03:33:43 -0800 From: ml-node+s789695n4650829...@n4.nabble.com To: genome1...@hotmail.com Subject: Re: Help with predict function in glm Hello, Why mail a question just to me? Post to the list and the odds of getting more answers (and better) are bigger. As for your question, the problem is in the call to glm, you don't need the prefix 'train$' in the formula, the argument 'data' solves that and when predicting R will look for the columns with names in the formula and is unable to find columns called train$Outcome and train$Weight in the new data.frame 'test'. Corrected: mylogit - glm(Outcome ~ Weight, data=train, family = binomial(logit)) predictions - predict(mylogit, newdata = test, type= response) Hope this helps, Rui Barradas Em 26-11-2012 01:42, somnath bandyopadhyay escreveu: Hi, I am trying some basic logistic regression analysis using glm. I just have one dependent variable (Outcome) which is binary in nature and one independent variable (Weight). I fit a model using a training data set (train) which has 85 observations and try to apply it on an independent dataset (test) which has 55 observations. When I apply the predict function on the fitted model for the new dataset, I get the following warning Warning message: 'newdata' had 55 rows but variable(s) found have 85 rows and the predict works on the training observations and not on the test observations. Following is he session info, code and the training and test datasets I am using. What am I doing wrong? Any help would be greatly appreciated. Thanks, S. train - read.table(train_data.txt, header=T, row.names=1, sep=\t) test- read.table(test_data.txt, header=T, row.names=1, sep=\t) mylogit - glm(train$Outcome ~ train$Weight, data=train, family = binomial(logit)) predictions - predict(mylogit, newdata = test, type= response) Warning message: 'newdata' had 55 rows but variable(s) found have 85 rows sessionInfo() R version 2.15.0 (2012-03-30) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base train Outcome Weight AB256939_21 0 0.331 AB257076_21 0 0.308 AB257079_21 0 0.453 AB415508_21 0 0.303 AB700497_21 0 0.354 AB904508_21 0 0.336 AC048719_21 0 0.420 AC185939_21 0 0.249 AC185940_21 0 1.525 AC445840_21 0 0.261 E7490523_21 0 0.269 E7490524_21 0 0.213 E7659579_21 0 0.360 E7661528_21 0 0.271 E7781094_21 0 0.156 E7781095_21 0 0.221 E7781096_21 0 0.098 E7969081_21 0 0.430 E8117594_21 0 0.321 E8133295_21 0 0.166 E8161578_22 0 0.269 E8483037_21 0 0.162 E8559720_21 0 0.226 L1065550_18 0 0.396 L1065607_17 0 0.541 L1065944_24 0 0.131 L1066017_20 0 0.421 L1069261_12 0 0.357 L1069262_14 0 0.309 L1069263_27 0 0.283 L1069297_24 0 0.620 L1081528_21 0 0.561 L1084066_21 0 0.564 L1086090_21 0 0.649 L1104280_17 0 0.181 L362_22 0 0.199 L1118063_15 0 0.369 L1133550_21 0 0.302 L1144201_14 0 0.249 L1155023_7 0 0.257 L1158386_21 0 0.470 L1163051_4 0 0.446 ... ... ... test Weight AB256870_21 0.364 AB256873_21 0.329 AB415518_21 0.219 AB460669_21 0.481 AB609036_21 0.313 AB609038_21 0.196 AB700495_21 0.402 AB700498_21 0.343 AC112834_21 0.372 AC185937_21 0.270 AC269527_21 0.285 E7352023_21 0.358 E7661554_21 0.471 E7750502_21 0.437 E7845183_21 0.232 E7854155_21 0.474 E7854156_21 0.121 E7924877_21 0.312 E7969079_21 0.423 E8139256_21 0.329 E8161577_22 1.060 E8161580_21 0.157 E8364473_21 0.227 E8364474_21 0.069 L1065940_14 0.256 L1065946_10 0.184 L1066018_25 0.282 L1069260_15 1.094 __ [hidden email] 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. If you reply to this email, your message will be added to the discussion below:
[R] Help with glht function and binary data!
Hi, I carried out an experiment, using a repeated measures design, in which I broadcast 6 playbacks to 15 nests. I am trying to run a posthoc pairwise comparison on a binomial GLMM with missing data points to determine which playback treatments differ. The response variable (Response) is binary, there is one categorical factor (Treatment) and Nest is included as a random factor. I have run a binomial GLMM using the lme4 package and found the effect of Treatment to be highly significant. I followed this up using the glht function in the multcomp package to look at the pairwise comparisons. However, it is giving me strange results for some of the comparisons, producing p-values of 1 for comparisons between what graphically (plot of mean and std error/boxplots) appear to be the most different treatments. For my other binomial GLMM, where I look at proportion rather than binary data, the glht function yields the expected results without problems. Can anyone help? Is the glht function not appropriate for binary data? Or have I got something wrong? Thank you! Jessica My data and code are below. # My data: Nest-c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,13,13,13,14,14,14,14,15,15,15,15,15) Treatment-factor(c(CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CRS,CL,CS,SL,SS,CRL,CL,CS,SL,SS,CRL,CS,SL,SS,CRL,CRS,CS,SL,SS)) Response-c(0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,1,0,0,1,0,1,1,0,0,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,0,0,0,1,1,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1,1) binomialGLMM-data.frame(Nest,Treatment,Response) # Binomial GLMM library(lme4) lmm1-lmer(Response~Treatment+(1|factor(Nest)),data=binomialGLMM,family=binomial) lmm2-lmer(Response~1+(1|factor(Nest)),data=binomialGLMM,family=binomial) anova(lmm1,lmm2) Data: binomialGLMM Models: lmm2: Response ~ 1 + (1 | factor(Nest)) lmm1: Response ~ Treatment + (1 | factor(Nest)) Df AIC BIC logLik Chisq Chi Df Pr(Chisq) lmm2 2 109.405 114.314 -52.703 lmm1 7 57.013 74.193 -21.506 62.392 5 3.889e-12 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # Post-hoc pairwise comparison library(multcomp) posthoc-glht(lmm1, linfct = mcp(Treatment = Tukey)) summary(posthoc) Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: Tukey Contrasts Fit: glmer(formula = Response ~ Treatment + (1 | factor(Nest)), data = binomialGLMM, family = binomial) Linear Hypotheses: Estimate Std. Error z value Pr(|z|) !!! *CRL - CL == 0 -2.088e+01 4.581e+03 -0.005 1.0* !!! *CRS - CL == 0 -2.247e+01 4.581e+03 -0.005 1.0* CS - CL == 0 -1.652e+01 4.581e+03 -0.004 1.0 SL - CL == 09.148e-07 6.276e+03 0.000 1.0 SS - CL == 0 -1.735e+01 4.581e+03 -0.004 1.0 CRS - CRL == 0 -1.595e+00 1.348e+00 -1.183 0.79608 CS - CRL == 0 4.359e+00 1.359e+00 3.206 0.01107 * !!! *SL - CRL == 0 2.088e+01 4.290e+03 0.005 1.0 * SS - CRL == 0 3.531e+00 1.072e+00 3.294 0.00790 ** CS - CRS == 0 5.953e+00 1.700e+00 3.502 0.00382 ** !!! *SL - CRS == 0 2.247e+01 4.290e+03 0.005 1.0* SS - CRS == 0 5.125e+00 1.480e+00 3.464 0.00421 ** SL - CS == 01.652e+01 4.290e+03 0.004 1.0 SS - CS == 0 -8.279e-01 1.465e+00 -0.565 0.98998 SS - SL == 0 -1.735e+01 4.290e+03 -0.004 1.0 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Adjusted p values reported -- single-step method) !!! on the left indicates unexpected p-values. -- View this message in context: http://r.789695.n4.nabble.com/Help-with-glht-function-and-binary-data-tp4650222.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help using optim function
tmuman mumantariq at gmail.com writes: Hi, am very new to R and I've written an optim function, but can't get it to work least.squares.fitter-function(start.params,gr, low.constraints,high.constraints,model.one.stepper,data,scale,ploton=F) { result-optim(par=start.params, method=c('Nelder-Mead'), fn=least.squares.fit, lower=low.constraints, upper=high.constraints,data=data,scale=scale,ploton=ploton) return(result) } least.squares.fitter(c(2,2),c(0,0),c(Inf,Inf),ricker.one.step,dat,'linear') where least.squares.fit is the function returning the difference between model data points and predicted points (residuals, residuals^2 and SSR), which i am trying to minimise and ricker.one.step is the function that generated the predictions I've been trying to figure out whats wrong for hours but can't. You haven't given us much to work with here -- have you read the posting guide (URL at the bottom of every message)? It's not reproducible ( http://tinyurl.com/reproducible-000 ), and you haven't told us what's going wrong. Are you getting error messages? What are they? Warnings? No errors but you have reason to believe the answer is wrong? A few comments: * 'scale' and 'ploton' are not arguments to optim(), so they'd better be accepted by your least.squares.fit() function * the Nelder-Mead algorithm doesn't accept constraints (lower/upper) (see the nloptr package for a Nelder-Mead implementation that does) * except for the constraints, you might be better off with ?nls if all you want to do is least-squares fitting; nlsLM in the minpack.lm package does least-squares fitting with constraints __ 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 using optim function
Hi, am very new to R and I've written an optim function, but can't get it to work least.squares.fitter-function(start.params,gr,low.constraints,high.constraints,model.one.stepper,data,scale,ploton=F) { result-optim(par=start.params,method=c('Nelder-Mead'),fn=least.squares.fit,lower=low.constraints,upper=high.constraints,data=data,scale=scale,ploton=ploton) return(result) } least.squares.fitter(c(2,2),c(0,0),c(Inf,Inf),ricker.one.step,dat,'linear') where least.squares.fit is the function returning the difference between model data points and predicted points (residuals, residuals^2 and SSR), which i am trying to minimise and ricker.one.step is the function that generated the predictions I've been trying to figure out whats wrong for hours but can't. Many thanks, Tariq -- View this message in context: http://r.789695.n4.nabble.com/help-using-optim-function-tp4647231.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] help on plotfit function in package nlstools
I encountered this problem when I was learning nonlinear regression with R : there is this function plotfit in package nlstools,which displays a superimposed plot of the dependent variable versus one the independent variables together with the fitted model.,but when I execute the example given in the help page, it keeps saying that : plotfit(nls1, smooth = TRUE) Error in form[[3]] : object of type 'symbol' is not subsettable can anybody tell me what this means and how to fix it ? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with rle function on paired data
hello, I have a question regarding the function WebCorpus..When I am using this function its showing error: couldnt find the function...I have downloaded the tm package...Can you help me in this regard? Also I want to know how we can data/information about a product or topic from google,facebook etc using R...I have find a command corpus - WebCorpus(GoogleFinanceSource(AAPL)) but this is not working and showing error couldnt find the function GoogleFinanceSource... please help me ..I will be thankful to you thanking you in anticipation Shilpa Rai MSc.(2011-2013) Applied Statistics and Informatics Indian Institute of Technology,Bombay -- View this message in context: http://r.789695.n4.nabble.com/help-with-rle-function-on-paired-data-tp4632856p4632869.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with rle function on paired data
ldiff - function(x)c(TRUE, x[-1]!=x[-length(x)]) # logical diff function d[ldiff(d$DataValue), ] ElpsdTime DataValue 1 0 1 490 2 6 150 3 7 180 2 8 210 3 ldiff does the first half of what rle does. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Steve E. Sent: Friday, June 08, 2012 2:33 PM To: r-help@r-project.org Subject: [R] help with rle function on paired data Dear R Community - I hope you might be able to provide some guidance regarding the use of the rle function. I have a set of time-series data where a measured value is recorded every 30 seconds after the start of an experiment. Many of the measured values repeat and I am interested only in the values when there is a change. If I turn the measured values into a vector, the rle function works perfectly for this but I need also the corresponding time of the value and I am not sure how to use rle on paired data. Below is a brief example to help explain the problem. I thank you in advance for any assistance you might be able to provide. Regards, Steve Original dataset: ElpsdTime, DataValue 0, 1 30, 1 60, 1 90, 2 120, 2 150, 3 180, 2 210, 3 240, 3 . . Desired dataset: ElpTime DataValue 0, 1 90, 2 150, 3 180, 2 210, 3 . . -- View this message in context: http://r.789695.n4.nabble.com/help-with-rle-function-on- paired-data-tp4632856.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with permutation function from Turner et al. 2010 (Ecology)
On 09.06.2012 03:57, Ashley Stasko wrote: Hello, I'm using R code that includes a residual permutation that was written as a supplement to the paper: Turner et al. 2010. A general hypothesis-testing framework for stable isotopes ratios in ecological studies. Ecology 91:2227-2233. The supplemental code is available at: http://www.esapubs.org/archive/ecol/E091/157/suppl-1.htm When I execute the function, no warnings are given (seems fine). But the resulting output matrix is simply a matrix of repeating rows (i.e., it appears as if the function is selecting the same random sample every time). The function is as follows (please keep in mind that many of the internal functions were written by Turner et al. as well and may not be familiar): line.1-c(0,traj.result(traj.size,traj.dir,traj.shape)) permute-function(y,x2,g,o,p) { p.table-line.1 yhat-predict(x2) res-resid(x2) line-nrow(y) for(i in 1:p){ line.rand-sample(line,replace=FALSE) res.temp-cbind(line.rand,res) z-(order(line.rand)) res.temp2-as.matrix(res.temp[z,]) res.p-res.temp2[,-1] # Rows of residuals are now randomized y.rand-yhat+res.p lm.new-lm(y.rand~o*g,x=T,model=T) ls-data.frame(expand.grid(o=levels(o),g=levels(g))) ls[]-lapply(ls, factor) means.r-predict(lm.new,ls) traj.r-arrayspecs(means.r,g,o) traj.size.r-trajsize(traj.r) traj.dir.r-trajorient(traj.r,ang.cor=T) traj.shape.r-trajshape(traj.r) result-traj.result(traj.size.r,traj.dir.r,traj.shape.r) p.table-rbind(p.table,c(i,result)) } p.table } But when I try to execute the function the rows in the output matrix repeat the same numbers: test3-permute(Y,lm.2f.red,group,obs,99) test3 [,1] [,2] [,3] [,4] p.table0 3.612322 0.9910104 0.3998703 1 3.612322 0.9910104 0.3998703 2 3.612322 0.9910104 0.3998703 3 3.612322 0.9910104 0.3998703 4 3.612322 0.9910104 0.3998703 5 3.612322 0.9910104 0.3998703 .. Can anyone see where there may be a problem with the permutation code? I'm relatively new to functions, and especially to writing permutation functions, so I'm having difficulty finding the problem. If the results are suspicious (we cannot reproduce easily since you have not provided reproducible code as the posting guide asks you), why not ask the authors of the code? They hopefully know better what they aimed to get than readers of this list. Best, Uwe Ligges Thanks, Ashley Ashley Stasko M.Sc. Candidate Cooperative Freshwater Ecology Unit Living With Lakes Centre Laurentian University Sudbury, ON Tel: 705-675-1151 ext. 2209 Email: ax_sta...@laurentian.ca __ 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] Help with permutation function from Turner et al. 2010 (Ecology)
Dear Ashley, I am one of the co-authors for these scripts. I just downloaded the example files from the ESA archives and ran in naively on some data and it worked fine. My guess is that the problem lies in a portion of the script that you did not provide. If I had to be more certain with my guess, I bet your 'y' data are the predicted values from a linear model instead of the raw data. Therefore, when the permutation procedure tries to shuffle residuals, there are no residuals to shuffle. If this is not the case, please email me and I can look at the rest of your adapted script. My new email address is michael (dot) collyer (at) wku (dot) edu. By the way, one regret that we had after publishing these R scripts in the ESA archives was to not provide a link for updates. ESA is going to allow us to make an amendment. Look for this change in the near future. We are in the process of streamlining the original functions a bit and plan to make these updates available. Sincerely, Michael Collyer Western Kentucky University, Department of Biology -- View this message in context: http://r.789695.n4.nabble.com/Help-with-permutation-function-from-Turner-et-al-2010-Ecology-tp4632865p4632902.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] help with rle function on paired data
Dear R Community - I hope you might be able to provide some guidance regarding the use of the rle function. I have a set of time-series data where a measured value is recorded every 30 seconds after the start of an experiment. Many of the measured values repeat and I am interested only in the values when there is a change. If I turn the measured values into a vector, the rle function works perfectly for this but I need also the corresponding time of the value and I am not sure how to use rle on paired data. Below is a brief example to help explain the problem. I thank you in advance for any assistance you might be able to provide. Regards, Steve Original dataset: ElpsdTime, DataValue 0, 1 30, 1 60, 1 90, 2 120, 2 150, 3 180, 2 210, 3 240, 3 . . Desired dataset: ElpTime DataValue 0, 1 90, 2 150, 3 180, 2 210, 3 . . -- View this message in context: http://r.789695.n4.nabble.com/help-with-rle-function-on-paired-data-tp4632856.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with rle function on paired data
On 2012-06-08 14:33, Steve E. wrote: Dear R Community - I hope you might be able to provide some guidance regarding the use of the rle function. I have a set of time-series data where a measured value is recorded every 30 seconds after the start of an experiment. Many of the measured values repeat and I am interested only in the values when there is a change. If I turn the measured values into a vector, the rle function works perfectly for this but I need also the corresponding time of the value and I am not sure how to use rle on paired data. Below is a brief example to help explain the problem. I thank you in advance for any assistance you might be able to provide. Regards, Steve Original dataset: ElpsdTime, DataValue 0, 1 30, 1 60, 1 90, 2 120, 2 150, 3 180, 2 210, 3 240, 3 . . Desired dataset: ElpTime DataValue 0, 1 90, 2 150, 3 180, 2 210, 3 . . Let's call your dataframe 'dat'. Here's a pretty brute-force way: z - rle(dat[, 2]) zV - z$values zL - z$lengths idx - c(0, head(cumsum(zL), -1)) + 1 newdat - data.frame(time = dat[idx, 1], value = zV) newdat Peter Ehlers __ 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 with permutation function from Turner et al. 2010 (Ecology)
Hello, I'm using R code that includes a residual permutation that was written as a supplement to the paper: Turner et al. 2010. A general hypothesis-testing framework for stable isotopes ratios in ecological studies. Ecology 91:2227-2233. The supplemental code is available at: http://www.esapubs.org/archive/ecol/E091/157/suppl-1.htm When I execute the function, no warnings are given (seems fine). But the resulting output matrix is simply a matrix of repeating rows (i.e., it appears as if the function is selecting the same random sample every time). The function is as follows (please keep in mind that many of the internal functions were written by Turner et al. as well and may not be familiar): line.1-c(0,traj.result(traj.size,traj.dir,traj.shape)) permute-function(y,x2,g,o,p) { p.table-line.1 yhat-predict(x2) res-resid(x2) line-nrow(y) for(i in 1:p){ line.rand-sample(line,replace=FALSE) res.temp-cbind(line.rand,res) z-(order(line.rand)) res.temp2-as.matrix(res.temp[z,]) res.p-res.temp2[,-1] # Rows of residuals are now randomized y.rand-yhat+res.p lm.new-lm(y.rand~o*g,x=T,model=T) ls-data.frame(expand.grid(o=levels(o),g=levels(g))) ls[]-lapply(ls, factor) means.r-predict(lm.new,ls) traj.r-arrayspecs(means.r,g,o) traj.size.r-trajsize(traj.r) traj.dir.r-trajorient(traj.r,ang.cor=T) traj.shape.r-trajshape(traj.r) result-traj.result(traj.size.r,traj.dir.r,traj.shape.r) p.table-rbind(p.table,c(i,result)) } p.table } But when I try to execute the function the rows in the output matrix repeat the same numbers: test3-permute(Y,lm.2f.red,group,obs,99) test3 [,1] [,2] [,3] [,4] p.table0 3.612322 0.9910104 0.3998703 1 3.612322 0.9910104 0.3998703 2 3.612322 0.9910104 0.3998703 3 3.612322 0.9910104 0.3998703 4 3.612322 0.9910104 0.3998703 5 3.612322 0.9910104 0.3998703 .. Can anyone see where there may be a problem with the permutation code? I'm relatively new to functions, and especially to writing permutation functions, so I'm having difficulty finding the problem. Thanks, Ashley Ashley Stasko M.Sc. Candidate Cooperative Freshwater Ecology Unit Living With Lakes Centre Laurentian University Sudbury, ON Tel: 705-675-1151 ext. 2209 Email: ax_sta...@laurentian.ca __ 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 for numericDeriv function
Hi, I am stuck on something for a couple days, I am almost about to give up. This looks simple, but I can't figure out. I hope I can get some help here. I am trying to do some symbolic and numerical derivations. Let me explain the problem. Let's say, I have a matrix as follows: load - matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE) load [,1] [,2] [1,]30 [2,]14 [3,]13 I will create objects for each element of the matrix, and assign the matrix elements to these objects. These objects will be also an element of another matrix. l - matrix(nrow=nrow(load),ncol=ncol(load)) for(i in 1:nrow(load)) { for(j in 1:ncol(load)) { l[i,j]=paste(l,i,j,sep=)}} l [,1] [,2] [1,] l11 l12 [2,] l21 l22 [3,] l31 l32 l11 [1] 3 l12 [1] 0 l21 [1] 1 l22 [1] 4 l31 [1] 1 l32 [1] 3 Let's say, I need to take the derivative of (l11*l21+l12*l22) with respect to l11,l12,l21,l22,l31,l32. numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE)) [1] 3 attr(,gradient) [,1] [,2] [,3] [,4] [,5] [,6] [1,]143000 Everything is fine up to this point. I need what I have. But, I don't want to manipulate the elements in quote() manually within the numericDeriv function. For instance, I want to call these elements from the matrix l as below: a - paste( + paste(l[1,1],l[2,1],sep=*), + paste(l[1,2],l[1,2],sep=*), + sep=+ + ) a [1] l11*l21+l12*l12 Now, a is a character. Do you have any idea about how to use the character value l11*l21+l12*l12 assigned to object a within the numericDeriv function. So, I can get the same result as above without manipulating the expression in quote() manually. Thanks -- Cengiz Zopluoglu [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for numericDeriv function
I missed a couple line of codes in the previous e-mail. Here is whole code again: load - matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE) l - matrix(nrow=nrow(load),ncol=ncol(load)) for(i in 1:nrow(load)) { for(j in 1:ncol(load)) { l[i,j]=paste(l,i,j,sep=)}} for(i in 1:nrow(load)){ for(j in 1:ncol(load)){ assign(paste(l,i,j,sep=),load[i,j]) }} numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE)) a - paste( paste(l[1,1],l[2,1],sep=*), paste(l[1,2],l[1,2],sep=*), sep=+ ) numericDeriv(???a???, c(t(l),recursive=TRUE)) Thanks On Fri, May 18, 2012 at 1:25 PM, Cengiz ZopluoÄlu cen@gmail.com wrote: Hi, I am stuck on something for a couple days, I am almost about to give up. This looks simple, but I can't figure out. I hope I can get some help here. I am trying to do some symbolic and numerical derivations. Let me explain the problem. Let's say, I have a matrix as follows: load - matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE) load [,1] [,2] [1,]30 [2,]14 [3,]13 I will create objects for each element of the matrix, and assign the matrix elements to these objects. These objects will be also an element of another matrix. l - matrix(nrow=nrow(load),ncol=ncol(load)) for(i in 1:nrow(load)) { for(j in 1:ncol(load)) { l[i,j]=paste(l,i,j,sep=)}} l [,1] [,2] [1,] l11 l12 [2,] l21 l22 [3,] l31 l32 l11 [1] 3 l12 [1] 0 l21 [1] 1 l22 [1] 4 l31 [1] 1 l32 [1] 3 Let's say, I need to take the derivative of (l11*l21+l12*l22) with respect to l11,l12,l21,l22,l31,l32. numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE)) [1] 3 attr(,gradient) [,1] [,2] [,3] [,4] [,5] [,6] [1,]143000 Everything is fine up to this point. I need what I have. But, I don't want to manipulate the elements in quote() manually within the numericDeriv function. For instance, I want to call these elements from the matrix l as below: a - paste( + paste(l[1,1],l[2,1],sep=*), + paste(l[1,2],l[1,2],sep=*), + sep=+ + ) a [1] l11*l21+l12*l12 Now, a is a character. Do you have any idea about how to use the character value l11*l21+l12*l12 assigned to object a within the numericDeriv function. So, I can get the same result as above without manipulating the expression in quote() manually. Thanks -- Cengiz Zopluoglu -- Cengiz Zopluoglu [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help for numericDeriv function
You could use parse(text=a)[[1]]: numericDeriv(parse(text=a)[[1]], c(t(l),recursive=TRUE)) [1] 3 attr(,gradient) [,1] [,2] [,3] [,4] [,5] [,6] [1,]103000 or construct your expression directly: lnames - array(lapply(l, as.name), dim=dim(l)) aa - call(+, call(*, lnames[[1,1]], lnames[[2,1]]), call(*, lnames[[1,2]], lnames[[1,2]])) numericDeriv(aa, c(t(l),recursive=TRUE)) [1] 3 attr(,gradient) [,1] [,2] [,3] [,4] [,5] [,6] [1,]103000 The results are identical. The latter method is a bit more work to set up but works no matter what the names in the expression look like. The former fails if the names contain spaces or other things that are not allowed in R names. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Cengiz Zopluoglu Sent: Friday, May 18, 2012 11:33 AM To: r-help@r-project.org Subject: Re: [R] Help for numericDeriv function I missed a couple line of codes in the previous e-mail. Here is whole code again: load - matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE) l - matrix(nrow=nrow(load),ncol=ncol(load)) for(i in 1:nrow(load)) { for(j in 1:ncol(load)) { l[i,j]=paste(l,i,j,sep=)}} for(i in 1:nrow(load)){ for(j in 1:ncol(load)){ assign(paste(l,i,j,sep=),load[i,j]) }} numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE)) a - paste( paste(l[1,1],l[2,1],sep=*), paste(l[1,2],l[1,2],sep=*), sep=+ ) numericDeriv(???a???, c(t(l),recursive=TRUE)) Thanks On Fri, May 18, 2012 at 1:25 PM, Cengiz ZopluoÄŸlu cen@gmail.com wrote: Hi, I am stuck on something for a couple days, I am almost about to give up. This looks simple, but I can't figure out. I hope I can get some help here. I am trying to do some symbolic and numerical derivations. Let me explain the problem. Let's say, I have a matrix as follows: load - matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE) load [,1] [,2] [1,]30 [2,]14 [3,]13 I will create objects for each element of the matrix, and assign the matrix elements to these objects. These objects will be also an element of another matrix. l - matrix(nrow=nrow(load),ncol=ncol(load)) for(i in 1:nrow(load)) { for(j in 1:ncol(load)) { l[i,j]=paste(l,i,j,sep=)}} l [,1] [,2] [1,] l11 l12 [2,] l21 l22 [3,] l31 l32 l11 [1] 3 l12 [1] 0 l21 [1] 1 l22 [1] 4 l31 [1] 1 l32 [1] 3 Let's say, I need to take the derivative of (l11*l21+l12*l22) with respect to l11,l12,l21,l22,l31,l32. numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE)) [1] 3 attr(,gradient) [,1] [,2] [,3] [,4] [,5] [,6] [1,]143000 Everything is fine up to this point. I need what I have. But, I don't want to manipulate the elements in quote() manually within the numericDeriv function. For instance, I want to call these elements from the matrix l as below: a - paste( + paste(l[1,1],l[2,1],sep=*), + paste(l[1,2],l[1,2],sep=*), + sep=+ + ) a [1] l11*l21+l12*l12 Now, a is a character. Do you have any idea about how to use the character value l11*l21+l12*l12 assigned to object a within the numericDeriv function. So, I can get the same result as above without manipulating the expression in quote() manually. Thanks -- Cengiz Zopluoglu -- Cengiz Zopluoglu [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with V function in igraph
Something weird must be going on in your s641_social object. Can you just simply check that the vertex names look OK with 'V(s641_social)$name'? If they look good, then can you send me the s641_social object in private? (Or part of it, assuming a part is enough to reproduce the problem.) Best, Gabor On Sat, May 12, 2012 at 8:07 PM, bmccowan bjmcco...@ucdavis.edu wrote: I am using the code below to output some network measures: central_social - data.frame(V(s641_social)$name, indegree_social, outdegree_social, incloseness_social, outcloseness_social, betweenness_social, eigen_social) and I get the following error: Error in Re(z) : non-numeric argument to function I know this has to do with V but I cannot figure out what is wrong. s641-social is a graph object and the vertices do have a name attribute. What can I do to fix? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Help-with-V-function-in-igraph-tp4629767.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Gabor Csardi csa...@rmki.kfki.hu MTA KFKI RMKI __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with V function in igraph
Thank for your response. It is oddly working now. Thanks again, Brenda On 5/14/12 10:36 AM, Gábor Csárdi-2 [via R] ml-node+s789695n4629973...@n4.nabble.com wrote: Something weird must be going on in your s641_social object. Can you just simply check that the vertex names look OK with 'V(s641_social)$name'? If they look good, then can you send me the s641_social object in private? (Or part of it, assuming a part is enough to reproduce the problem.) Best, Gabor On Sat, May 12, 2012 at 8:07 PM, bmccowan [hidden email] /user/SendEmail.jtp?type=nodenode=4629973i=0 wrote: I am using the code below to output some network measures: central_social - data.frame(V(s641_social)$name, indegree_social, outdegree_social, incloseness_social, outcloseness_social, betweenness_social, eigen_social) and I get the following error: Error in Re(z) : non-numeric argument to function I know this has to do with V but I cannot figure out what is wrong. s641-social is a graph object and the vertices do have a name attribute. What can I do to fix? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Help-with-V-function-in-igraph-tp4629767.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] /user/SendEmail.jtp?type=nodenode=4629973i=1 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. -- Gabor Csardi [hidden email] /user/SendEmail.jtp?type=nodenode=4629973i=2    MTA KFKI RMKI __ [hidden email] /user/SendEmail.jtp?type=nodenode=4629973i=3 mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Help-with-V-function-in-igraph-tp4629767p4629973 .html To unsubscribe from Help with V function in igraph, click here http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_c odenode=4629767code=YmptY2Nvd2FuQHVjZGF2aXMuZWR1fDQ2Mjk3Njd8MTg1NDg1MDYx . NAML http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid= instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace- nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespac ebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble %3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/Help-with-V-function-in-igraph-tp4629767p4629989.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Help with V function in igraph
I am using the code below to output some network measures: central_social - data.frame(V(s641_social)$name, indegree_social, outdegree_social, incloseness_social, outcloseness_social, betweenness_social, eigen_social) and I get the following error: Error in Re(z) : non-numeric argument to function I know this has to do with V but I cannot figure out what is wrong. s641-social is a graph object and the vertices do have a name attribute. What can I do to fix? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Help-with-V-function-in-igraph-tp4629767.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with gsub function or a similar function
You don't provide a reproducible example, or even str(), but I'd guess you need to match ^15 instead of just 15. Sarah On Apr 5, 2012, at 10:38 PM, ieatnapalm era...@tulane.edu wrote: Hey, sorry if this has been addressed before, but I'm really new to R and having trouble with the gsub function. I need a way to make this function exclude certain values from being substituted: ie my data looks something like (15:.0234,10:.0157) and I'm trying to replace the leading 15 with something else - but of course it replaces the second 15 with something else too. If there's a way to exclude anything with a leading decimal point or something like that, that would do the trick. Thanks yall. -- View this message in context: http://r.789695.n4.nabble.com/Help-with-gsub-function-or-a-similar-function-tp4536584p4536584.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Help with gsub function or a similar function
Hey, sorry if this has been addressed before, but I'm really new to R and having trouble with the gsub function. I need a way to make this function exclude certain values from being substituted: ie my data looks something like (15:.0234,10:.0157) and I'm trying to replace the leading 15 with something else - but of course it replaces the second 15 with something else too. If there's a way to exclude anything with a leading decimal point or something like that, that would do the trick. Thanks yall. -- View this message in context: http://r.789695.n4.nabble.com/Help-with-gsub-function-or-a-similar-function-tp4536584p4536584.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with gsub function or a similar function
On Apr 5, 2012, at 10:38 PM, ieatnapalm wrote: Hey, sorry if this has been addressed before, but I'm really new to R and having trouble with the gsub function. I need a way to make this function exclude certain values from being substituted: ie my data looks something like (15:.0234,10:.0157) and I'm trying to replace the leading 15 with something else - but of course it replaces the second 15 with something else too. If there's a way to exclude anything with a leading decimal point or something like that, that would do the trick. Thanks yall. Cross-posting to SO and rhelp in quick succession is deprecated. If you hadn't gotten an answer on SO after a day or so, then fine, go a head cross-post but in point of fact you have already gotten three answer on SO so you are just wasting rhelp brain-bandwidth. -- View this message in context: http://r.789695.n4.nabble.com/Help-with-gsub-function-or-a-similar-function-tp4536584p4536584.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT __ 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.