[R] Variable name as string

Jan private jrheinlaender at gmx.de
Sun Oct 17 05:41:12 CEST 2010


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



More information about the R-help mailing list