Search code examples
rconditional-statementsmeansapply

Find conditional expectation in R using sapply


I have one data frame that has 6 variables. df is the name of my data frame.

I found the expectation of e (variable in df) using

Ee <- mean(df[["e"]])

How do I find E[e|Z=z] for z in {0,1}?

Similarly, how do I find E[e|X=x] for x in {1...20} using the sapply function?


Solution

  • Here's a thought:

    set.seed(42)
    sampdata <- data.frame(e = runif(1000), z = sample(0:1, size=1000, replace=TRUE), x = sample(1:20, size=1000, replace=TRUE))
    head(sampdata)
    #           e z  x
    # 1 0.9148060 1 15
    # 2 0.9370754 0  2
    # 3 0.2861395 1 13
    # 4 0.8304476 1 12
    # 5 0.6417455 1  4
    # 6 0.5190959 0  7
    
    aggregate(e ~ z, data = sampdata, FUN = mean)
    #   z         e
    # 1 0 0.4910876
    # 2 1 0.4852118
    aggregate(e ~ x, data = sampdata, FUN = mean)
    #     x         e
    # 1   1 0.5097038
    # 2   2 0.4495141
    # 3   3 0.5077897
    # 4   4 0.5300375
    # 5   5 0.4549345
    # 6   6 0.5122537
    # 7   7 0.4704425
    # 8   8 0.4911532
    # 9   9 0.5572367
    # 10 10 0.4634067
    # 11 11 0.4408758
    # 12 12 0.4815633
    # 13 13 0.5503166
    # 14 14 0.4922317
    # 15 15 0.5205427
    # 16 16 0.4999023
    # 17 17 0.4784551
    # 18 18 0.4282990
    # 19 19 0.4202285
    # 20 20 0.4852303
    

    But if you feel you must use sapply, then this can be equivalent.

    sapply(setNames(nm = unique(sampdata$z)), function(Z) mean(sampdata[["e"]][ sampdata[["z"]] == Z ]))
    #         1         0 
    # 0.4852118 0.4910876 
    sapply(setNames(nm = unique(sampdata$x)), function(X) mean(sampdata[["e"]][ sampdata[["x"]] == X ]))
    #        15         2        13        12         4         7        19        16        10         1 
    # 0.5205427 0.4495141 0.5503166 0.4815633 0.5300375 0.4704425 0.4202285 0.4999023 0.4634067 0.5097038 
    #         9         3        14        18        11        20         5         8        17         6 
    # 0.5572367 0.5077897 0.4922317 0.4282990 0.4408758 0.4852303 0.4549345 0.4911532 0.4784551 0.5122537