[R] Linear Regression with slope equals 0

2007-08-14 Thread E . N . D . Grew

 Hi there, am trying to run a linear regression with a slope of 0.

 I have a dataset as follows

 t d
 1 303
 2 302
 3 304
 4 306
 5 307
 6 303

 I would like to test the significance that these points would lie on a
horizontal straight line.

 The standard regression lm(d~t) doesn't seem to allow the slope to be set.

 Any help very welcome.

 ed

__
R-help@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.


Re: [R] Linear Regression with slope equals 0

2007-08-14 Thread Prof Brian Ripley
On Tue, 14 Aug 2007, [EMAIL PROTECTED] wrote:


 Hi there, am trying to run a linear regression with a slope of 0.

 I have a dataset as follows

 t d
 1 303
 2 302
 3 304
 4 306
 5 307
 6 303

 I would like to test the significance that these points would lie on a
 horizontal straight line.

 The standard regression lm(d~t) doesn't seem to allow the slope to be set.

lm(d ~ 1) does, though, to zero.

More generally you can use offset(), e.g. lm(d ~ offset(7*t)) forces a 
slope of 7.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@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.


Re: [R] Linear Regression with slope equals 0

2007-08-14 Thread Ted Harding
On 14-Aug-07 11:36:37, [EMAIL PROTECTED] wrote:
 
  Hi there, am trying to run a linear regression with a slope of 0.
 
  I have a dataset as follows
 
  t d
  1 303
  2 302
  3 304
  4 306
  5 307
  6 303
 
  I would like to test the significance that these points would lie on a
 horizontal straight line.
 
  The standard regression lm(d~t) doesn't seem to allow the slope to be
 set.

The model d~1 will fit a constant (the mean), i.e. a regressio with
slope = 0. The model d~t will fit the usual linear regression.
The two con be compared with anova(), as well as getting the details
of the individual fits with summary().

E.g. (with your example):

  d-c(303,302,304,306,207,303)
  t-c(1,2,3,4,5,6)
  lm0-lm(u~1);lm1-lm(u~t);anova(lm0,lm1)

##Analysis of Variance Table
##Model 1: u ~ 1
##Model 2: u ~ t
##  Res.DfRSS Df Sum of Sq  F Pr(F)
##1  5 7785.5   
##2  4 6641.4  11144.1 0.6891 0.4531

summary(lm0)
## Call: lm(formula = u ~ 1)
## Residuals:
##1 2 3 4 5 6 
## 15.5  14.5  16.5  18.5 -80.5  15.5 
## Coefficients:
##Estimate Std. Error t value Pr(|t|)
## (Intercept)   287.50  16.11   17.85 1.01e-05 ***
##Residual standard error: 39.46 on 5 degrees of freedom

mean(d)
## [1] 287.5

summary(lm1)
## Call: lm(formula = u ~ t)
## Residuals:
##  1   2   3   4   5   6 
## -4.714   2.371  12.457  22.543 -68.371  35.714 
## Coefficients:
## Estimate Std. Error t value Pr(|t|)   
## (Intercept)  315.800 37.934   8.325  0.00114 **
## t -8.086  9.740  -0.830  0.45314   
## Residual standard error: 40.75 on 4 degrees of freedom
## Multiple R-Squared: 0.147,  Adjusted R-squared: -0.0663 
## F-statistic: 0.6891 on 1 and 4 DF,  p-value: 0.4531 

The P-value for the slope in lm1 is the same as the P-value
returned by anova().

If you want to force a particular non-zero slope (e.g. s0)
for comparison with the data, you can use

  lm0 - lm(d - s0*t ~ 1),

compared with

  lm1- lm(d- s0*t ~ t)

for instance.

Hoping this helps,
Ted.


E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 14-Aug-07   Time: 13:16:05
-- XFMail --

__
R-help@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.


Re: [R] Linear Regression with slope equals 0

2007-08-14 Thread ONKELINX, Thierry
Dear Ed,

In my opinion you don't need to set the slope to zero. Just test if the
slope in lm(d ~ t) is significant. If it is significant then you have
evidence that the slope is NOT zero. But when it is not significant (and
in your example it is), you can't say that it is zero. But doing some
power calculations allows you to estimate the smallest detectable slope.
The estimated slope in your example is 0.49. Your design has a power of
13% to detect slopes of this size.  The real slope has to be about 1.75
in order to have 80% power. Let's say that it would matter if the slope
is about 2 or larger and it won't matter is it is below 2. The power to
detect a slope of 2 is 89%, hence you would likely get a significant
slope if it was larger then 2. Since the slope was not significant, it's
safe to say that the slope is not larger then 2.
But if a slope of 0.5 would matter, you couldn't make this kind of
assumptions because the power to detect a slope of 0.5 is only 13%. So
the change of rejecting the null hypothesis (slope = 0) when slope = 0.5
is too small.

HTH,

Thierry

 power.trend - function(repetitions = 5, x = c(0, 1), sd = 1, slope =
1, alpha = 0.05){
+   X - rep(x, repetitions)
+   ncp - slope ^ 2 * sum((X - mean(X))^2) / sd ^ 2
+   return(1 - pf(qf(1 - alpha, 1, length(X) - 2), 1, length(X) - 2, ncp
= ncp))
+ }
 
 df - data.frame(t = 1:6, d = c(303, 302, 304, 306, 307, 303))
 fit - lm(d ~ t, data = df)
 summary(fit)

Call:
lm(formula = d ~ t, data = df)

Residuals:
   123456 
 0.04762 -1.43810  0.07619  1.59048  2.10476 -2.38095 

Coefficients:
Estimate Std. Error t value Pr(|t|)
(Intercept) 302.4667 1.7849  169.45 7.28e-09 ***
t 0.4857 0.45831.060.349
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

Residual standard error: 1.917 on 4 degrees of freedom
Multiple R-Squared: 0.2192, Adjusted R-squared: 0.02402 
F-statistic: 1.123 on 1 and 4 DF,  p-value: 0.349 

 power.trend(repetitions = 1, x = df$t, sd = sd(df$d), slope =
coef(fit)[t])
[1] 0.1292668
 power.trend(repetitions = 1, x = df$t, sd = sd(df$d), slope = 1.75)
[1] 0.8021454




ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
[EMAIL PROTECTED]
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

 

 -Oorspronkelijk bericht-
 Van: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] Namens 
 [EMAIL PROTECTED]
 Verzonden: dinsdag 14 augustus 2007 13:37
 Aan: r-help@stat.math.ethz.ch
 Onderwerp: [R] Linear Regression with slope equals 0
 
 
  Hi there, am trying to run a linear regression with a slope of 0.
 
  I have a dataset as follows
 
  t d
  1 303
  2 302
  3 304
  4 306
  5 307
  6 303
 
  I would like to test the significance that these points 
 would lie on a horizontal straight line.
 
  The standard regression lm(d~t) doesn't seem to allow the 
 slope to be set.
 
  Any help very welcome.
 
  ed
 
 __
 R-help@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.


__
R-help@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.