Re: [R] help with coef
Using the builtin BOD data frame: as.vector(coef(lm(demand ~ Time, BOD)))[2] On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Hi, I am trying to get R to return just the slope of a linear regression line, but it seems that R has to return both the slope and the name of the slope. For example, a=coef(lm(y~miles)) a (Intercept) miles 360.3778 -7.2875 names(a) [1] (Intercept) miles a[1] (Intercept) 360.3778 a[2] miles -7.2875 I don't understand the data structure that's returned from coef(). names(a) seems to suggest that coef() returns two columns of data, column one is the Intercept, and column two miles. But R keeps telling me that the return value from coef() has only one dimension, i.e., a[,2] doesn't work, but a[2] works. However, a[2] contains more than the slope, it also has the name of the slope. Does anyone know how to access just the slope without its name? Thanks, Tom [[alternative HTML version deleted]] __ 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.
Re: [R] help with coef
Gabor, Thanks for the code example, but it seems that BOD is not needed. I still don't understand what is going on with the data structure returned by coef(). The strangness is illustrated by the following example: a=coef(lm(y~miles)) is.vector(a) [1] TRUE a[2] miles -7.2875 a=as.vector(a) is.vector(a) [1] TRUE a[2] [1] -7.2875 As you can see, although coef() returns a vector already, only after as.vector(a) is used, did a[2] include the slope without the name of the slope. Why is that, and what happened to the name of the slope (names(a) returns NULL)? Tom On 10/20/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Using the builtin BOD data frame: as.vector(coef(lm(demand ~ Time, BOD)))[2] On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Hi, I am trying to get R to return just the slope of a linear regression line, but it seems that R has to return both the slope and the name of the slope. For example, a=coef(lm(y~miles)) a (Intercept) miles 360.3778 -7.2875 names(a) [1] (Intercept) miles a[1] (Intercept) 360.3778 a[2] miles -7.2875 I don't understand the data structure that's returned from coef(). names(a) seems to suggest that coef() returns two columns of data, column one is the Intercept, and column two miles. But R keeps telling me that the return value from coef() has only one dimension, i.e., a[,2] doesn't work, but a[2] works. However, a[2] contains more than the slope, it also has the name of the slope. Does anyone know how to access just the slope without its name? Thanks, Tom [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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] help with coef
On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Gabor, Thanks for the code example, but it seems that BOD is not needed. I still demand and Time are columns of BOD so if you omit it then it won't know where they are. don't understand what is going on with the data structure returned by coef(). The strangness is illustrated by the following example: coef returns a named vector. as.vector returns an unnamed vector. a=coef(lm(y~miles)) is.vector(a) [1] TRUE a[2] miles -7.2875 a=as.vector(a) is.vector(a) [1] TRUE a[2] [1] -7.2875 As you can see, although coef() returns a vector already, only after as.vector(a) is used, did a[2] include the slope without the name of the slope. Why is that, and what happened to the name of the slope (names(a) returns NULL)? Tom On 10/20/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Using the builtin BOD data frame: as.vector(coef(lm(demand ~ Time, BOD)))[2] On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Hi, I am trying to get R to return just the slope of a linear regression line, but it seems that R has to return both the slope and the name of the slope. For example, a=coef(lm(y~miles)) a (Intercept) miles 360.3778 -7.2875 names(a) [1] (Intercept) miles a[1] (Intercept) 360.3778 a[2] miles -7.2875 I don't understand the data structure that's returned from coef(). names(a) seems to suggest that coef() returns two columns of data, column one is the Intercept, and column two miles. But R keeps telling me that the return value from coef() has only one dimension, i.e., a[,2] doesn't work, but a[2] works. However, a[2] contains more than the slope, it also has the name of the slope. Does anyone know how to access just the slope without its name? Thanks, Tom [[alternative HTML version deleted]] __ 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.
Re: [R] help with coef
Tom, coef returns a named vector, which is a vector with an extra attribute called names. To remove the extra attribute you can: names(a) - NULL# through the accessor function [EMAIL PROTECTED] - NULL # directly accessing the attribute names or by creating a new vector as you did without setting its names attribute: as.vector(a) -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of tom soyer Sent: Saturday, October 21, 2006 1:00 AM To: Gabor Grothendieck Cc: r-help Subject: Re: [R] help with coef Gabor, Thanks for the code example, but it seems that BOD is not needed. I still don't understand what is going on with the data structure returned by coef(). The strangness is illustrated by the following example: a=coef(lm(y~miles)) is.vector(a) [1] TRUE a[2] miles -7.2875 a=as.vector(a) is.vector(a) [1] TRUE a[2] [1] -7.2875 As you can see, although coef() returns a vector already, only after as.vector(a) is used, did a[2] include the slope without the name of the slope. Why is that, and what happened to the name of the slope (names(a) returns NULL)? Tom On 10/20/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Using the builtin BOD data frame: as.vector(coef(lm(demand ~ Time, BOD)))[2] On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Hi, I am trying to get R to return just the slope of a linear regression line, but it seems that R has to return both the slope and the name of the slope. For example, a=coef(lm(y~miles)) a (Intercept) miles 360.3778 -7.2875 names(a) [1] (Intercept) miles a[1] (Intercept) 360.3778 a[2] miles -7.2875 I don't understand the data structure that's returned from coef(). names(a) seems to suggest that coef() returns two columns of data, column one is the Intercept, and column two miles. But R keeps telling me that the return value from coef() has only one dimension, i.e., a[,2] doesn't work, but a[2] works. However, a[2] contains more than the slope, it also has the name of the slope. Does anyone know how to access just the slope without its name? Thanks, Tom [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] help with coef
Also try this to look inside the objects: cc - coef(lm(demand ~ Time, BOD)) dput(cc) structure(c(8.52142857142858, 1.72142857142857), .Names = c((Intercept), Time)) cc2 - as.vector(cc) dput(cc2) c(8.52142857142858, 1.72142857142857) from which we see that the only difference in their internal representation is the addition of the names attribute. On 10/21/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Gabor, Thanks for the code example, but it seems that BOD is not needed. I still demand and Time are columns of BOD so if you omit it then it won't know where they are. don't understand what is going on with the data structure returned by coef(). The strangness is illustrated by the following example: coef returns a named vector. as.vector returns an unnamed vector. a=coef(lm(y~miles)) is.vector(a) [1] TRUE a[2] miles -7.2875 a=as.vector(a) is.vector(a) [1] TRUE a[2] [1] -7.2875 As you can see, although coef() returns a vector already, only after as.vector(a) is used, did a[2] include the slope without the name of the slope. Why is that, and what happened to the name of the slope (names(a) returns NULL)? Tom On 10/20/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Using the builtin BOD data frame: as.vector(coef(lm(demand ~ Time, BOD)))[2] On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Hi, I am trying to get R to return just the slope of a linear regression line, but it seems that R has to return both the slope and the name of the slope. For example, a=coef(lm(y~miles)) a (Intercept) miles 360.3778 -7.2875 names(a) [1] (Intercept) miles a[1] (Intercept) 360.3778 a[2] miles -7.2875 I don't understand the data structure that's returned from coef(). names(a) seems to suggest that coef() returns two columns of data, column one is the Intercept, and column two miles. But R keeps telling me that the return value from coef() has only one dimension, i.e., a[,2] doesn't work, but a[2] works. However, a[2] contains more than the slope, it also has the name of the slope. Does anyone know how to access just the slope without its name? Thanks, Tom [[alternative HTML version deleted]] __ 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.
Re: [R] help with coef
Thanks Christos and Gabor. I didn't know there is such a thing called named vector in R. Very cool. Tom On 10/21/06, Christos Hatzis [EMAIL PROTECTED] wrote: Tom, coef returns a named vector, which is a vector with an extra attribute called names. To remove the extra attribute you can: names(a) - NULL# through the accessor function [EMAIL PROTECTED] - NULL # directly accessing the attribute names or by creating a new vector as you did without setting its names attribute: as.vector(a) -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of tom soyer Sent: Saturday, October 21, 2006 1:00 AM To: Gabor Grothendieck Cc: r-help Subject: Re: [R] help with coef Gabor, Thanks for the code example, but it seems that BOD is not needed. I still don't understand what is going on with the data structure returned by coef(). The strangness is illustrated by the following example: a=coef(lm(y~miles)) is.vector(a) [1] TRUE a[2] miles -7.2875 a=as.vector(a) is.vector(a) [1] TRUE a[2] [1] -7.2875 As you can see, although coef() returns a vector already, only after as.vector(a) is used, did a[2] include the slope without the name of the slope. Why is that, and what happened to the name of the slope (names(a) returns NULL)? Tom On 10/20/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Using the builtin BOD data frame: as.vector(coef(lm(demand ~ Time, BOD)))[2] On 10/21/06, tom soyer [EMAIL PROTECTED] wrote: Hi, I am trying to get R to return just the slope of a linear regression line, but it seems that R has to return both the slope and the name of the slope. For example, a=coef(lm(y~miles)) a (Intercept) miles 360.3778 -7.2875 names(a) [1] (Intercept) miles a[1] (Intercept) 360.3778 a[2] miles -7.2875 I don't understand the data structure that's returned from coef(). names(a) seems to suggest that coef() returns two columns of data, column one is the Intercept, and column two miles. But R keeps telling me that the return value from coef() has only one dimension, i.e., a[,2] doesn't work, but a[2] works. However, a[2] contains more than the slope, it also has the name of the slope. Does anyone know how to access just the slope without its name? Thanks, Tom [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.