How to round the results of a GLM predict in Julia -
i trying simple logistic regression in julia. julia's typing system seems causing me problems. basically, glm predict gives me array of probabilities. want simple round if probability >= 0.5, 1, otherwise 0. labels integers.
no matter do, can't convert dataarray returned predict int64. if create adhoc dataarray, can round fine. though both show type of dataarrays.dataarray{float64,1}. i've tried things pred>0.5, fails similarly. there magic return value predict, beyond type, makes different other dataarray in short program.
using dataframes; using glm; df = readtable("./data/titanic-dataset.csv"); delete!(df, :passengerid); delete!(df, :name); delete!(df, :ticket); delete!(df, :cabin); pool!(df, [:sex]); pool!(df, [:embarked]); df[isna.(df[:age]),:age] = median(df[ .~isna.(df[:age]),:age]) model = glm(@formula(survived ~ pclass + sex + age + sibsp + parch + fare + embarked), df, binomial(), logitlink()); pred = predict(model,df); z = dataarray([1.0,2.0,3.0]); println(typeof(z)); println(typeof(pred)); println(round.(int64,z)); # why work? println(round.(int64,pred)); # not? the output is:
dataarrays.dataarray{float64,1} dataarrays.dataarray{float64,1} [1, 2, 3] methoderror: no method matching round(::type{int64}, ::dataarrays.natype) closest candidates are: round(::type{t<:integer}, ::integer) t<:integer @ int.jl:408 round(::type{t<:integer}, ::float16) t<:integer @ float.jl:338 round(::type{t<:union{signed, unsigned}}, ::bigfloat) t<:union{signed, unsigned} @ mpfr.jl:214 ... stacktrace: [1] macro expansion @ c:\users\jheaton\.julia\v0.6\dataarrays\src\broadcast.jl:32 [inlined] [2] macro expansion @ .\cartesian.jl:64 [inlined] [3] macro expansion @ c:\users\jheaton\.julia\v0.6\dataarrays\src\broadcast.jl:111 [inlined] [4] _broadcast!(::dataarrays.##116#117{int64,base.#round}, ::dataarrays.dataarray{int64,1}, ::dataarrays.dataarray{float64,1}) @ c:\users\jheaton\.julia\v0.6\dataarrays\src\broadcast.jl:67 [5] broadcast!(::function, ::dataarrays.dataarray{int64,1}, ::type{int64}, ::dataarrays.dataarray{float64,1}) @ c:\users\jheaton\.julia\v0.6\dataarrays\src\broadcast.jl:169 [6] broadcast(::function, ::type{t} t, ::dataarrays.dataarray{float64,1}) @ .\broadcast.jl:434 [7] include_string(::string, ::string) @ .\loading.jl:515
you can't create integers when have nas in z. can round. them (in case you'll dataarray of floats), when try make them int complain because na can't int64. instead do
convert(dataarray{int}, round.(z)) also, nicer post example using data available in package rather local dataset on computer.
Comments
Post a Comment