HW 7.7

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

HW 7.7

Scott
How do we use the predict command?
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Darth Knight
This post was updated on .
The link actually explains a lot: HERE

The first argument is just the "lm" object gathered by
the response and explanatory variable.

The second argument bothers us a lot because we need to make it as a "dataframe".
So, in our case, "data.frame(RPPG=___)".

Don't forget to use the right level of prediction.
This can be chosen by adding an argument "level=___".
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Scott
The problem though is that when I tried to put that into R it outputted 30 lines of data, and I am not sure which to use if any
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Darth Knight
Well. That is interesting.
Please put the codes and result here.
Let me briefly take a look.
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Scott
Not sure if there is a more elegant way to do this, but:

download.file("http://people.stat.sc.edu/taeho/teaching/lab/NBA.RData",
+ destfile = "NBA.RData");load("NBA.RData");attach(NBA)
trying URL 'http://people.stat.sc.edu/taeho/teaching/lab/NBA.RData'
Content type 'application/octet-stream' length 1145 bytes
downloaded 1145 bytes

> RPPG = PPG/OPPG
> fit = lm(RPPG~Win_P)
> summary(fit)

Call:
lm(formula = RPPG ~ Win_P)

Residuals:
      Min        1Q    Median        3Q       Max
-0.021211 -0.009092  0.001262  0.006863  0.025325

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.866656   0.007976  108.66   <2e-16 ***
Win_P       0.268285   0.015308   17.53   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.01229 on 28 degrees of freedom
Multiple R-squared:  0.9165, Adjusted R-squared:  0.9135
F-statistic: 307.2 on 1 and 28 DF,  p-value: < 2.2e-16

> newdata = data.frame(RPPG = .98)
> predict(fit, newdata, interval = "predict", level = .90)
         fit       lwr       upr
1  0.9452634 0.9233385 0.9671883
2  1.0466751 1.0249616 1.0683885
3  0.9581411 0.9364896 0.9797926
4  0.9844330 0.9631218 1.0057442
5  0.9549217 0.9332082 0.9766351
6  1.0303097 1.0088656 1.0517538
7  0.9452634 0.9233385 0.9671883
8  1.0171637 0.9958525 1.0384749
9  0.9943595 0.9730984 1.0156206
10 1.0563333 1.0344084 1.0782582
11 1.0794058 1.0568257 1.1019859
12 1.0236026 1.0022357 1.0449695
13 1.0040178 0.9827636 1.0252720
14 0.9812136 0.9598768 1.0025503
15 0.9385563 0.9164623 0.9606503
16 1.0107249 0.9894512 1.0319987
17 1.0107249 0.9894512 1.0319987
18 1.0203832 0.9990464 1.0417199
19 1.0236026 1.0022357 1.0449695
20 0.9616288 0.9400395 0.9832181
21 1.0236026 1.0022357 1.0449695
22 0.9484828 0.9266327 0.9703330
23 1.0367485 1.0152120 1.0582850
24 0.9353369 0.9131554 0.9575183
25 1.0270903 1.0056857 1.0484949
26 0.9549217 0.9332082 0.9766351
27 1.0203832 0.9990464 1.0417199
28 1.0598210 1.0378105 1.0818316
29 1.0236026 1.0022357 1.0449695
30 1.0072372 0.9859761 1.0284983
Warning message:
'newdata' had 1 row but variables found have 30 rows
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Darth Knight
I see, Scott.
We only need to fix one line.
When you fit the model in the code,
the response and explanatory variables were flipped.
Instead, it should be "lm(Win_P~RPPG)".
I guess this should fix the problem.
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Scott
Naturally its something that simple. Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: HW 7.7

Darth Knight
Indeed, R does not provide a good debugging procedure.