r - Prepare data for generalized linear regression -


i want perform glm dataset titanic in r. did following steps prepare data , run glm:

install.packages("reshape") library(reshape) data=(titanic) da=melt(titanic)  m=dim(da)[1] (i in 1:m){     if (da[i,5]!=0){         (k in 1:da[i,5]){             da=rbind(da,da[i,])         }     } }  one=rep(1, dim(da)[1]) x=as.matrix(cbind(one,da[,1:3])) y=da[,4]  m1=glm(y~x,family=binomial) 

i received error message:

error in `[[<-.data.frame`(`*tmp*`, i, value = c(1l, 1l, 1l, 1l, 1l, 1l,  :    replacement has 8932 rows, data has 2233 

can me fix problem?

your main error in specification of formula: right hand side being x, not translated "the main terms each column of x": turns matrix x 1 column (of length 8932) , tries use predictor.

the simplest way kind of thing, ensure columns of interest in 1 data.frame. fortunately, in case, have in da:

m1<-glm(survived~class+sex+age, data=da, family=binomial) 

in addition, default glm add intercept itself, don't need manually specify column of ones (as avoided in above).


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -