Home > OS >  Function for returning estimates based on direction of confidence interval values
Function for returning estimates based on direction of confidence interval values

Time:12-10

I have a dataframe with parameter estimates, the lower and upper 95% confidence interval from a local statistics model. I want to create a new column that returns the parameter estimates if the lower and upper confidence intervals are in the same direction (that is, when both are positive or negative) else a null or na. Using the table below as an example

enter image description here

I want a dataframe that looks like this

enter image description here

CodePudding user response:

Here is a solution using dplyr, but of course you can use the same strategy based on ifelse with traditional variable assignment.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- tribble(~est, ~lci, ~uci,
              0.25, 0.12, 0.35,
              -0.36, -0.45, 0.01,
              -0.56, -0.62, -0.34)

df |> 
  mutate(SIG = ifelse(lci*uci>0, est, NA))
#> # A tibble: 3 × 4
#>     est   lci   uci   SIG
#>   <dbl> <dbl> <dbl> <dbl>
#> 1  0.25  0.12  0.35  0.25
#> 2 -0.36 -0.45  0.01 NA   
#> 3 -0.56 -0.62 -0.34 -0.56

Created on 2021-12-09 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related