Dear all, I'd like to get a linear regression of some data, and impose that the line goes through a given point P. I've tried to use the lm() method in the package "stats", but I wasn't able to specify the coordinates of the point P. Maybe I should use another method? I also have another question: How does lm() choose the point through which the output straight line goes in order to compute the values of its slope and intercept? I would be very grateful if anyone could help me. Domenico ********************************************* Domenico Cozzetto Biocomputing group Department of Biochemical Sciences "A. Rossi Fanelli" University of Rome "La Sapienza" P.le Aldo Moro, 5 - 00185 Rome Tel: +39 06 49690276 Fax: +39 06 4400062 URL: http://cassandra.bio.uniroma1.it/~cozzetto/
details about lm()
6 messages · Domenico Cozzetto, vincent@7d4.com, Uwe Ligges
Domenico Cozzetto a ??crit :
Dear all, I'd like to get a linear regression of some data, and impose that the line goes through a given point P. I've tried to use the lm() method in the package "stats", but I wasn't able to specify the coordinates of the point P. Maybe I should use another method?
add directly P in your data is also a way
vincent at 7d4.com wrote:
Domenico Cozzetto a ??crit :
Dear all, I'd like to get a linear regression of some data, and impose that the line goes through a given point P. I've tried to use the lm() method in the package "stats", but I wasn't able to specify the coordinates of the point P. Maybe I should use another method?
add directly P in your data is also a way
No! Please, both of you, consult a basic textbook on linear regression. You can transform the data (linear) so that P becomes (0,0), after that you can estimate the slope without intercept by specifying lm(y ~ x - 1) The slope estimate is still valid while your intercept can be calculated afterwards. Uwe Ligges
______________________________________________ R-help at 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
Uwe Ligges a ??crit :
vincent at 7d4.com wrote:
Domenico Cozzetto a ??crit :
Dear all, I'd like to get a linear regression of some data, and impose that the line goes through a given point P. I've tried to use the lm() method in the package "stats", but I wasn't able to specify the coordinates of the point P. Maybe I should use another method?
add directly P in your data is also a way
No!
Sorry indeed for my not at all rigourous answer. Adding P in the data set will indeed not force the regression line to pass through P (P will only be one more points of the cloud, adding P will "attract" the regression line, not more.) I did make this answer because I'm yet working with very small data sets, and adding P (in more than one exemplar when needed in order to give it more weight), is a fast, (a bit ugly I agree), way to do. But on the kind of data I use, it works good enough. I should have add this precision. Apologies.
Please, both of you, consult a basic textbook on linear regression.
If you have a good reference or link in mind, I would thank you.
You can transform the data (linear) so that P becomes (0,0), after that you can estimate the slope without intercept by specifying lm(y ~ x - 1) The slope estimate is still valid while your intercept can be calculated afterwards.
Sorry for my lack of knowledge, but will the above trick really force the regression line to pass through P ? adding (0,0) in this new system of coordinates isn't it equivalent to add P to the dataset in the original system ? If my question is too basic and/or too stupid, just give it a rest. Vincent
vincent at 7d4.com wrote:
Uwe Ligges a ??crit :
vincent at 7d4.com wrote:
Domenico Cozzetto a ??crit :
Dear all, I'd like to get a linear regression of some data, and impose that the line goes through a given point P. I've tried to use the lm() method in the package "stats", but I wasn't able to specify the coordinates of the point P. Maybe I should use another method?
add directly P in your data is also a way
No!
Sorry indeed for my not at all rigourous answer. Adding P in the data set will indeed not force the regression line to pass through P (P will only be one more points of the cloud, adding P will "attract" the regression line, not more.) I did make this answer because I'm yet working with very small data sets, and adding P (in more than one exemplar when needed in order to give it more weight), is a fast, (a bit ugly I agree), way to do. But on the kind of data I use, it works good enough. I should have add this precision. Apologies.
Please, both of you, consult a basic textbook on linear regression.
If you have a good reference or link in mind, I would thank you.
E.g., among several other, the great comprehensive books by John Fox are really well written and easy to understand ...
You can transform the data (linear) so that P becomes (0,0), after that you can estimate the slope without intercept by specifying lm(y ~ x - 1) The slope estimate is still valid while your intercept can be calculated afterwards.
Sorry for my lack of knowledge, but will the above trick really force the regression line to pass through P ? adding (0,0) in this new system of coordinates isn't it equivalent to add P to the dataset in the original system ?
Well, you do not add that point, but transform the others: Say you have (let's make a very simple 1-D example) points P_i = (x_i, y_i), and P = (x_0, y_0). Then calculate for all i: P'_i = (x_i - x_0, y_i - y_0) Now you can calculate a regression without any Intercept by lm(y ~ x - 1) You got the slope now and the Intercept is 0 so far for P'. After that, you can re-transform to get the real data's intercept: Intercept = -(slope * x_0) + y_0 Uwe Ligges
If my question is too basic and/or too stupid, just give it a rest. Vincent
Uwe Ligges a ??crit :
vincent at 7d4.com wrote:
Sorry for my lack of knowledge, but will the above trick really force the regression line to pass through P ? adding (0,0) in this new system of coordinates isn't it equivalent to add P to the dataset in the original system ?
Well, you do not add that point, but transform the others: Say you have (let's make a very simple 1-D example) points P_i = (x_i, y_i), and P = (x_0, y_0). Then calculate for all i: P'_i = (x_i - x_0, y_i - y_0) Now you can calculate a regression without any Intercept by lm(y ~ x - 1) You got the slope now and the Intercept is 0 so far for P'. After that, you can re-transform to get the real data's intercept: Intercept = -(slope * x_0) + y_0
Thank you very much for the kind answer and for your time. (I'll read that carefully and take my rule, pencil and R). Vincent