Search code examples
stata

Scatterplot of two column matrices


I am currently having a problem with using Stata to draw a scatterplot when A (independent variable) and B (dependent variable) are two matrix vectors of size 1 x 1000.

I used the command twoway scatterbut this keeps failing because Stata deems A and B not to be variables. However, I defined A and B with the command matrix define.

The Variables window is empty and I am not sure why A and B are not variables.

Sample Code:

matrix define A = [1,2,3,4,5,6,7,8,9,10]' 

matrix define B = [2,3,4,5,6,7,8,9,10]'

//drawing scatterplot with A vs B and overlay a horizontal line x = 5 onto the scatterplot.   
twoway scatter A B || xline(5)

Can I declare a matrix as a variable-type and save it so that I can re-use it with twoway scatter?


Solution

  • You need to use the svmat command to first create the variables and then draw the graph:

    clear
    matrix define A = (1,2,3,4,5,6,7,8,9,10)'
    matrix define B = (2,3,4,5,6,7,8,9,10)'
    
    svmat A
    svmat B
    
    twoway scatter A B, xline(5)
    

    Matrices and variables in Stata are two different things.