Error In Weighted.mean.defaulty Weights
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss
Weighted Mean In R
the workings and policies of this site About Us Learn more about glmnet Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up CCA Analysis: Error in weighted.mean.default(newX[, i], …) : 'x' and 'w' must have the same length up vote 0 down vote favorite 1 I'm very new to R and this might be a very silly question to ask but I'm quite stuck right now. I'm currently trying to do a Canonical Correspondence Analysis on my data to see which environmental factors have more weight on community distribution. I'm using the vegan package. My data consists of a table for the environmental factors (dataset EFamoA) and another for an abundance matrix (dataset AmoA). I have 41 soils, with 39 environmental factors and 334 species. After cleaning my data of any variables which are not numerical, I try to perform the cca analysis using the formula notation: CCA.amoA <- cca (AmoA ~ EFamoA$PH + EFamoA$LOI, data = EFamoA, scale = TRUE, na.action = na.omit) But then I get this error: Error in weighted.mean.default(newX[, i], ...) : 'x' and 'w' must have the same length I don't really know where to go from here and haven't found much regarding this problem anywhere (which leads me to think that it must be some sort of very basic mistake I'm doing). My environmental factor data is not standardized as I red in the cca help file that the algorithm does it but maybe I should standardize it bef
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes http://stackoverflow.com/questions/24514861/cca-analysis-error-in-weighted-mean-defaultnewx-i-x-and-w-must a minute: Sign up Weighted mean by row up vote 5 down vote favorite I have the following data: a=c(1:10) b=c(16:25) c=c(24:33) wa=c(3,7,3,3,3,3,3,3,3,1) wb=c(3,2,3,3,3,3,3,3,3,8) wc=c(4,1,4,4,4,4,4,4,4,1) z=data.frame(a,b,c,wa,wb,wc) I want to get the weighted mean for each record. So I tried this: weight=apply(subset(z,select=c(wa,wb,wc)),1,function(x) x) z$weightMean=apply(subset(z,select=c(a,b,c)),1,function(x) weighted.mean(x,weight)) Which returned the following error message: Error in weighted.mean.default(x, weight) : 'x' and 'w' must have the same http://stackoverflow.com/questions/9864631/weighted-mean-by-row length So then I tried this: weight=as.vector(weight) z$weightMean=apply(subset(z,select=c(a,b,c)),1,function(x) weighted.mean(x,weight)) Which also returned the same error. What am I doing wrong? r share|improve this question asked Mar 25 '12 at 22:23 thequerist 5822721 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted This seems to do the trick: > apply(z, 1, function(x) weighted.mean(x[1:3], x[4:6])) [1] 14.7 7.3 16.7 17.7 18.7 19.7 20.7 21.7 22.7 24.3 This will probably be a bit faster, though less clear as to what's going on: > rowSums(z[,1:3] * z[,4:6]) / rowSums(z[,4:6]) [1] 14.7 7.3 16.7 17.7 18.7 19.7 20.7 21.7 22.7 24.3 share|improve this answer answered Mar 25 '12 at 22:33 Chase 37.3k586131 Only the first method can deal with NA values, using weighted.mean(..., na.rm=T). The second method returns NA as soon as one of the columns contains NA. –Martin Jan 30 at 21:37 @Martin - rowSums() supports the na.rm argument, so it should work for that one as well. –Chase Feb 10 at 3:14 add a comment| Your Answer draft saved draft discarded Sign up or
weighted mean is to be computed. w a numerical vector of https://stat.ethz.ch/R-manual/R-patched/library/stats/html/weighted.mean.html weights the same length as x giving the http://stats.stackexchange.com/questions/21315/error-when-running-glmnet-in-multinomial weights to use for elements of x. ... arguments to be passed to or from methods. na.rm a logical value indicating whether NA values in x should be stripped before the computation proceeds. Details error in This is a generic function and methods can be defined for the first argument x: apart from the default methods there are methods for the date-time classes "POSIXct", "POSIXlt", "difftime" and "Date". The default method will work for any numeric-like object for which error in weighted.mean.defaulty [, multiplication, division and sum have suitable methods, including complex vectors. If w is missing then all elements of x are given the same weight, otherwise the weights coerced to numeric by as.numeric and normalized to sum to one (if possible: if their sum is zero or infinite the value is likely to be NaN). Missing values in w are not handled specially and so give a missing value as the result. However, zero weights are handled specially and the corresponding x values are omitted from the sum. Value For the default method, a length-one numeric vector. See Also mean Examples ## GPA from Siegel 1994 wt <- c(5, 5, 4, 1)/15 x <- c(3.7,3.3,3.5,2.8) xm <- weighted.mean(x, wt) [Package stats version 3.2.2 Index]
Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Cross Validated Questions Tags Users Badges Unanswered Ask Question _ Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Error when running glmnet in multinomial up vote 7 down vote favorite 1 The problem mentioned in this question is fixed in version 1.7.3 of the R package glmnet. I'm having some problems running glmnet with family=multinomial, and was wondering has encountered something similar or might be able to tell me what I'm doing wrong. When I put my own dummy data in, the error "Error in apply(nz, 1, median) : dim(X) must have a positive length" gets reported when I run cv.glmnet, which apart from saying "it didn't work" wasn't hugely informative to me. y=rep(1:3,20) #=> 60 element vector set.seed(1011) x=matrix(y+rnorm(20*3*10,sd=0.4),nrow=60) # 60*10 element matrix glm = glmnet(x,y,family="multinomial") #=> returns without error crossval = cv.glmnet(x,y,family="multinomial") #=> Error in apply(nz, 1, median) : dim(X) must have a positive length crossval = cv.glmnet(x,y,family="multinomial",type.measure="class") #=> Error in apply(nz, 1, median) : dim(X) must have a positive length crossval = cv.glmnet(x,y,family="multinomial",type.measure="mae") #=> Error in apply(nz, 1, median) : dim(X) must have a positive length cvglm = cv.gl