[R] Using predicted() in R

David Winsemius dwinsemius at comcast.net
Mon Oct 24 21:15:36 CEST 2011


On Oct 24, 2011, at 2:45 PM, flokke wrote:

> Dear all,
> I am a R user since about 3 weeks now and still struggeling with  
> things that
> must be very
> easy for you...
> This week I am struggling with the function predict()
>
> I want to use this function to get a 95% interval.
> I understand that you have to use it in such a way as:
>
> lm_examplemodel<- lm(y~x1 + x2 + x3)

Many times the x1,x2,and x3 will be irregularly spaced.

> newdata <- data.frame(x1 = ???, x2 = ???, x3 = ???)

You might want to create predictions on a regular grid for plotting  
functions htat require such. Obviously you would want to keep the  
values within the ranges of original values so you don't extrapolate  
beyond the range of measurements.

> predict(lm_examplemodel, newdata, interval = 'confidence')

And you might be plotting the estimated and 95% ranges.

>
> And here comes my questions. As you maybe already have guessed its  
> about
> the questions marks/
> the values for the new data frame you have to use.
>
> I read in help(predict.lm) that you have to use a new data frame to  
> be able
> to use the predict
> function, but I dont know what values I have to assign to x1 and  
> x2 ... to
> create the new data set.

Perhaps:

x1=seq(range(x1)[1]:range(x1)[2], by=diff(range(x1))/10

... und so weiter.

> In the help function it says that you can use the fitted values, but  
> when I
> use the function:
>
> fitted(lm_examplemodel)
> or fitted.values(lm_examplemodel)
>
> I get many many columns. I think that the function gives a fitted  
> value for
> every Yi and Xi.

That looks wrong. `fitted` should be one per record. You have not  
followed the advice to post real data so it is not possible to  
diagnose your error. Please read the Posting Guide.


> SO how do I get those values? Do i have to use another function than  
> fitted?
> Or not the fitted
> values at all?
>


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list