DS4PS / cpp-525-sum-2020

Course shell for CPP 525 Foundations of Program Evaluation III for Summer 2020.
http://ds4ps.org/cpp-525-sum-2020/
1 stars 0 forks source link

Lab 06 #4

Open Jigarci3 opened 4 years ago

Jigarci3 commented 4 years ago

Hello, I am getting an error when I try to run the predict function to find the probability change for an average state- has anyone else been able to run it?

Here is my code:

data2 <- with( data, 
               data.frame( Democrats=mean(Democrats), 
                           Evangelics=mean(Evangelics), 
                           Catholics = mean(Catholics),
                           Media= mean(Media),
                           Merck= mean(Merck) ))

data2$Adoption_Prop <- predict( log, newdata = data2, type = "response" )

data2$Adoption_Prop

The error i receive is: Error in $<-.data.frame(*tmp*, Adoption_Prop, value = c(1 = 0.35531731324293, : replacement has 49 rows, data has 1

Thanks!

Dselby86 commented 4 years ago

Look at the dimensions of Data2, before you do you calculate the predicted value. Then look at the results of the predict function. It looks like the data frame and the predict function have different dimensions.

lecy commented 4 years ago

If you are having a hard time with the with() and predict() functions you can also calculate the predicted probabilities using the logit formula.

I find it a little more intuitive because you can see the steps:


m.logit <- glm( Admission ~ LSAT + Essay + GPA, data = df, family = "binomial")

coefficients <- coefficients( m.logit ) 
covariates <- c( 1,  mean(df$LSAT ),  mean(df$Essay),  mean(df$GPA)   )

B <- sum( coefficients * covariates )
p <- 1 / ( 1 + exp( -B ) )

p