[R] r function for calculating extreme spread in group
I'm trying to figure out how to write a r function that will calculate the extreme spread of a group of points given their (x,y) coordinates. Extreme Spread is the maximal Euclidean distance between two points in a group ex.spread = max{ sqrt [ (xi-xj)^2 - (yi-yj)^2 ] } for i not equal to j I have 60 levels to apply this to. There is the combination function in the dprep package but I wasn't sure how to use this or apply an index the points in each group. Any ideas? Steve Steven Matthew Anderson [EMAIL PROTECTED] Ad Astra per Aspera __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] r function for calculating extreme spread in group
Thank you Jorge, My talent in building functions is weak. I think I'm close but I'm not doing something right. res=dist(yourdata) res[which.max(res)] does provide me with the correct distance but how do I apply it across data with level such as ... > yourdata =as.data.frame(cbind(lvl=LETTERS[1:2],x=rpois(10,10),y=rnorm(10) )) > yourdata lvl x y 1A 10 0.14377148075807 2B 5 -0.117753598165951 3A 14 -0.912068366948338 4B 10 -1.43758624082998 5A 16 -0.797089525071965 6B 11 1.25408310644997 7A 7 0.77214218580453 8B 9 -0.219515626753440 9A 12 -0.424810283377287 10 B 13 -0.418980099421959 My attempt to use tapply blew up on me. > Extreme.Spread<-matrix(nrow=60,ncol=2) > ES<-function(Level) { +temp<- subset(x=ES.Data,Level==level,select=c(Horizontal,Vertical)) +e<-dist(rbind(temp$Horizontal,temp $Vertical),method="euclidean") +m<-e[which.max(e)] +es<-cbind(level,m) +Extreme.Spread<-rbind(Extreme.Spread,es) +} > tapply(X=ES.Data,INDEX=Level,FUN=ES) Error in tapply(X = ES.Data, INDEX = Level, FUN = ES) : arguments must have same length On Aug 27, 2008, at 2:30 PM, Jorge Ivan Velez wrote: > R-help@r-project.org Steven Matthew Anderson [EMAIL PROTECTED] Ad Astra per Aspera [[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] predicted values after fitting gamma2 function
Question: after fitting a gamma function to some data, how do I get predicted values? I'm a SAS programmer, I new R, and am having problems getting my brain to function with the concept of "object as class ...". The following is specifics of what I am doing: I'm trying to determine the pdf from data I have created in a simulation. I have generated frequency counts using the following: Max.brks <- pretty(range(Max.Spread$Distance), 100) Max.f<-hist(x=Max.Spread$Distance, breaks=Max.brks,plot=FALSE ) Max.cnt<-as.data.frame(cbind(sim,Max.f$mids,Max.f$counts)) colnames(Max.cnt)<-c("Simulation","MidPoint","Count") then I fit this to a gamma distribution function: modl<- vglm (Count ~ MidPoint ,gamma2 ,data =subset(Max.cnt,select=(simulation,MidPoint,Count),trace=TRUE,crit="c") print(coef(modl2,matrix=TRUE)) print(summary(modl2)) This produces the output: VGLMlinear loop 1 : coefficients = 3.231473495, 0.597085743, -0.195591168 ... VGLMlinear loop 20 : coefficients = 3.663316903, 0.897355891, -0.620449146 log(mu) log(shape) (Intercept) 3.6633169 0.8973559 MidPoint-0.6204491 0.000 Call: vglm(formula = Count ~ MidPoint, family = gamma2, data = modl.subset, trace = TRUE, crit = "c") Pearson Residuals: Min1Q Median 3Q Max log(mu)-1.4846 -0.715285 -0.15436 0.61641 2.17298 log(shape) -5.8348 -0.099617 0.42094 0.61865 0.70901 Coefficients: Value Std. Error t value (Intercept):1 3.66332 0.131643 27.8277 (Intercept):2 0.89736 0.138591 6.4749 MidPoint -0.62045 0.047505 -13.0607 Number of linear predictors: 2 Names of linear predictors: log(mu), log(shape) Dispersion Parameter for gamma2 family: 1 Log-likelihood: -276.0009 on 181 degrees of freedom Number of Iterations: 20 Now - how do I get this information to give me predicted values given the same x-values I used in the experimental model (i.e. from Max.brks <- pretty(range(Max.Spread$Distance), 100)). Steven Matthew Anderson Anderson Research, LLC Statistical Programming and Analysis SAS (R) Certified Professional adastr...@mac.com Ad Astra per Aspera שָׁלוֹם __ 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] exporting data to csv file -problem with column names
I have a data.frame with three columns + the row number Sample MidPoint Count [1,] 10.025 1 [2,] 10.075 3 [3,] 10.12515 [4,] 10.17512 [5,] 10.225 5 [6,] 10.275 8 When I export the data to a csv the column names are shifted because the row numbers are being exported as well. Sample MidPointCount 1 1 0.025 1 2 1 0.075 3 3 1 0.125 15 Is there a way to exclude the row numbers from the exported csv file? Or add a column name for row number to keep this from happening? I obvisouly can go into each csv file and shift the column names over - but there are going to be a large number of csv files so I with to automate this. Thank you [[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] variable driven csv file names?
Is it possible to have variable driven csv file names? Such as: ds.name<-"bob.csv" write.table( distribution.data, file = "~//Documents/Research/Distribution Analysis/ds.name", sep = ",", col.names = FALSE, qmethod = "double") Steven Matthew Anderson Anderson Research, LLC Statistical Programming and Analysis SAS (R) Certified Professional adastr...@mac.com Ad Astra per Aspera שָׁלוֹם __ 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] turn off graphic plot in histogram()
Is there a way to just produce the data object in histogram() and not run the plot portion? All I want it the $mids and $counts. Thank you Steven Matthew Anderson Anderson Research, LLC Statistical Programming and Analysis SAS (R) Certified Professional adastr...@mac.com Ad Astra per Aspera שָׁלוֹם __ 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] hist returning density larger than 1
The following code is giving me problems. I want to export densities of a distribution to a csv file. At the bottom of the code I use the hist function to generate the densities. But hist is returning values greater than 1. I don't understand, why. Any help you can supply is greatly appreciated. # Set word path dir<-"~/Research/MR Distribution Analysis/" setwd(dir) getwd() # Define Objects Max.Shots=3 Max.Groups=5000 hist.brks <- pretty(range(c(0,4)), 200) for(sim in (2:Max.Shots)){ x.Data<-matrix(nrow=Max.Groups,ncol=sim,byrow=TRUE) y.Data<-matrix(nrow=Max.Groups,ncol=sim,byrow=TRUE) shot2mean.x<-matrix(nrow=Max.Groups,ncol=sim,byrow=TRUE) shot2mean.y<-matrix(nrow=Max.Groups,ncol=sim,byrow=TRUE) Euclid.Dist<-matrix(nrow=Max.Groups,ncol=sim,byrow=TRUE) mean.radius<-vector(mode="numeric",length=Max.Groups) # Generate the Simulation Data ### # Number of Groups for(Groups in (1:Max.Groups)) { # Number of Shots per Group for(Shots in (1:sim)) { # Populate the Simulation Data x.Data[Groups,Shots]<-rnorm(n=1,mean=0,sd=1) y.Data[Groups,Shots]<-rnorm(n=1,mean=0,sd=1) } } # Calculate the Component Distances from the Shots to the Group Mean # Calculate the Euclidean Distance from the Shots to the Group Mean # Calculate the Group Mean Radius ### # Number of Groups for(Groups in (1:Max.Groups)){ # Number of Shots per Group for(Shots in (1:sim)){ shot2mean.x[Groups,Shots]<-(x.Data[Groups,Shots]- mean(x.Data[Groups,]))^2 shot2mean.y[Groups,Shots]<-(y.Data[Groups,Shots]- mean(y.Data[Groups,]))^2 Euclid.Dist[Groups,Shots]<-sqrt(shot2mean.x[Groups,Shots] +shot2mean.y[Groups,Shots]) } mean.radius[Groups]<-mean(Euclid.Dist[Groups,]) } f<-hist(mean.radius,breaks=hist.brks,plot=FALSE) density.data<-cbind(sim,f$mids,f$density) colnames(density.data)<-c("Simulation","MidPoint","Density") if (sim==2) {Simulation.Data<-density.data} else {Simulation.Data<- rbind(Simulation.Data,density.data)} rm(x.Data) rm(y.Data) rm(shot2mean.x) rm(shot2mean.y) rm(Euclid.Dist) rm(mean.radius) } write.table( Simulation.Data, file=paste(dir,"/DATA/Simulation_Data.csv",sep=''), sep=",", quote=FALSE, row.names=FALSE, qmethod="double") Steven Matthew Anderson Anderson Research, LLC Statistical Programming and Analysis SAS (R) Certified Professional adastr...@mac.com Ad Astra per Aspera שָׁלוֹם __ 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] p-values from VGAM function vglm
Anyone know how to get p-values for the t-values from the coefficients produced in vglm? Attached is the code and output — see comment added to output to show where I need p-values + print(paste("** Using VGAM function gamma2 **")) + modl2<- vglm(MidPoint~Count,gamma2,data=modl.subset,trace=TRUE,crit="c") + print(coef(modl2,matrix=TRUE)) + print(summary(modl2)) [1] "** Using VGAM function gamma2 **" VGLMlinear loop 1 : coefficients = 0.408464609241, 3.255887520104, -0.000220585671 VGLMlinear loop 2 : coefficients = 2.34723239e-01, 1.28969691e+00, -4.52393778e-05 VGLMlinear loop 3 : coefficients = 2.19500481e-01, 1.92534895e+00, -3.02160949e-05 VGLMlinear loop 4 : coefficients = 2.19383151e-01, 2.26845910e+00, -3.00838664e-05 VGLMlinear loop 5 : coefficients = 2.19383045e-01, 2.34645688e+00, -3.00836087e-05 VGLMlinear loop 6 : coefficients = 2.19383045e-01, 2.34977070e+00, -3.00836082e-05 VGLMlinear loop 7 : coefficients = 2.19383045e-01, 2.34977637e+00, -3.00836082e-05 VGLMlinear loop 8 : coefficients = 2.19383045e-01, 2.34977637e+00, -3.00836082e-05 log(mu) log(shape) (Intercept) 2.193830e-01 2.349776 Count -3.008361e-05 0.00 Call: vglm(formula = MidPoint ~ Count, family = gamma2, data = modl.subset, trace = TRUE, crit = "c") Pearson Residuals: Min 1Q Median 3Q Max log(mu)-1.7037 -0.82997 0.072275 0.78520 1.72834 log(shape) -2.5152 -0.32448 0.254698 0.58772 0.70678 # NEED P-VALUES HERE # Coefficients: Value Std. Error t value (Intercept):1 2.1938e-01 5.2679e-02 4.16455 (Intercept):2 2.3498e+00 1.7541e-01 13.39574 Count -3.0084e-05 8.9484e-05 -0.33619 Number of linear predictors: 2 Names of linear predictors: log(mu), log(shape) Dispersion Parameter for gamma2 family: 1 Log-likelihood: -26.39268 on 123 degrees of freedom Number of Iterations: 8 Steven Matthew Anderson Anderson Research, LLC Statistical Programming and Analysis SAS (R) Certified Professional adastr...@mac.com Ad Astra per Aspera שָׁלוֹם __ 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.