Stats help! , Linear Models

Status
Not open for further replies.

BassBomb

Diamond Member
Nov 25, 2005
8,390
1
81
I am trying to fit two separate linear models to this problem:

Example of the data:
Price Pages Type
34.55 234 p
43.64 356 c
...

The items are either of type "p" or "c". I need to have two linear models, one for p and one for c. Each one has the same type of model.

The linear model I am fitting is:
Yi = beta0 + beta1 * Xi
Price = beta0 + beta1*Pages

So I used lm function as in our previous class examples. I have been trying to use the subset argument in lm() to do each one. I am only able to get one of them to work.. Any idea how to generate two models using subset argument?

Here is the code:
Code:
lm(Price~Pages, , subset = Type)->model
abline(model$coef[1], model$coef[2])
summary(model)
 
Last edited:

BassBomb

Diamond Member
Nov 25, 2005
8,390
1
81
Another question,

The next part of the problem is to estimate price of a 250 page book, using the model coefficients. That part I can do, but it requires a 95% confidence interval for the estimate y hat for which I have no clue how to do!

Yhat = beta0 hat + beta1 hat * 250

How does a linear model that had no noise component, have a confidence interval for Yhat estimate? Beta0 hat and Beta1 hat have their own variances, and covariances (B0, B1), but how does that influence a confidence interval?

If I take Yhat and add +/- 2* var(Yhat) is that my 95% confidence interval?
 
Last edited:

blinky8225

Senior member
Nov 23, 2004
564
0
0
model1 <- lm(Price~Pages, subset = c(Type == "p"))
model2 <- lm(Price~Pages, subset = c(Type == "c"))
abline(coef(model1))
abline(coef(model2))
summary(model)

is what you want.
 

BassBomb

Diamond Member
Nov 25, 2005
8,390
1
81
model1 <- lm(Price~Pages, subset = c(Type == "p"))
model2 <- lm(Price~Pages, subset = c(Type == "c"))
abline(coef(model1))
abline(coef(model2))
summary(model)

is what you want.

model1 <- lm(Price~Pages, subset = c(Type = "p"))
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases
 

blinky8225

Senior member
Nov 23, 2004
564
0
0
model1 <- lm(Price~Pages, subset = c(Type = "p"))
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases
should be Type == "p"

Sorry, I originally had one equal sign before the edit. Also, make sure you have attached your data frame so R knows what Type is.
 

BassBomb

Diamond Member
Nov 25, 2005
8,390
1
81
should be Type == "p"

Sorry, I originally had one equal sign before the edit. Also, make sure you have attached your data frame so R knows what Type is.

That worked, thanks good sir!

Anyone know how to start the Confidence Interval problem?
 

blinky8225

Senior member
Nov 23, 2004
564
0
0
That worked, thanks good sir!

Anyone know how to start the Confidence Interval problem?

I can! You need to know that variance for a y_hat is
sqrt(Var(y_hat)) = S*sqrt(1/n+(x_star-mean(x))^2/SXX)

n = number of observations
x = pages
y_hat = random variable of predicted price
x_star = 250
S = sum((y_i-y_hat_i)^2)/(n-2) = (sum of squared residuals)/(n-2)
SXX = sum(x_i-mean(x))^2)

Then you can use the T distribution.
y_hat +/- qt(0.975, n-2)*sqrt(Var(y_hat))

qt is the R function for the quantile function of the T distribution. n-2 is the degrees of freedom.
 
Last edited:

BassBomb

Diamond Member
Nov 25, 2005
8,390
1
81
Our other problem was a multiple linear regression problem. Given betas, X'X inverse, estimated variance, etc.

First part was a hypothesis test, which I was able to do.

Next parts are confidence intervals that need to be created for beta, or linear combination of betas.

How do I do this part? Is it the same as taking a hypothesis test of B1 wrt 0?
 
Status
Not open for further replies.