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
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.
> coeffname <- deparse(substitute(var)) > > Is that what you wanted? >Yes! That gets rid of the extra parameter. Thanks
On 10/17/2010 05:41 AM, Jan private wrote:> 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.In a word, no, it isn't. Your SE formula only holds when "var" is the only predictor. It would be more general to extract the SE from coefficients(summary(lm.result)) (which also avoids having to pass var as an argument). -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
So here is the next version. Why does the intercept needs lower.tail=TRUE to give the same result as summary() for value=0? # See Verzani, simpleR (pdf), p. 80 coeff.test <- function(lm.result, idx, value) { # idx = 1 is the intercept, idx>1 the other coefficients # null hypothesis: coeff = value # alternative hypothesis: coeff != value coeff <- coefficients(lm.result)[idx] SE <- coefficients(summary(lm.result))[idx,"Std. Error"] n <- df.residual(lm.result) t <- (coeff - value )/SE if (idx == 1) { 2 * pt(t,n,lower.tail=TRUE) # times two because problem is two-sided } else { 2 * pt(t,n,lower.tail=FALSE) } }