Dear All I have three data sets, X1, X2 and Y. X1 is data, X2 and Y were generated in (different) R programs. All three vectors have one column of 60 data points. I am using the code lm(Y~X1)$coef and lm(Y~X2)$coef. The first returns two values, an intercept and a slope, but the second returns 60 values. I suspect there is something in the "type" of X2 such that it forces the regression to do something different, but I can't work this out. Please help! Lewis ********************************************************************** Hermes Fund Managers Limited Registered in England No. 1661776, Lloyds Chambers, 1 Portsoken Street, London E1 8HZ *** Please read the Hermes email disclaimer at http://www.hermes.co.uk/email_terms.htm before acting on this email or opening any attachment *** The contents of this email are confidential. If you have received this message in error, please delete it immediately and contact the sender directly or the Hermes IT Helpdesk on +44(0)20 7680 2117. Any reliance on, use, disclosure, dissemination, distribution or copying of this email is unauthorised and strictly prohibited. This message has been checked for viruses but the recipient is strongly advised to rescan the message before opening any attachments or attached executable files. Hermes do not accept any liability for any damage sustained as a result of a virus introduced by this email or any attachment. ********************************************************************** ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email
Hi r-help-bounces at r-project.org napsal dne 26.09.2008 14:17:59:> Dear All > I have three data sets, X1, X2 and Y. X1 is data, X2 and Y were > generated in (different) R programs. All three vectors have one column > of 60 data points. > I am using the code lm(Y~X1)$coef and lm(Y~X2)$coef. The first returns > two values, an intercept and a slope, but the second returns 60 values. > I suspect there is something in the "type" of X2 such that it forces the > regression to do something different, but I can't work this out.try str(X2) it is probably character vector or factor, so you need to transfer it to numeric. see ?as.numeric and beware of factor properties (if X2 is factor) Regards Petr> Please help! > Lewis > > > ********************************************************************** > Hermes Fund Managers Limited > Registered in England No. 1661776, Lloyds Chambers, 1 Portsoken Street,London E1 8HZ> > *** Please read the Hermes email disclaimer athttp://www.hermes.co.uk/email_terms.htm> before acting on this email or opening any attachment *** > > The contents of this email are confidential. If you have received this > message in error, please delete it immediately and contact the senderdirectly> or the Hermes IT Helpdesk on +44(0)20 7680 2117. Any reliance on, use, > disclosure, dissemination, distribution or copying of this email is > unauthorised and strictly prohibited. > > This message has been checked for viruses but the recipient is strongly > advised to rescan the message before opening any attachments or attached> executable files. Hermes do not accept any liability for any damagesustained> as a result of a virus introduced by this email or any attachment. > > > ********************************************************************** > > > ______________________________________________________________________ > This email has been scanned by the MessageLabs Email Security System. > For more information please visit http://www.messagelabs.com/email > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
check is(X1) is(X2) to investigate whether the types of the variables are equal. Probably (most probably), X2 is a factor (treated as dummy variables) so that your 60 values from the second regression are the intercept (i.e. the coefficient for the first observation) plus 59 dummies for the offset difference between each of the 59 observations and the first observation. run: summary(lm(Y~X1)) summary(lm(Y~X2)) and you will see from the regression output that the second regression is estimated with dummies for X2 rather than treating X2 as a numeric variable. Transform X2 to numeric by: X3=as.numeric(X2) and check whether the values are otherwise equal to X2. cbind(X2,X3) If that's the case, rerun your analysis summary(lm(Y~X3)) and you will only get one intercept and a slope coefficient. Cheers, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von GRANT Lewis Gesendet: Friday, September 26, 2008 8:18 AM An: r-help at r-project.org Betreff: [R] Regression and data types Dear All I have three data sets, X1, X2 and Y. X1 is data, X2 and Y were generated in (different) R programs. All three vectors have one column of 60 data points. I am using the code lm(Y~X1)$coef and lm(Y~X2)$coef. The first returns two values, an intercept and a slope, but the second returns 60 values. I suspect there is something in the "type" of X2 such that it forces the regression to do something different, but I can't work this out. Please help! Lewis ********************************************************************** Hermes Fund Managers Limited Registered in England No. 1661776, Lloyds Chambers, 1 Portsoken Street, London E1 8HZ *** Please read the Hermes email disclaimer at http://www.hermes.co.uk/email_terms.htm before acting on this email or opening any attachment *** The contents of this email are confidential. If you hav...{{dropped:28}}
On Fri, 2008-09-26 at 13:17 +0100, GRANT Lewis wrote:> Dear All > I have three data sets, X1, X2 and Y. X1 is data, X2 and Y were > generated in (different) R programs. All three vectors have one column > of 60 data points. > I am using the code lm(Y~X1)$coef and lm(Y~X2)$coef.Others have replied with an answer to your question. I just wanted to suggest you don't rummage around in R model objects taking what you like using '$'. 9 times out of 10 you'll get what you want, but that last remaining time will have you rubbing your head in confusion at best, at worst, answers may be just plain wrong. Use extractor functions, such as coef() instead: coef(lm(Y ~ X1)) G> The first returns > two values, an intercept and a slope, but the second returns 60 values. > I suspect there is something in the "type" of X2 such that it forces the > regression to do something different, but I can't work this out. > Please help! > Lewis > > > ********************************************************************** > Hermes Fund Managers Limited > Registered in England No. 1661776, Lloyds Chambers, 1 Portsoken Street, London E1 8HZ > > *** Please read the Hermes email disclaimer at http://www.hermes.co.uk/email_terms.htm before acting on this email or opening any attachment *** > > The contents of this email are confidential. If you have received this message in error, please delete it immediately and contact the sender directly or the Hermes IT Helpdesk on +44(0)20 7680 2117. Any reliance on, use, disclosure, dissemination, distribution or copying of this email is unauthorised and strictly prohibited. > > This message has been checked for viruses but the recipient is strongly advised to rescan the message before opening any attachments or attached executable files. Hermes do not accept any liability for any damage sustained as a result of a virus introduced by this email or any attachment. > > > ********************************************************************** > > > ______________________________________________________________________ > This email has been scanned by the MessageLabs Email Security System. > For more information please visit http://www.messagelabs.com/email > > ______________________________________________ > 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.