Robi Ragan
2006-Sep-14 05:33 UTC
[R] Adding predicted values as a new variable in a data frame
I am running a regression: ols.reg1 <- lm(y ~ x1 + x2 + x3 + x4) on a data.frame and then generating fitted values: y.hat <- ols.reg1$fitted.values Then I would like to add these fitted values to the data.frame as a new variable. The problem is that when the values are predicted the resulting output has too few rows. for some reason certian observations do not get predicted values. So this shrinks the column down and I then cannot combine the output into the original data.frame. If someone could please help I would apreciate it. Stata automatically adds a new column to the data set when you find the fitted values. So having to fight with R just to do something I used to routimely do has made me think of turning back to the dark side. I hope I have just missed something trival in all the help files I have been looking through. Thanks, -- Robi Ragan Graduate Student Department of Economics Department of Political Science The University of Georgia
Gabor Grothendieck
2006-Sep-14 06:04 UTC
[R] Adding predicted values as a new variable in a data frame
Specify na.action = na.exlude, e.g.> x <- y <- 1:10; x[5] <- NA > fitted(lm(y ~ x, na.action = na.exclude))1 2 3 4 5 6 7 8 9 10 1 2 3 4 NA 6 7 8 9 10 On 9/14/06, Robi Ragan <robi.ragan at gmail.com> wrote:> I am running a regression: > > ols.reg1 <- lm(y ~ x1 + x2 + x3 + x4) > > on a data.frame > > and then generating fitted values: > > y.hat <- ols.reg1$fitted.values > > Then I would like to add these fitted values to the data.frame as a > new variable. The problem is that when the values are predicted the > resulting output has too few rows. for some reason certian > observations do not get predicted values. So this shrinks the column > down and I then cannot combine the output into the original > data.frame. > > If someone could please help I would apreciate it. Stata automatically > adds a new column to the data set when you find the fitted values. So > having to fight with R just to do something I used to routimely do has > made me think of turning back to the dark side. I hope I have just > missed something trival in all the help files I have been > looking through. > > Thanks, > > > -- > > Robi Ragan > Graduate Student > Department of Economics > Department of Political Science > The University of Georgia > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
Prof Brian D Ripley
2006-Sep-14 06:42 UTC
[R] Adding predicted values as a new variable in a data frame
?na.exclude should help you: my guess is that you asked (by using the default) na.action = na.omit) for rows with missing values to be excluded from the residuals. But since you have not mentioned missing values, we have to guess what 'for some reason' was: please note the footer of this messag. On Thu, 14 Sep 2006, Robi Ragan wrote:> I am running a regression: > > ols.reg1 <- lm(y ~ x1 + x2 + x3 + x4) > > on a data.frameHmm, no data frame is mentioned: you want a data= argument.> and then generating fitted values: > > y.hat <- ols.reg1$fitted.valuesPlease use the accessor functions and not dive into the internal details, e.g. y.hat <- fitted(ols.reg1) BTW: where did you get the use of ols.reg1$fitted.values from?> Then I would like to add these fitted values to the data.frame as a > new variable. The problem is that when the values are predicted the > resulting output has too few rows. for some reason certian > observations do not get predicted values. So this shrinks the column > down and I then cannot combine the output into the original > data.frame.fit <- lm(formula, data=data_frame, na.action=na.exclude) data_frame$fitted <- fitted(fit)> If someone could please help I would apreciate it. Stata automatically > adds a new column to the data set when you find the fitted values. So > having to fight with R just to do something I used to routimely do has > made me think of turning back to the dark side. I hope I have just > missed something trival in all the help files I have been > looking through.The above looks trivial to me. It was not in R or S when lm was first introduced (1991 White Book), but was added last century (thanks to the ideas and persistent advocacy of Terry Therneau). -- Brian D. Ripley, ripley at stats.ox.ac.uk 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, UK Fax: +44 1865 272595