Problem with cbind in calling mana r

I am trying to make a multi-dimensional ANOVA with the manova function in R. My problem is that I am trying to find a way to pass a list of dependent variables without typing them all manually, as there are many and they have terrible names. My data is in a data frame, where β€œunit” is a dependent variable (coefficient), and the remaining columns are various numerical response variables. eg.

unit C_pct Cln C_N_mol Cnmolln C_P_mol N_P_mol 1 C 48.22 3.88 53.92 3.99 3104.75 68.42 2 C 49.91 3.91 56.32 4.03 3454.53 62.04 3 C 50.75 3.93 56.96 4.04 3922.01 69.16 4 SH 50.72 3.93 46.58 3.84 2590.16 57.12 5 SH 51.06 3.93 43.27 3.77 2326.04 53.97 6 SH 48.62 3.88 40.97 3.71 2357.16 59.67 

If I write a mans call as

 fit <- manova(cbind(C_pct, Cln) ~ unit, data = plots) 

it works fine, but I would like to be able to skip a long list of columns without naming them one by one, something like

 fit <- manova(cbind(colnames(plots[5:32])) ~ unit, data = plots) 

or

 fit <- manove(cbind(plots[,5:32]) ~ unit, data = plots) 

I get an error

 "Error in model.frame.default(formula = as.matrix(cbind(colnames(plots[5:32]))) ~ : variable lengths differ (found for 'unit') 

I am sure because I am using cbind incorrectly but cannot understand. Any help is appreciated! Sorry if formatting is rude, this is my first question.

EDIT: Both methods (all 3, actually) work. thanks everyone!

+6
source share
2 answers

manova , like most R modeling functions, builds its formula from the variable names found in the dataset. However, when you pass it colnames , you technically pass strings that represent the names of these variables. Therefore, the function does not know what to do with them, and suffocates.

You can get around this. LHS formulas only need to be resolved to the matrix; using cbind(C_pct, Cln, ...) is a way to get the matrix by evaluating the names of its arguments C_pct , Cln , etc. in the environment of your data frame. But if you provide a matrix to begin with, then an assessment is not required.

 fit <- manova(as.matrix(plots[, 5:32]) ~ unit, data=plots) 

Some notes. as.matrix necessary because getting columns from a data frame like this returns a data frame. manova will not like it, so we force the data frame to the matrix. Secondly, this works by assuming that you do not have the actual plots variable inside the plots data frame. This is because if R does not find the name inside your data frame, then it looks into the calling environment, in this case the global environment.

You can also create a matrix before setting the model with

 plots$response <- as.matrix(plots[, 5:32]) fit <- manova(response ~ unit, data=plots) 
+7
source

You can build your formula as a string and apply it to the formula:

 responses <- paste( colnames( plots )[2:6], collapse=",") myformula <- as.formula( paste0( "cbind(", responses , ")~ unit" ) ) manova( myformula, data = plots ) Call: manova(myformula, data = plots) Terms: unit Residuals resp 1 0.4 6.8 resp 2 0 0 resp 3 220.6 21.0 resp 4 0.1 0.0 resp 5 1715135.8 377938.1 Deg. of Freedom 1 4 Residual standard error: 1.3051760.027080132.293640.04966555307.3834 Estimated effects may be unbalanced 
+2
source

Source: https://habr.com/ru/post/947580/


All Articles