Hello R users, I am new to R and am wondering if anyone can help me out with the following issue: I wrote a function to build ts models using different inputs, but when R displays the call for a model, I cannot tell which variables it is using because it shows the arguments instead of the real variables passed to the function. (e.g Call: lm(formula = dyn(dep ~ lag(dep, -1) + indep)) ---> not what I want to see lm(formula = dyn(Y ~ lag(Y, -1) + XVARY)) -----> this is what I want to see (see simplified version of the code and output below) Thanks in advance for any help!! options (scipen=999, digits=7) library(Hmisc) library(DAAG) library(car) library(MASS) library(nlme) library(dyn) library(zoo) tdata <- ts(read.table("C:/R/testing/data.csv" ,sep = ",",header=TRUE)) print(tdata) coeff <- function(dep, indep) { mod <- dyn$lm(dep ~ lag(dep, -1)+ indep) summ <- summary(mod) res <- list(COEF=summ) } out <- coeff(tdata[ ,"Y"], tdata[ ,"XVARY"]); out #########output#############> print(tdata)Time Series: Start = 1 End = 20 Frequency = 1 Unit Y XVARY K DWAY 1 1 3 2 4 50 2 2 5 3 9 50 3 3 6 11 22 50 4 4 8 4 7 2 5 5 9 11 11 2 6 6 12 13 13 2 7 7 23 25 12 2 8 8 22 30 31 3 9 9 23 3 3 3 10 10 19 21 21 32 11 11 3 2 4 34 12 12 5 3 9 4 13 13 6 11 22 4 14 14 8 4 7 4 15 15 9 11 11 4 16 16 12 13 13 4 17 17 23 25 12 4 18 18 22 30 31 4 19 19 23 3 3 4 20 20 19 21 21 5> > coeff <- function(dep, indep) {+ + mod <- dyn$lm(dep ~ lag(dep, -1)+ indep) + summ <- summary(mod) + res <- list(COEF=summ) }> > out <- coeff(tdata[ ,"Y"], tdata[ ,"XVARY"]); out$COEF Call: lm(formula = dyn(dep ~ lag(dep, -1) + indep)) Residuals: Min 1Q Median 3Q Max -10.7157 -2.5454 -0.2090 0.8359 7.3292 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 2.6473 2.1952 1.206 0.24538 lag(dep, -1) 0.5506 0.1558 3.535 0.00275 ** indep 0.3033 0.1259 2.408 0.02845 * --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 Residual standard error: 4.643 on 16 degrees of freedom (2 observations deleted due to missingness) Multiple R-squared: 0.6679, Adjusted R-squared: 0.6264 F-statistic: 16.09 on 2 and 16 DF, p-value: 0.0001479 -- View this message in context: http://www.nabble.com/How-to-show-variables-used-in--lm-function-call--tp21814443p21814443.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2009-Feb-03 17:34 UTC
[R] How to show variables used in lm function call?
Pick off the names in the first two lines of the function body and then paste them into a formula, converting to a real formula object and then make your call: mylm <- function(dep, indep, env = parent.frame()) { depnm <- deparse(substitute(dep)) indepnm <- deparse(substitute(indep)) fo <- sprintf("%s ~ lag(%s, -1) + %s", depnm, depnm, indepnm) fo <- as.formula(fo, env = env) do.call(dyn$lm, list(fo)) } library(dyn) x <- zoo(1:10) y <- x*x mylm(y, x) On Tue, Feb 3, 2009 at 12:16 PM, Pele <drdionc at yahoo.com> wrote:> > Hello R users, > > I am new to R and am wondering if anyone can help me out > with the following issue: I wrote a function to build ts models using > different inputs, but when R displays the call for a model, I cannot tell > which variables > it is using because it shows the arguments instead of the real variables > passed to the function. > > (e.g > > Call: > lm(formula = dyn(dep ~ lag(dep, -1) + indep)) ---> not what I want to see > > lm(formula = dyn(Y ~ lag(Y, -1) + XVARY)) -----> this is what I want to > see > > > (see simplified version of the code and output below) > > Thanks in advance for any help!! > > > > options (scipen=999, digits=7) > > library(Hmisc) > library(DAAG) > library(car) > library(MASS) > library(nlme) > library(dyn) > library(zoo) > > tdata <- ts(read.table("C:/R/testing/data.csv" ,sep = ",",header=TRUE)) > print(tdata) > > coeff <- function(dep, indep) { > mod <- dyn$lm(dep ~ lag(dep, -1)+ indep) > summ <- summary(mod) > res <- list(COEF=summ) } > > out <- coeff(tdata[ ,"Y"], tdata[ ,"XVARY"]); out > > #########output############# > >> print(tdata) > Time Series: > Start = 1 > End = 20 > Frequency = 1 > Unit Y XVARY K DWAY > 1 1 3 2 4 50 > 2 2 5 3 9 50 > 3 3 6 11 22 50 > 4 4 8 4 7 2 > 5 5 9 11 11 2 > 6 6 12 13 13 2 > 7 7 23 25 12 2 > 8 8 22 30 31 3 > 9 9 23 3 3 3 > 10 10 19 21 21 32 > 11 11 3 2 4 34 > 12 12 5 3 9 4 > 13 13 6 11 22 4 > 14 14 8 4 7 4 > 15 15 9 11 11 4 > 16 16 12 13 13 4 > 17 17 23 25 12 4 > 18 18 22 30 31 4 > 19 19 23 3 3 4 > 20 20 19 21 21 5 >> >> coeff <- function(dep, indep) { > + > + mod <- dyn$lm(dep ~ lag(dep, -1)+ indep) > + summ <- summary(mod) > + res <- list(COEF=summ) } >> >> out <- coeff(tdata[ ,"Y"], tdata[ ,"XVARY"]); out > $COEF > > Call: > lm(formula = dyn(dep ~ lag(dep, -1) + indep)) > > Residuals: > Min 1Q Median 3Q Max > -10.7157 -2.5454 -0.2090 0.8359 7.3292 > > Coefficients: > Estimate Std. Error t value Pr(>|t|) > (Intercept) 2.6473 2.1952 1.206 0.24538 > lag(dep, -1) 0.5506 0.1558 3.535 0.00275 ** > indep 0.3033 0.1259 2.408 0.02845 * > --- > Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > Residual standard error: 4.643 on 16 degrees of freedom > (2 observations deleted due to missingness) > Multiple R-squared: 0.6679, Adjusted R-squared: 0.6264 > F-statistic: 16.09 on 2 and 16 DF, p-value: 0.0001479 > > -- > View this message in context: http://www.nabble.com/How-to-show-variables-used-in--lm-function-call--tp21814443p21814443.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >