[R] Variable name as string

Greg Snow Greg.Snow at imail.org
Sun Oct 17 06:31:04 CEST 2010


You can get the name of var by doing 

coeffname <- deparse(substitute(var))

Is that what you wanted?

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jan private
> Sent: Saturday, October 16, 2010 9:41 PM
> To: r-help
> Subject: [R] Variable name as string
> 
> Hello,
> 
> from Verzani, simpleR (pdf), p. 80, I created the following function to
> test the coefficient of lm() against an arbitrary value.
> 
> coeff.test <- function(lm.result, var, coeffname, value) {
>   # null hypothesis: coeff = value
>   # alternative hypothesis: coeff != value
>   es <- resid(lm.result)
>   coeff <- (coefficients(lm.result))[[coeffname]]
>   # degrees of freedom = length(var) - number of coefficients?
>   n <- df.residual(lm.result)
>   s <- sqrt( sum( es^2 ) / n )
>   SE <- s/sqrt(sum((var - mean(var))^2))
>   t <- (coeff - value )/SE
>   2 * pt(t,n,lower.tail=FALSE) # times two because problem is two-sided
> }
> 
> E.g. it needs to be called coeff.test(lm(N ~ D), D, "D", 70) if I want
> to test the probability slope of the linear model being 70.
> 
> Is there a more elegant way to avoid passing D and "D" both as
> parameters?
> 
> Also, as a non-professional, I would like to know whether the function
> is valid for all coefficients of lm(), e.g. coeff.test(lm(N ~ D + H),
> H,
> "H", 70). I am aware that Verzani gives a different formula for testing
> the intercept.
> 
> Thanks!
> 	Jan Rheinländer
> 
> ______________________________________________
> 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.


More information about the R-help mailing list