Victor Bennett
2007-May-05 05:24 UTC
[R] dynamically specifying regressors/RHS variables in a regression
Does anyone know if there is a way to specify regressors dynamically rather than explicitly? More specifically, I have a data set in "long format" that details a number of individuals and their responses to a question (which can be positive, negative, or no answer). Each individual answers as many questions as they want, so there are a different number of rows per individual. For each number of questions, I want to run a logit on all the individuals who saw that many questions, predicting whether they choose to answer anymore afterwards by their choices on the earlier questions. the second logit would look like answered_only_2~answer_1_pos+answer_1_neg+answer_2_pos+answer_2_neg This will result in over 100 regressions, with different numbers of RHS variables. I'd like to iterate over the sequence of numbers and run a logit for each, but I can't find any means for dynamically generating the RHS variable from an array, or vector. The only way I can think of to write the function would still require the same number of RHS variables in each regression. Is there a way to dynamically generate the right hand side?
Gabor Grothendieck
2007-May-05 09:47 UTC
[R] dynamically specifying regressors/RHS variables in a regression
Using the builtin data frame CO2 this regresses uptake, which is variable number 5, against each consecutive pair of variables: for(i in 1:3) { idx <- c(i, i+1, 5) print(lm(uptake ~., CO2[idx])) } On 5/5/07, Victor Bennett <bennett at haas.berkeley.edu> wrote:> Does anyone know if there is a way to specify regressors dynamically > rather than explicitly? > > More specifically, I have a data set in "long format" that details a > number of individuals and their responses to a question (which can be > positive, negative, or no answer). Each individual answers as many > questions as they want, so there are a different number of rows per > individual. > > For each number of questions, I want to run a logit on all the > individuals who saw that many questions, predicting whether they > choose to answer anymore afterwards by their choices on the earlier > questions. > > the second logit would look like > answered_only_2~answer_1_pos+answer_1_neg+answer_2_pos+answer_2_neg > > This will result in over 100 regressions, with different numbers of > RHS variables. I'd like to iterate over the sequence of numbers and > run a logit for each, but I can't find any means for dynamically > generating the RHS variable from an array, or vector. The only way I > can think of to write the function would still require the same number > of RHS variables in each regression. > > Is there a way to dynamically generate the right hand side? > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Apparently Analagous Threads
- Model matrix using dummy regressors or deviation regressors
- [Solved][Code Snippets] Dropping Empty Regressors
- Time Series with External Regressors in R Problems with XReg
- beginner's question: group of regressors by name vector?
- random effects mixed model, different regressors