Re: [R] linear model with interaction / segments

2009-08-29 Thread David Winsemius


On Aug 29, 2009, at 7:56 AM, Markus Gesmann wrote:


Dear R-help,

Suppose I have the following data:

df=data.frame(x=1:10, y=c(1,2,3,4,5,12,14,16,18,20))
plot(y~x, df, t=b)

How can I fit a model which estimates the slopes between x = 1-5,  
5-6, and 6-10?


Adding the factor f:

df$f -  gl(2,5)

Allows me to fit a linear model with interaction

lm(y ~ x:f, data=df)

which gives me the slope of 1 and 2 between 1-5, and 6-10  
respectively, however it can not cope with the change from 5 to 6.


Can you be a bit more mathematically precise, ... and less  
anthropomorphic,  in explaining what you mean by cannot cope? When I  
look at the plot of fitted values I fail to see any coping  
deficiencies, at least with my understanding regarding what you were  
expecting  about which I am very much guessing at this point. Did  
you want only two regression lines that share a joinpoint at x=5.5?




I would appreciate if someone could point me into the right direction.

Many thanks

Markus


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

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


Re: [R] linear model with interaction / segments

2009-08-29 Thread Gavin Simpson
On Sat, 2009-08-29 at 12:56 +0100, Markus Gesmann wrote:
 Dear R-help,
 
 Suppose I have the following data:
 
 df=data.frame(x=1:10, y=c(1,2,3,4,5,12,14,16,18,20))
 plot(y~x, df, t=b)
 
 How can I fit a model which estimates the slopes between x = 1-5, 5-6,  
 and 6-10?

Does the segmented package do what you want?

G

 
 Adding the factor f:
 
 df$f -  gl(2,5)
 
 Allows me to fit a linear model with interaction
 
 lm(y ~ x:f, data=df)
 
 which gives me the slope of 1 and 2 between 1-5, and 6-10  
 respectively, however it can not cope with the change from 5 to 6.
 
 I would appreciate if someone could point me into the right direction.
 
 Many thanks
 
 Markus
 
 __
 R-help@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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London  [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

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