1
votes

My linear model is trying to predict the amount gambled based on the variables sex, income, verbal, and status. Sex is a binary variable that is "Male" or "Female" (they are factors), while the rest are all numeric.

lm3 <- lm(gamble ~ sex + status + income + verbal, data=teengamb)

That is my linear model. I'm having trouble predicting the function for a male with mean status, income, and verbal:

newdata <- c(as.factor("Male"), mean(teengamb$status), mean(teengamb$income), mean(teengamb$verbal))
newdata <- data.frame(newdata)
predict(lm3, newdata) 

I'm not sure how to go about doing this.

Note that the way I converted it to Male and Female is:

However, I have converted the 0=male, 1=female to "Male" and "Female."

teengamb$sex[teengamb$sex==0] <- "Male"
teengamb$sex[teengamb$sex==1] <- "Female"
teengamb$sex <- as.factor(teengamb$sex)
1
In what way are you having trouble? Can you show your call to predict? - David Robinson
This is what I have, and it's not working. - user2303557
newdata <- c(as.factor("Male"), mean(teengamb$status), mean(teengamb$income), mean(teengamb$verbal)) newdata <- data.frame(newdata) predict(lm3, newdata) - user2303557
sorry for some reason the line spacing isnt work. but its three lines of code. - user2303557
In the future, click "edit" under your question and edit it into the question (I've done it for you) - David Robinson

1 Answers

4
votes

When you create your newdata data frame, you have to be sure each column has a name:

newdata <- data.frame(sex=0, status=mean(teengamb$status),
                      income=mean(teengamb$income),
                      verbal=mean(teengamb$verbal))
predict(lm3, newdata)
# 28.24252

Also note that sex is represented as 0=male, 1=female (you can see this by doing help(teengamb)).

(Which means, that it should be:

newdata <- data.frame(sex=factor("Male", levels=c("Female", "Male")), 
                      status=mean(teengamb$status),
                      income=mean(teengamb$income),
                      verbal=mean(teengamb$verbal))