Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • asdoc or estpost to combine summary statistics and statistical tests

    Dear all

    I am trying to create a table that contains summary statistics with chi2 and t-test.
    I tried both asdoc and eststo/ estpost commands, but I still do not succeed to produce the tab I would like.

    With eststo/ estpost I tried the following

    Code:
    eststo clear
     
    eststo old: qui estpost sum gender age career_path farm_size farm_typology acquire_old farm_turnover if survey == 1
    eststo young: qui estpost sum gender age career_path farm_size farm_typology acquire_old farm_turnover if survey ==0
    * this is for continuous variables (I have more in my sample)
    local var1 age farm_turnover
    eststo test1: qui estpost ttest `var1', by(survey) unequal
    local var2 gender career_path farm_size farm_typology acquire_old
    eststo test2: qui estpost tab `var2' survey, chi2
    local pval1= e(p)
    mat define pval=(`pval1', `pval2')
    *here I would need a matrix that save all the pvalues in test2
    mat colnames pval= "`varname’"
    estadd matrix pval= pval
    estimates store pval_test2
    tab `vars' survey, chi2eststo ttest: qui estpost ttest price mpg, by(foreign) unequal
    
    esttab old young test1 test2, replace star(* 0.10 ** 0.05 *** 0.01) nonumber noobs label ///
    alignment(rrrr) ///
    cells("count(pattern(1 1 0 0) fmt(0)) mean(pattern(1 1 0 0) fmt(2)) b(star pattern(0 0 1 0) fmt(2)) pval(star pval(pval) pattern(0 0 0 1) fmt(3)) ") ///
    mtitle("Old" "Young" "ttest" "Chi2") ///
    collabels("N" "Mean" "Mean Diff." "p-value")
    With asdoc I like the possibility of using i.var in my descriptive statistics. Anyway I was not able to insert a new column with the statistical tests
    Code:
    global var i.gender age career_path i.farm_size i.farm_typology i.acquire_old farm_turnover
    row( \i, \i, Old, \i, \i, Young,\i) replace
    asdoc, row( N, Mean, SD, N, Mean, SD)
    bys survey: sum  $var, label save(file3) replace dec(2)
    asdoc, accum(`r(N)', `r(mean)', `r(sd)')
    tabulate $var survey, chi2
    asdoc, accum(`r(chi2)'  `r(p)')
    asdoc, row( `var’, $accum)
    In the command above there is an error for sure.
    I would like to have a table like this and in addition have the sd in parentheses below the mean and the stars in the statistical test
    Variable Old Young Total mean diff
    Obs Mean (sd) Obs. Mean (sd) Obs
    ttest/chi2
    Gender . . . . .
    Male
    Female
    Age of the interviewed
    Early involvement in agriculture
    Farm size
    Small farms: EUR 2 000 – < EUR 8
    Medium-sized farms: EUR 8 000 –
    Large farms: EUR 25 000 – < EUR
    Very large farms: ≥ EUR 100 000
    Farm typology
    Innovator
    Diversifier
    Environmentalist
    Traditionalist
    here an example of my data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte gender float(age career_path farm_size farm_typology acquire_old farm_turnover)
    1 57 0 1 4 1   7000
    0 58 0 3 4 1  50000
    0 56 1 4 3 1 200000
    0 63 0 3 2 2  61000
    0 67 0 3 4 2  40000
    0 60 0 4 3 1 230000
    0 65 0 4 4 1 100000
    0 75 0 2 2 1  15000
    0 59 1 3 4 1  85000
    0 66 0 3 2 1  40000
    0 56 0 2 2 1  10000
    0 56 0 3 2 1  35000
    0 59 1 3 4 1  55000
    1 57 0 3 2 1  40000
    0 68 0 4 1 1 130000
    0 62 0 3 1 1  30000
    0 72 0 2 1 1  20000
    0 75 0 2 4 2  10000
    0 61 0 3 1 1  50000
    1 56 0 3 4 1  75000
    1 64 1 2 3 2  20000
    0 56 0 3 4 1  50000
    0 66 0 3 4 2  75000
    0 61 0 3 2 1  70000
    0 59 0 4 2 1 120000
    0 59 1 3 3 1  70000
    0 73 1 4 2 2 180000
    0 62 0 2 4 1  10000
    1 58 0 3 2 1  55000
    0 58 1 4 3 1 240000
    0 69 0 3 4 1  30000
    0 56 0 3 4 1  50000
    1 60 0 3 4 1  54300
    0 66 0 2 2 1  20000
    1 61 0 3 4 1  30000
    1 65 0 2 2 1  10000
    0 66 1 3 4 1  65000
    0 72 0 4 3 2 110000
    0 63 0 3 4 2  90000
    0 68 0 3 1 1  90000
    1 68 0 2 2 1  10000
    0 58 0 1 1 1   5000
    0 67 1 4 3 2 110000
    1 57 0 3 4 1  60000
    0 57 0 4 4 1 150000
    0 68 0 4 3 1 100000
    0 70 1 3 4 2  70000
    1 81 0 3 2 1  95000
    0 65 0 1 2 2   6000
    0 63 1 3 3 2  90000
    0 70 1 3 3 1  70000
    0 56 0 4 4 1 135000
    1 56 0 4 2 1 106000
    0 57 0 2 2 1  15000
    0 61 0 3 3 1  70000
    0 62 0 4 4 1 100000
    0 64 0 2 1 1   8000
    0 57 1 4 3 1 650000
    1 58 0 3 1 1  50000
    1 60 0 3 4 1  65000
    0 61 0 3 4 1  35000
    0 65 0 2 2 1   8000
    0 73 1 4 2 1 160000
    0 67 1 4 3 1 180000
    1 56 0 2 4 1  20000
    0 70 0 3 2 1  65000
    0 56 0 2 2 1  15000
    0 59 0 3 2 1  30000
    0 66 0 2 4 1  10000
    0 66 0 4 3 1 110000
    0 63 0 3 2 2  32000
    0 66 1 4 3 2 120000
    1 56 0 2 2 1  13000
    0 71 1 3 3 2  70000
    0 69 0 4 3 2 100000
    0 60 0 2 4 1  15000
    0 60 1 4 3 1 230000
    0 58 0 2 2 1  20000
    0 71 0 3 3 1  90000
    0 56 0 3 4 1  30000
    0 57 0 3 2 1  30000
    1 61 0 2 4 1  20000
    0 74 1 3 3 1  75000
    1 56 0 2 2 1  20000
    0 72 0 3 4 1  30000
    0 66 0 2 4 1  24000
    0 70 0 3 4 1  65000
    0 67 0 4 3 2 140000
    0 61 0 3 3 1  28000
    0 58 0 3 1 1  60000
    0 65 0 3 2 1  30000
    0 56 0 3 2 1  30000
    1 61 0 1 1 1   5000
    1 59 0 2 2 1  10000
    1 59 1 3 4 1  60000
    0 59 1 4 2 2 190000
    0 63 1 3 3 2  75000
    0 63 0 3 2 1  75000
    0 71 1 3 3 2  75000
    1 65 0 1 4 1   6000
    end
    label values gender gender
    label def gender 0 "Male", modify
    label def gender 1 "Female", modify
    label values farm_size farmsize
    label def farmsize 1 "Small farms: EUR 2 000 – < EUR 8 000", modify
    label def farmsize 2 "Medium-sized farms: EUR 8 000 – < EUR 25 000", modify
    label def farmsize 3 "Large farms: EUR 25 000 – < EUR 100 000", modify
    label def farmsize 4 "Very large farms: ≥ EUR 100 000", modify
    label values farm_typology farm_typology
    label def farm_typology 1 "Innovator", modify
    label def farm_typology 2 "Diversifier", modify
    label def farm_typology 3 "Environmentalist", modify
    label def farm_typology 4 "Traditionalist", modify
    label values acquire_old acq_1
    label def acq_1 1 "Inherited or donated", modify
    label def acq_1 2 "Entirely or partially bought/rented", modify
    Last edited by Federica Di Marcantonio; 05 Nov 2024, 09:12.
Working...
X