## MDC Code (updated)

| Gabriel |

I continue to make baby steps towards a self-contained ado file for my multilevel diffusion curves (MDC) technique. This is tricky as the model has to be run through a quadratic. My co-author and his RA wrote a spreadsheet that does this, but it’s kind of a pain to copy the output from Stata into the spreadsheet. Anyway, the current version of the script still requires you to run it through the spreadsheet, but it makes it a lot more convenient by putting it in the shape the spreadsheet expects and it gives you the Bass model for the baseline (no independent variables) model. Eventually I’ll work myself up to figuring out how to do the matrix multiplication in Stata and then it will be totally plug and chug.
Here’s how you’d specify:

```mdcrun newadoptions innovationlevelvariable timelevelvariable, i(serialno) nt(laggedcumulativeadoptions) saving(resultsfile)
```

Here’s the program itself:

```capture program drop quadraticprocess
matrix betas=e(b)
matrix varcovar=e(V)
mata: st_matrix("se",sqrt(diagonal(st_matrix("varcovar")))')
matrix results= betas\se
matrix rowname results = beta se
* A B C in the quadratic sense
matrix colname results = B C A

*several abs() fcns necessary for se to avoid imaginary numbers and keep se>=0
local nmax	= (-results[1,1] - ((results[1,1]^2)-4*results[1,3]*results[1,2])^0.5) / (2*results[1,2])
local nmax_se	= abs(`nmax'-(-(results[2,1]+results[1,1])-((results[2,1]+results[1,1])^2-4*(results[2,3]+results[1,3])*(results[2,2]+results[1,2]))^0.5)/2/(results[2,2]+results[1,2]))
local endo	= - results[1,2]
local endo_se	= abs(`endo'-(-results[2,2]))
local exo	= results[1,3] / `nmax'
local exo_se	= abs(`exo'- results[2,3] /`nmax_se')

return local N		= `e(N)'
return local nmax	= `nmax'
return local nmax_se	= `nmax_se'
return local endo	= `endo'
return local endo_se	= `endo_se'
return local exo	= `exo'
return local exo_se	= `exo_se'
end

capture program drop mdcrun
program define mdcrun
*dependency: mat2txt
set more off
syntax varlist , i(string asis) nt(string asis) [SAVing(string asis)]

disp as text "{hline 80}"
disp as text "MDCRUN"
disp as text "This code gives information which must be interpreted"
disp as text "with the spreadsheet at http://gabrielr.bol.ucla.edu/mdc.xls"
disp as text "{hline 80}"

gettoken first varlist : varlist

preserve
gen cons=1
foreach var in `varlist' cons {
quietly drop if `var'==.
quietly gen `var'_1=`nt'*`var'
quietly gen `var'_2=`nt'*`nt'*`var'
}

* create `varlist_ext' as an alternate varlist macro that has the interactions
foreach var in `varlist' {
local varlist_ext="`varlist_ext' `var'_1 `var'_2 `var'"
}
local varlist_ext="`varlist_ext' cons_1 cons_2"

quietly tabstat `varlist', save stat(mean sd)
matrix varlistdescriptive=r(StatTotal)'
matrix varlistdescriptive=varlistdescriptive\(1,0)
disp as text "Baseline model"
xtreg `first' cons_1 cons_2, re i(`i')
matrix baselineresults = results[1...,"A"], results[1...,"B"], results[1...,"C"]
matrix colnames baselineresults = raw nt nt2
matrix baselineresults = baselineresults'
disp as text "model: adds = ( `r(exo)' + `r(endo)' Nt ) (`r(nmax)' - Nt) "
disp "error: adds = ( `r(exo_se)' + `r(endo_se)' Nt ) (`r(nmax_se)' - Nt) "
disp "{hline 80}"
disp "Covariates Model"
*coefficients are vars + interactions with nt and nt^2"
xtreg `first' `varlist_ext', re i(`i')
matrix betas=e(b)
matrix varcovar=e(V)
mata: st_matrix("se",sqrt(diagonal(st_matrix("varcovar")))')
foreach figure in betas se {
local counter=1
foreach var in `varlist' cons {
matrix `figure'_`var' = (`figure'[1,`counter'], `figure'[1,`counter'+1], `figure'[1,`counter'+2])
local counter=`counter'+3
}
matrix `figure'grid = (., ., .)
foreach var in `varlist' cons {
matrix `figure'grid = `figure'grid \ `figure'_`var'
}
matrix define `figure'grid = `figure'grid[2...,....]
matrix colnames `figure'grid = nt nt2 raw
matrix rownames `figure'grid = `varlist' cons
}
matrix biggrid=varlistdescriptive, betasgrid[1...,"raw"], segrid[1...,"raw"], betasgrid[1...,"nt"], segrid[1...,"nt"], betasgrid[1...,"nt2"], segrid[1...,"nt2"]
matrix colnames biggrid = mean sd b_raw se_raw b_nt se_nt b_nt2 se_nt2
matrix rownames biggrid = `varlist' cons
di as text "{hline 80}"
di as text "The following should be fed into the spreadsheet http://gabrielr.bol.ucla.edu/mdc.xls"
disp "The baseline model goes in L7:N9"
*transpose? beta, se should be columns
matlist baselineresults
disp as text _newline "The covariate model should go in cells xx:xx"
di as text "Excel Cols:  |         H & J        |       D & F         |         L & N       |      T & V" _continue
matlist biggrid
disp "Please see AC-AJ for interpretation"
*write to disk or print to screen: varlistdescriptive col 1+2, beta_col3, se_col3, beta_col1, se_col1, beta_col2, se_col2
*tab or comma as "delimchar"
disp as text "{hline 80}"
disp as text "For citation and help with theory/interpretation, see"
disp as text `"Rossman, Chiu, and Mol. 2008. "Modeling Diffusions of"'
disp "Multiple Innovations Via Multilevel Diffusion Curves:"
disp `"Payola in Pop Music Radio" Sociological Methodology"'
disp "38:201-230." _newline
if "`saving'"!="" {
mat2txt , matrix(baselineresults) saving(`saving') title(baseline model, L7:N9) replace
mat2txt , matrix(biggrid) saving(`saving') title(covariates, D-V) append
}
restore
end
```

(Btw, here’s the older version of the MDC code.)

Entry filed under: Uncategorized. Tags: , .

• 1. PJ Lamberson  |  May 15, 2013 at 2:58 pm
• 2. gabrielrossman  |  May 15, 2013 at 3:22 pm