error in assigning NA to Float64 in a DataFrame

48 views
Skip to first unread message

Min-Woong Sohn

unread,
Dec 30, 2015, 1:39:49 PM12/30/15
to julia-stats
I am trying to work out the following example:

using DataFrames, GLM
N = 1000
data = DataFrame(X=randn(N),Y=randn(N))
beta_0 = 0
beta_1 = 1
index = (beta_0 + beta_1*data[:X])
probability = cdf(Normal(0,1),index)
D = zeros(N)
for i=1:N
    D[i]=rand(Bernoulli(probability[i]))
end
data[:D]=D
data2 = data
data2[data2[:D] .== 0.0, :Y] = NA
data2

data3 = DataFrame(rand(MvNormal([0,0.],[1 .5;.5 1]),N)')
names!(data3,[:X,:Y])
mean(convert(Array,data3),1)
cov(convert(Array,data3))
index = (beta_0 + beta_1*data3[:X])
probability = cdf(Normal(0,1),index)
D = zeros(N)
for i=1:N
    D[i]=rand(Bernoulli(probability[i]))
end
data3[:D]=D
data4 = data3
data4[data4[:D] .== 0.0, :Y] = NA

Lines 14 and 29 are exactly the same but the first one works fine and the second one gives an error: LoadError: MethodError: `convert` has no method matching convert(::Type{Float64}, ::DataArrays.NAtype)

Does anyone know what's happening here?


Alex Williams

unread,
Dec 30, 2015, 2:42:54 PM12/30/15
to julia...@googlegroups.com
The problem is that data4[:Y] is an Array, not a DataArray... so you can't add NA values to it. I think it works if you add the following command right before the last line:

data4[:Y] = convert(DataArray,data4[:Y])



--
You received this message because you are subscribed to the Google Groups "julia-stats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to julia-stats...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Min-Woong Sohn

unread,
Dec 30, 2015, 4:40:33 PM12/30/15
to julia-stats
Thanks a bunch. It works now.
Reply all
Reply to author
Forward
0 new messages