Stata 鐭╅樀鎿嶄綔
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stata 鐭╅樀鎿嶄綔相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/matrix' title='matrix'>matrix 闄勫姞 tle trace rod 瀹氫箟 varname 缁撴灉 鍛戒护
涓€銆佺敓鎴愮煩闃?/p>
matrix 鐭╅樀鍚?= (1,2,3 4,5,6)
鍙橀噺杞煩闃?mkmat
鐭╅樀杞彉閲?svmat
浜?/p>
1 matrix list 鐭╅樀鍚?[, noblank nohalf noheader nonames format(%fmt) title(string) nodotz] // 鍒楃ず鐭╅樀 2 * 鏇翠负缁嗚嚧鍦板垪绀虹煩闃? 3 #delimit ; 4 matrix Htest = ( 12.30, 2, .00044642 5 2.17, 1, .35332874 6 8.81, 3, .04022625 7 20.05, 6, .00106763 ) ; 8 #delimit cr 9 matrix rownames Htest = trunk length weight overall // 瀹氫箟琛屽悕 10 matrix colnames Htest = chi2 df p // 瀹氫箟鍒楀悕 11 matlist Htest // 娣诲姞杈规 12 matrix rename 鍘熺煩闃靛悕 鏂扮煩闃靛悕 // 鐭╅樀鏇村悕 13 * 鐭╅樀鐨勮鏁板拰鍒楁暟 14 matrix 鐭╅樀鍚?= (1,2,3 4,5,6) 15 scalar ra = rowsof(鐭╅樀鍚? 16 scalar ca = colsof(鐭╅樀鍚? 17 dis in g "鐭╅樀 鐭╅樀鍚?鐨勮鏁版槸: " in y ra 18 dis in g "鐭╅樀 鐭╅樀鍚?鐨勫垪鏁版槸: " in y ca 19 matrix dir // 鏌ユ壘鐭╅樀 20 matrix drop 鐭╅樀鍚? // 鍒犻櫎鐭╅樀 21 display matmissing(鐭╅樀鍚? // 鏄剧ず鐭╅樀缂烘紡鍊间釜鏁?/span> 22 * 鐭╅樀鐨勮鍚嶅拰鍒楀悕 23 matrix rownames 鐭╅樀鍚?= 琛屽悕(绌烘牸鍒嗛殧) // 琛屽悕 24 matrix colnames 鐭╅樀鍚?= 鍒楀悕(绌烘牸鍒嗛殧) // 鍒楀悕 25 * 鐭╅樀鐨勯€夋嫨 26 matrix 鏂扮煩闃靛悕 = 鐭╅樀鍚峓琛岀储寮?鍒楃储寮昡 // 涓棿鐪佺暐锛?. 鍏ㄩ儴鐪佺暐锛?... 灏鹃儴鐪佺暐锛?.. 27 matrix 鐭╅樀涓兂瑕佷慨鏀圭殑鍏冪礌 = 淇敼鍊? // 濡傛灉鎯宠淇敼鍖哄煙锛屽彧闇€鍦ㄧ瓑鍙峰乏渚у~鍏ヤ慨鏀瑰尯鍩熷乏涓婅鐨勫厓绱犱綅缃嵆鍙?/span> 28 * 鐭╅樀鐨勫悎骞?29 matrix aa = [a1, a2] // 妯悜鍚堝苟涓や釜鐭╅樀 30 matrix aaa = [a1 a2] // 绾靛悜杩藉姞涓や釜鐭╅樀 31 * 甯哥敤鐭╅樀鐨勫畾涔?32 matrix I = I(n) // 鍗曚綅鐭╅樀 33 matrix 鐭╅樀鍚?= J(琛屾暟,鍒楁暟,甯告暟) 34 matrix r3 = matuniform(10,4) // 鐢熸垚涓€涓?0*4鐨勯殢鏈烘暟鐭╅樀锛岄殢鏈烘暟鍖洪棿涓?0,1) 35 * 灏嗕竴缁寸煩闃佃浆鎹㈡垚瀵硅鐭╅樀 36 mat u = J(5,1,-0.5) 37 mat du = diag(u) // 鍙栧嚭瀵硅鍏冪礌 38 * 灏嗗彉閲忚浆鎹负鐭╅樀 39 mkmat varlist [if] [in] [, matrix(matname) nomissing // 鍗曞彉閲忥細鐭╅樀鍚嶉粯璁や负鍙橀噺鍚嶏紝閫夐」nomissing琛ㄧず浠呭寘鍚潪缂烘紡鍊?/span> 40 * 灏嗙煩闃佃浆鍖栦负鍙橀噺 41 xsvmat 鐭╅樀鍚? list(,) // 浠ュ彉閲忔柟寮忓垪绀虹煩闃电殑鍐呭 42 * 鐢ㄧ煩闃靛瓨鍌ㄧ粺璁$粨鏋?43 makematrix [matrix_name], from(results_list) [production_options] [list_options]:["]command["] [varlist] ... [, options ] 44 * 鐭╅樀杩愮畻 45 mgen exprlist , in(matname) out(matname) [ common(term) ] // in-杩涜鎿嶄綔鐨勭煩闃?out-鏂扮煩闃?exprlist锛氭暟瀛﹁〃杈惧紡 46 * 淇濆瓨鐭╅樀 47 matsave matrix [, replace saving dropall path(path) type(type) ] // 淇濆瓨鍒癲ta涓?/span> 48 mat2txt , matrix(matrixname) saving(filename) [ title(text) note(text) format(formatlist) replace append ] // 淇濆瓨涓簍xt鏍煎紡 49 dataout <using filename> [, options] // word:杞垚rtf鏍煎紡鐨剋ord鏂囨。 excel:杞垚xml鏍煎紡鐨別xcel鏂囨。
涓?/p>
1 * 鍩烘湰杩愮畻 2 matrix A = (1,23,4) 3 matrix B = (5,79,2) 4 matrix C = A+B // 鍔犳硶 5 matrix B = A-B // 鍑忔硶 6 matrix X = (1,12,58,04,5) 7 matrix C = 3*X*A鈥?/span>*B // 涔樻硶 8 matrix D = (X鈥?/span>*X - A鈥?/span>*A)/4 9 matrix D = A#D // 鐩翠箻 10 matrix E = hadamard(A,B) // Hadamard涔樻硶 11 * 鐭╅樀鍏冪礌鐨勬暟瀛﹀彉鎹?12 math B = function(A) // 鍙緵璋冪敤鐨刦unction锛?help math functions 13 mgen exprlist , in(matname) out(matname) [ common(term) ] // 鍒嗗垪鍙樻崲锛?v1=ln(c1)" 涓嶅彲浠ュ啓涓?"v1 = ln(c1)" 14 * 鐭╅樀涓庡崟鍊肩殑杩愮畻 15 scalar c = 5 // 鍗曞€糲 16 mat D = J(4,4,1) // 鐭╅樀D 17 mat Dc = D*c // mat cD = c*D 鐭╅樀涓庡崟鍊肩浉涔?/span> 18 mat D_c = D/c // 鐭╅樀涓庡崟鍊肩浉闄?/span> 19 * 鐭╅樀鐨勮浆缃? 琛屽垪浜掓崲 20 matrix A = (-1, 2 3, 4 ) 21 mat At = A鈥?/span> // 杞疆杩愮畻浼樺厛浜庝箻娉曡繍绠?/span> 22 * 鐭╅樀鐨勯€嗙煩闃?23 scalar detA = det(A) // 鐭╅樀鐨勮鍒楀紡 24 dis issym(A) // 鍒ゆ柇涓€涓煩闃垫槸鍚︿负瀵圭О鐭╅樀 25 mat invA = inv(A) // 姹傜煩闃电殑閫嗙煩闃?/span> 26 * 鐭╅樀鐨勫悜閲忓寲 27 mat vA = vec(A) 28 mat dA = vecdiag(A) // 鍚戦噺鍖栨柟闃电殑瀵硅鍏冪礌 29 * 鐭╅樀鐨勫瑙掑€?trace) 30 matrix Atr = trace(A) // 鏂归樀鐨勫瑙掑厓绱犱箣鍜?/span>
鍥?/p>
matrix accum A = varlist [if] [in] [weight] [, noconstant deviations means(M) absorb(varname)] // matrix accum 璇硶 matrix vecaccum a = varlist [if] [in] [weight] [, noconstant] // matrix vecaccum璇硶 *- 鍑犱釜閲嶈閫夐」锛?* (1) noconstant 涓嶅湪 X 鐭╅樀涓嚜鍔ㄩ檮鍔犲父鏁伴」锛?* (2) deviation 閲囩敤绂诲樊鐨勫舰寮? *-eg1- 绾挎€фā鍨嬬殑 OLS 浼拌 *-鐩殑锛氭眰鍙?b = inv(X鈥?/span>X)*X鈥?/span>y * 鍏朵腑锛寉 = price, * X =(weight,mpg,Cons) sysuse auto, clear * 鏂规硶1锛氫粎浣跨敤 matrix accum 鍛戒护 * 鎬濊矾: 鑻?A = (y, X)锛?鍒?* * [ y鈥?/span>y y鈥?/span>X ] * mat accum (A) = S = (y, X)鈥?/span>(y, X) = [ ] * [ X鈥?/span>y X鈥?/span>X ] matrix accum S = price weight mpg // y=price, X=[weight mpg] mat list S matrix XX = S[2..., 2...] matrix Xy = S[2..., 1] mat b = inv(XX)*Xy mat list b reg price weight mpg,nohead // 妫€楠屼笂杩扮粨鏋?/span> * 鏂规硶2锛氱粨鍚堜娇鐢?matrix accum 鍜?matrix vecaccum mat accum XX = weight mpg mat vecaccum yX = price weight mpg mat Xy = yX鈥?mat b = inv(XX)*Xy mat list b reg price weight mpg, noheader // 妫€楠屼笂杩扮粨鏋?/span> * -eg2- 鑾峰彇鍙橀噺鐨勭浉鍏崇郴鏁扮煩闃?sysuse auto, clear corr price weight mpg length * 鍔犳潈浜や箻鐭╅樀 -mat glsaccum- matrix glsaccum A = varlist [if] [in] [weight], group(groupvar) glsmat(W|stringvar) row(rowvar) [noconstant] // 鍩烘湰璇硶 *-mat glsaccum 鐨勫畾涔夛細mat glsaccum(X) = S = X鈥?/span>BX * 鍏朵腑锛孊 涓烘潈閲嶇煩闃碉紝瀹氫箟濡備笅锛?* [ W_1 0 ... 0 ] * | 0 W_2 ... 0 | * B = | . . . . | W_k(k=1,2,...,K) 琛ㄧず绗?k 缁勮瀵熷€肩殑鏉冮噸鐭╅樀锛屾槸涓€涓柟闃?* | . . . . | * [ 0 0 ... W_k ] * 鑻?X 涔熸牴鎹粍鍒畾涔夛紝鍒欏彲琛ㄧず涓猴細 * [ X_1 ] * | X_2 | * X = | . | * | . | * [ X_k ] * 鐢辨鍙互鏇翠负缁嗚嚧鐨勪簡瑙e埌 glsaccum 鐨勫畾涔夋柟寮忥細X鈥?/span>BX = X1鈥?/span>W1X1 + X2鈥?/span>W2X2 + ... + X_k鈥?/span>*W_k*X_k *- 搴旂敤涓句緥锛歐hite(1980) 寮傛柟宸ǔ鍋ユ€ф爣鍑嗚鐨勮绠?* * Var(b) = inv(X鈥?/span>X)*(X鈥?/span>WX)*inv(X鈥?/span>X) // White(1980)绋冲仴鎬ф柟宸?鍗忔柟宸煩闃? * * 鍏朵腑锛?* * [ e1^2 0 ... 0 ] * | 0 e2^2 ... 0 | * W = | . . . . | * | . . . . | * [ 0 0 ... eN^2 ] NXN 鐭╅樀 * * ei 琛ㄧず绗?i 涓瀵熷€煎搴旂殑娈嬪樊 * * 闂鐨勫叧閿細姹傚緱 (X鈥?/span>WX) 鐭╅樀鍗冲彲锛屽彲閲囩敤 -mat glsaccum- 鍛戒护 sysuse auto, clear *-1 鑾峰緱OLS浼拌鍊?mat accum XX = weight mpg mat vecaccum yX = price weight mpg mat Xy = yX鈥?mat b = inv(XX)*Xy mat list b *-2 姹傚彇娈嬪樊涔嬪钩鏂瑰悜閲忥細e2 mkmat price, mat(y) gen cons = 1 mkmat wei len mpg cons, mat(X) // 娉ㄦ剰闄勫姞甯告暟椤?/span> mat e = y - X*b // 娈嬪樊鍚戦噺
銆€銆€
以上是关于Stata 鐭╅樀鎿嶄綔的主要内容,如果未能解决你的问题,请参考以下文章