Stata是統計學專業軟體,可以很方便的對資料處理,但幾乎只能按照整行整列進行,而且每次只能載入一個矩陣(dta檔案),如果要用到多個矩陣資料進行操作或進行複雜的迴圈控制,就力不從心了。
而Matlab工業界廣泛使用的資料分析處理工具,對矩陣支援良好,除了可以像c語言一樣完成底層的操作之外,還包含很多函數庫,囊括工控、訊號處理、金融、人工智慧各個行業。雖然沒有Stata內建的統計學函數全面,但在底層操作方面具有明顯優勢。
因此,在一次協助別人完成金融資料分析時,嘗試使用Stata對資料進行預先處理,Matlab完成運算之後再由Stata完成進一步的加標籤等操作。
Stata完成資料預先處理
Stata參考資料不多,主要參考了《應用Stata做統計分析》前兩章基本操作部分,還有搜尋。
待處理的資料為大盤行情與個股行情,從資料庫下載到的資料為xls格式。使用
import excel *.xlsimport excel *.xls,clear#載入另一個檔案
可以載入excel檔案,當然在載入之前最好改變workspace到所在目錄。
資料中一列為“1991-01-01”格式的日期資料,可以使用
generate dated=date(B,"YMD")
把日期同意換算成消逝日期,即相對於1960年元旦的天數。
而day、month、year等函數可以由消逝天數計算出當天的年月日。
匯入的資料均為字串類型,要把字串轉換為數字,要使用
encode x,gen(y)
為了完成把資料匯入Matlab進行處理,可以建立一個xlsx(Office2007格式)檔案,Stata中輸入browse查看資料,將資料複製進入Excel,儲存。而在Matlab中點擊匯入資料完成匯入,匯入之後別忘了重新命名。可以匯入多個檔案。
如果多個dta檔案具有相同的列結構,可以採用下列命令合并:
use data1append using data2save data12
要刪除本來為表格行列名的行,使用:
drop if dated==.
如果有些值是錯誤的,可以標記為error:
gen iserror="error" if acumrate==65535
如果要儲存為Excel能夠開啟的的xml格式,使用:
xmlsave filename,doctype(excel)
drop,replace,ls,rename命令也經常用到。
Matlab完成資料處理
Matlab中m檔案編程可以完成很多操作,這次新學習到一個命令
try...catch...end
這樣如果對多個資料進行處理,單個資料由於不完整等原因出錯時可以繼續運行下去。
這次操作,用到了polyfit和polyval命令,分別是進行單變數多項式擬合和估計。
程式碼如下:
%company is sorted, need to change to no-sort%ind=563 company=[681,17902,563] is wrong,stock price's date may out of%rangeload('jr.mat')[m,n]=size(company);acum_index=zeros(m,1);errcompany=zeros(1,3);%error for which can't find stock priceerrind=1;for ind=1:m arit=zeros(40,1); com_id=company(ind,1);%company code com_date=company(ind,2);%company board date index_date_ind=find(ind399108(:,1)==com_date,1);%pointer to index if isempty(index_date_ind) index_date_ind=find(ind399108(:,1)==com_date+1,1); if isempty(index_date_ind) index_date_ind=find(ind399108(:,1)==com_date+2,1); end end % index 40befor 40after index_befor=ind399108(index_date_ind-40:index_date_ind-1,2); index_after=ind399108(index_date_ind:index_date_ind+39,2); %calc stock increase stock_tmp=stock(find(stock(:,1)==com_id,1,'first'):find(stock(:,1)==com_id,1,'last'),:); stock_tmp_ind=find(stock_tmp(:,2)==com_date,1); if isempty(stock_tmp_ind) stock_tmp_ind=find(stock_tmp(:,2)==com_date+1,1); if isempty(stock_tmp_ind) stock_tmp_ind=find(stock_tmp(:,2)==com_date+2,1); end end if isempty(stock_tmp_ind) errcompany(errind,:)=company(ind,:); errind=errind+1; acum_index(ind)=inf; continue end try stock_index_befor=(stock_tmp(stock_tmp_ind-40:stock_tmp_ind-1,2)-stock_tmp(stock_tmp_ind-40-1:stock_tmp_ind-1-1,2))./stock_tmp(stock_tmp_ind-40-1:stock_tmp_ind-1-1,2); stock_index_after=(stock_tmp(stock_tmp_ind:stock_tmp_ind+39,2)-stock_tmp(stock_tmp_ind-1:stock_tmp_ind+39-1,2))./stock_tmp(stock_tmp_ind-1:stock_tmp_ind+39-1,2); catch display(company(ind,:)); display(ind); acum_index(ind)=inf; continue; end %use index_befor stock_index_befor index_after stock_index_after to %calc p=polyfit(index_befor,stock_index_befor,1); arit=stock_index_after-polyval(p,index_after); acum_index(ind)=sum(arit);endoutdata=[company,acum_index];
完成處理後,使用
xlswrite('x.xls',x)
可以把數組x寫入xls檔案。
之後可以在Stata中把消逝天數轉換為日期,給變數重新命名,就完成了操作。
R語言
這次工作完成的非常吃力,主要是工具的原因,Stata無法很好的完成迴圈控制等底層操作,Matlab雖然可以完成所有操作但Matlab中的字串和日期操作非我所長,R語言作為專業統計語言,完成這些資料處理和計算應該是十分方便的。很期待R的表現。