2
votes

I am using glm() function in R with link= log to fit my model. I read on various websites that fitted() returns the value which we can compare with the original data as compared to the predict(). I am facing some problem while fitting the model.

data<-read.csv("training.csv")
data$X2 <- as.Date(data$X2, format="%m/%d/%Y")
data$X3 <- as.Date(data$X3, format="%m/%d/%Y")
data_subset <- subset(...)
attach(data_subset)

#define variable
Y<-cbind(Y)
X<-cbind(X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X14)

# correlation among variables
cor(Y,X)

model <- glm(Y ~ X , data_subset,family=Gamma(link="log"))
summary(model)

detach(data_subset)

validation_data<-read.csv("validation.csv")

validation_data$X2 <- as.Date(validation_data$X2, format="%m/%d/%Y")
validation_data$X3 <- as.Date(validation_data$X3, format="%m/%d/%Y")

attach(validation_data)
predicted_valid<-predict(model, newdata=validation_data)

I am not sure how does predict work with gamma log link. I want to transform the predicted values so that it can be compared with the original data. Can someone please help me.

2

2 Answers

3
votes

Looks to me like fitted doesn't work the way you seem to think it does.

You probably want to use predict there, since you seem to want to pass it data.

see ?fitted vs ?predict

3
votes

Add type="response" to your predict call, to get predictions on the response scale. See ?predict.glm.

predict(model, newdata=*, type="response")