[R] Using predict()?

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Dec 12 14:14:25 CET 2007



Zembower, Kevin wrote:
> I'm trying to solve a homework problem using R. The problem gives a list
> of cricket chirps per second and corresponding temperature, and asks to
> give the equation for the linear model and then predict the temperature
> to produce 18 chirps per second. So far, I have:
> 
>> # Homework 11.2.1 and 11.3.3
>> chirps <- scan()
> 1: 20
> 2: 16
> 3: 19.8
> 4: 18.4
> 5: 17.1
> 6: 15.5
> 7: 14.7
> 8: 17.1
> 9: 15.4
> 10: 16.2
> 11: 15
> 12: 17.2
> 13: 16
> 14: 17
> 15: 14.4
> 16: 
> Read 15 items
>> temp <- scan()
> 1: 88.6
> 2: 71.6
> 3: 93.3
> 4: 84.3
> 5: 80.6
> 6: 75.2
> 7: 69.7
> 8: 82
> 9: 69.4
> 10: 83.3
> 11: 79.6
> 12: 82.5
> 13: 80.6
> 14: 83.5
> 15: 76.3
> 16: 
> Read 15 items
>> chirps
>  [1] 20.0 16.0 19.8 18.4 17.1 15.5 14.7 17.1 15.4 16.2 15.0 17.2 16.0
> 17.0 14.4
>> temp
>  [1] 88.6 71.6 93.3 84.3 80.6 75.2 69.7 82.0 69.4 83.3 79.6 82.5 80.6
> 83.5 76.3
>> chirps.res <- lm(chirps ~ temp)
>> summary(chirps.res)
> 
> Call:
> lm(formula = chirps ~ temp)
> 
> Residuals:
>      Min       1Q   Median       3Q      Max 
> -1.56146 -0.58088  0.02972  0.58807  1.53047 
> 
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)    
> (Intercept) -0.31433    3.10963  -0.101 0.921028    
> temp         0.21201    0.03873   5.474 0.000107 ***
> ---
> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
> 
> Residual standard error: 0.9715 on 13 degrees of freedom
> Multiple R-Squared: 0.6975,     Adjusted R-squared: 0.6742 
> F-statistic: 29.97 on 1 and 13 DF,  p-value: 0.0001067
>> # From the linear model summary output above, the equation for the
> least squares line is:
>> #    y = -0.3143 + 0.2120*x or chirps = -0.3143 + 0.2120*temp
>>
> 
> I can then determine the answer to the prediction, using algebra and R:
>> pred_temp <- (18+0.3143)/0.2120
>> pred_temp
> [1] 86.3882
> 
> However, I'd like to try to use the predict() function. Since 'chirps'
> and 'temp' are just vectors of numbers, and not dataframes, these
> failed:
> predict(chirps.res, newdata=data.frame(chirp=18))
> predict(chirps.res, newdata="chirp=18")
> predict(chirps.res, newdata=18)
> 


Well, "chirps" (not "chirp", BTW) was your *response* in the lm() call!
Your new data has to be called "temp", otherwise either the regression 
did not make sense or you are confusing different things.

Best,
Uwe Ligges


> I then tried to turn my two vectors into a dataframe. I would have bet
> money that this would have worked, but it didn't:
>> df <- data.frame(chirps, temp)
>>  chirps.res <- lm(chirps ~ temp, data=df)
>> predict(chirps.res, newdata=data.frame(chirps=18))
> 
> Can anyone tell me how to use predict() in this circumstance?
> 
> Thanks for your help and advice.
> 
> -Kevin
> 
> Kevin Zembower
> Internet Services Group manager
> Center for Communication Programs
> Bloomberg School of Public Health
> Johns Hopkins University
> 111 Market Place, Suite 310
> Baltimore, Maryland  21202
> 410-659-6139 
> 
> ______________________________________________
> 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.



More information about the R-help mailing list