You are on page 1of 4
//Ctrl + R to run saved files new; cls} rndseed 12398764; starttime = date; //for the duration of prog JJat the end of the progran end; path_for_all = "C:/Users/Ilaria/Documents/St gallen/Stata & Gauss"; outputfite = "Exan.out"; _// define output file to be used below outputfile = path_for_all $+ outputfile; output file=Aoutputfile reset; //Simple assignments and print statements Variablel = 5; // this assigns the value 5 to variablel Variable2 = Variablel; //this assigns the value of variablel to variable2 Variable3 = "trallala"; // this assigns a string to variable 3 //printing variables Variable3; // a missing assignment operation is a print statement! Variable3;; // a missing assignment operation with ‘double ;' is a print statement without Line break (carriage return) $variable3; // this prints a string //random number creations x = rndu(i,1); // random number Rndu(1,1)3 //Uniform in (8,1) Rndu(1,1)%5+b; //Uniform in [6+b,a+b] Rndn(1,1)3 //Standard normal rndn(1,1)sqrt(variance)+mean; //normally distributed random variables with given mean and variance /Jadding strings /INew_string = old_stringl $+ old_string2; Variables = Variable3 $+ "hahah"; //if, else loops... As soon as one of the elseif condition is fufilled program ends A If A == 1; "A equals 1"; Elseif A<=2; "A smaller or equal to 2"; Elseif 5 "A greater or equal to 3"; Elseif A!=0; "A does not equal 0"; Endif; /fogical operators if xl > @.5 and x2 > 0.5; "both greater than 0.5"; Elseif x1 > 0.5 or x2 <= 0.5; "x1 greater than 0.5"; endif variable = variable + (x > cutoff); //if condition x>cut_off is true that (x > cut_off)=1 else 0. //Identity matrix here 10 = n eye(18) 5 // functions Proc(number_of_outputs) =nane_of_procedure (input, input2,..) 5 local variable, variable2, .; // all variables that exist only in procedure /Iprog code retp(outputi, output2..); //variables being outputed Endp; // end of procedure //calling the function {outputi, output2...} = name_of_procedure (input, input2,. //if there are no returns call name_of_procedure(inputi, input2, 63 64 65 66 67 68 69 70 m1 72 73 14 15 16 17 78 79 80 81 82 83 84 85 86 87 88 39 99 91 92 93 94 95 96 97 98 99 109 1e1 102 193 loa 195 196 197 108 199 119 alt 112 113 414 41s 116 ar 118 119 120 a2. 122 123 124 //0o until ~ runs until the condition is met R= 55 Do until A < 15 //repeats the loop until A= 1 1; endo; //Do while ~ runs until the condition is no Longer met Do while A> 1; //repeats the loop until variable equals 2 AS A= hl; endo} //for loops - repeats the task a fixed number of times For i(first_value_of_i, final_value_of_i, increment_value_of_i); Endfor; //loop operator break; //stop the loop continue; //Go to the top of the loop: /IMatrix Creation Rows1 = 33 Colsl = 4; M1 = zeros(rowsl, colsl); // creates 3 x 4 matrix with zeros M1[2,3] = 25 // row 2, col. 3 is set to 2 M1[3,4]; // row 3, col. 4 is printed (9) MA[.,3]5 // all rows of column 3 M1[3,.1} // all columns of row 3 M1[.2:41; // all rows of columns 2, 3, 4 X = zeros(a,b); // all a x b elenents are @ X = ones(a,b); // all a x b elenents are 1 xX = eye(a); // identity matrix of dimension a x a X = rndu(a,b); // all a x b elements are different random numbers A = seqa(startvalue, increment ,number_of_elements); // vector: additive sequence M = seqm(startvalue, increment ,number_of_elements); // vector: multiplicative sequence X = rndBernoulli(rows_of_x, colunns_of_x, p); // Bernoulli randon matrix with P(x=1) P X = rndMVn(rows_of_x, vector_of_means, covariance_matrix); //Multivariate normal X NCHS) //rows_of_x = sample size //vector_of_means = ones(2,1); //covariance_matrix= eye(2); sigma’2 //covariance_matrix[2,1] = 9.5; rho //covariance_matrix[1,2] = 9.5; rho / [Matrix operations B+ Cs // ‘normal! matrix operator; same for + - BY; // matrix is transposed B .+ C; // each elenent of 8 is multiplied by same element in C B .#. Cs // each element of B is multiplied by full matrix C - Kronecker BC; // columns of C are added after 8 (matrices must have the sane number of ows) A=B|C; // rows of C are added after 8 (matrices must have the same number of cols) meanc(A); // column means of matrix A !! meanc(A') //mean rows cumsume(x); // Cumulatively sum the columns sume(x); // sum of column stde(x); // standard deviaton of all columns of x varCovX(x); // variance-covariance natrix of X corrx(X); // correlation matrix of X median(data_natrix) ; quantile(data_natrix, probabilities) quantiled(dataset, probabilities, variablenames) ; y = inv(x); // inverse of a full rank matrix a >>eee> 125 126 127 128 129 130 431 132 133 134 135 136 137 138 139 149 aaa 142 143 144 145 146 447 148 149 150 151 152 153 154 155 156 157 158 159 169 161 162 163 164 165 166 1s? 168 169 170 ami 472 173 174 17s 176 at 178 179 180 181 182 183 184 ‘invpd(x); // inverse positive definite matrix k = rank(x)3 // rank of any matrix /JOLS regression n=1000; x = ones(n,1) ~ rndn(n,3); //first col 1s, then 3 col around 1 y = sumr(x) + rndn(n,1); //last term = error term betas = invpd(x'x) * x'y3 //OLS equation u = rndn(N,1) * sqrt(sigmaz_u); //u=error term N(@,sigma2_u) //bootstrap N=1000; blindex = rndi(N,1,1|N)5 boot_data = datalb_index,.]; //data is an existing matrix, this operation tells then to extract random rows 1/Graphing Call plotxy(x,y); // 2 dimensional Line plot Call plothist(x,v); // histogramme; x: data, v: breakpoints or # of bins //Import data Load path="this_is_the_path_where_I_store_my_data_for_this_projec X = readr(filehandle, number_of_rows) ; call seekr(filehandle, this_is_the_row_I_need); //if reading should start at a particular row Ue filehandle = datacreate(filename, vnames, ©, 8, vtyp); Filehandle = dataopen(filename,mode) ; Filehandle=Close (fi tehandle) ; /{reading a data set Fin = dataopen(my_data_file,"read"); // Open one file - fin is variable containing filehandle Vnames = getname(my_data_file); // vector with variable names from my_data_file J}writing a data set datafile = "l4_datl"; datafile = path_for_all $+ datafile; let variablenames = Ni N2 N3 N4 NS UL U2 U3 NUL NU2; fout = datacreate(datafile, variablenames, 0, 8, 1); // create new file with same column names Jicreate x ~ a matrix with all the data call writer (fout,x); //check that getorders(x) [2] closeall fout; call dstat(datafile,0); //descriptive statistics of the data just created # of variables in variablenames Do until eof (fin); // eof(fin) is one if reading file is completed x = readr(fin,1900); // (up to) 1900 rows read x[.,8] = x[.,5] * 2; // column 5 is doubled call writer(fout, x); // modified matrix X is written to new file Endo; // do this until all rows have been read Closeall fin, fout; //Descriptive statatistics of imported data {varname, mean, var, std, min, max, valid_cases, missing_cases} = dstat(dataset, variable_names) ; //duration of progranme starttime = date; proc(®)=program_duration(starttime) ; local endtime hss endtime = date; hs = ethsec(starttime, endtime) ; "Duration of programme was " etstr (hs) 5 endp;

You might also like