Introduction

Getting started

Load the dataset

We load the dataset:

Our training dataset consists of a hand-coded sample of 1102 articles (out of 2744 in our initial corpus) which related to “returned students” in the English-language press in China. (More details on the method for building the corpus here). The article were unequally distributed across ten different periodicals, among which the British North-China Herald, the American China Weekly Review and the Chinese China Press (Dalubao) - all based in Shanghai - clearly dominated. Each of the 1102 articles was manually associated to the section under which it appeared in the periodical. We defined ten broad sections (genres). Each article can fall under one and only one class. (More details on the classification method here)

Our modeling goal is to predict the genre of each article (i.e. the section under which it appeared) and its source (i.e. the periodical in which it appeared).

Explore the data

How were the articles distributed across periodicals and newspaper sections?

Periodicals

rs_genre %>%
  count(Source, sort = TRUE)

As already said, three Shanghai-based periodicals dominated the corpus: the British North-China Herald (409 articles), the Chinese China Press (Dalubao) (290) and the American China Weekly Review (238).

For better legibility, we recode the genres as follows:

rs_genre <- rs_genre %>%
  mutate(Genre_code = fct_recode(Genre,
    "0"    = "Advertisement",
    "1"      = "Authored article",
    "2" = "Correspondence (Abroad)",
    "3" = "Correspondence (China)",
    "4"        = "Editorial",
    "5"      = "People & Events",
    "6" = "Local & General News",
    "7"                 = "Readers Letter",
    "8"                 = "Special Pages",
    "9"                 = "Other"
  )) 

rs_genre

Genres (newspaper section)

rs_genre %>%
  count(Genre_code, Genre, sort = TRUE)

The classes are clearly imbalanced. The sections “Correspondence (China)” and “People & Events” amounted for almost half of the sample. “Authored article”, “Local & General News” and “Advertisements” altogether represented about one third. How does this vary across periodicals ?

Genre by periodical

rs_genre %>%
  mutate(
    Source = fct_inorder(Source),
    Genre = fct_lump_n(Genre, 10)
  ) %>%
  count(Source, Genre_code, Genre) %>%
  mutate(Genre_code = reorder_within(Genre_code, n, Source)) %>%
  ggplot(aes(n, Genre_code, fill = Source)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~Source, scales = "free", ncol = 2) +
  scale_y_reordered() +
  labs(title = "Genre of articles in the 'returned students' press corpus", 
       subtitle = "Distribution by periodical",
       x = "Number of articles",
    y = "Section")

Advertisements dominated in two periodicals, the China Press and Peking Gazette. Local & General News prevailed in most non-Shanghai papers (Peking Daily News, Peking Leaders, Canton Times). This section also featured prominently in the Shanghai Times and The China Press. The two top sections “Correspondence (China)” and “People & Events” dominated in the two major periodicals - the British North-China Herald and the American China Weekly Review. Their statistical importance largely accounts for the prevalence of the two sections in the entire corpus. The missionary journal The Chinese Recorder presented a different profile, privileging opinion articles (“Authored article”) upon all other genres.

Let’s create a dataset for our modeling question, and look at a few example lines.

Preprocessing

In order to simplify, we retain only the three most prominent periodicals and lump together the less important ones:

rs_genre_featured <- rs_genre %>%
  mutate(Source_fct = fct_lump(Source, n = 3)) %>% 
           select(Source_fct, Genre_code, Genre, text = Text)


rs_genre_featured %>%
  count(Source_fct, sort = TRUE) 

Let’s have a look of a few example lines from the “Local & General News” section:

rs_genre_featured %>%
  filter(Genre == "People & Events") %>%
  sample_n(5) %>%
  pull(text)
## [1] "  WHO S WHO IN CHINA Hsieh Chien Government official native of Yungchang Szechwan born at Kweiyang Kweichow 1884; first studied in Hupeh and later at Shanghai participated in revolutionary activities against the Manchu government because of persecution by the Manchus he took refuge in Japan where he studied law at the Tokyo Imperial University and graduated with LL degree in 1909 returning to China he became teacher at the High Police School in Peking and was conferred the degree of LL by the Board of Education after having passed the Returned Students ex amination in 1910, he served in the Supreme Court and later went to Hupeh where he law school and organised the Hankow District Procuratorate with himself serving as Procurator soon after he was transferred to bo judge in the Criminal Division of the Hupeh High Court and Chief Procurator of the Wuchang Disrtict Procurator- ate following the occupation of Wuchang and Hankow by the revolutionary forces in 1911, he went to' Shanghai to participate in the revolution there while in Shanghai he was appointed judge of the Judiciary' Bureau and org -m- ised the Shanghai District Court with himself serving as President he also initiated the organization of the Chinese Bar Association after practising law for one year he left Shang hai in 1914 for Hunch where he was Commissioned to organise the branch court of the Hupeh High Court in Hsiangvang with himself serving as chief judge between the years of 1915 and 1924 he served as magistrate of various districts in Hupeh and secretary and counsellor to the Civil Governor of Chekiang in 1928 he was appointed senior secretary of the National Government in Nanking and was also invited to serve as member of the Examination and Selection Committee of the Examination Yuan in 1932, he was appointed concurrently chief secretary ot the Commission for Disciplinary Punishment of Public Functionaries by the National Government 'in recent years he has also held the concurrent posts as secretary ot the 1'irst People Congress mem ber of the Examination Committee of the irst and Third Civil Service Examinations delegate to the 4th National Congress of Kuoinintang appointed Administrative icc- of Justice in December of 1934, which post he has since held he is Buddhist follower being an executive member of several Buddhist organ izations address Ministrv of Justice Nanking -t- gBK 57jC BelfvF IvV ii tji mm JZtJh tit mmm ilM 'i Miff ill JiTr Wl fix Hi Yi I'fl ft -J-- Mmfr mnm fritted M'-V' 'M fl- 1$ ft -il -L j4 If SI c-i ft -Vi- I J ft ft mmk HI -c- RK Hsieh Chien"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
## [2] "  HERE THERE Shanghai Feb 27. The arrival of interesting visitors and the leave-takings of residents are providing the incentives for most of Shanghai's social activity just now There were almost every minute of the day and night this past week for The Ranee of Sarawak who sailed yesterday in the ss Empress of Japan Now Shanghai is involved in series of bon voyage affairs for Sir John and Lady Bre- nan Dr Anne Walter Fearn and Mr and Mrs Fullam all ofwhom are sailing away next month Sir John and Lady Brenan bade farewell to their friends in the con sular body at cocktails in Con General compound last evening Dr Fearn who is leaving in ss President Hoover on March 9 will entertain to dinner on March 2 and at large cocktail gathering on March 0. The Fullams also are leaving on March 9. They are giving farewell cocktail party on the same evening as is Dr Fearn Final Good-Bye Mr and Mrs Verne Clair Mr and Mrs Porter and Mr and Mrs Fritchman are among those entertaining for the Fullams before their departure Members of the British Women's Association of which Lady Brenan is Honorary President will make their final farewells to her at large re- in Headquarters on next Thursday afternoon This pro mises to be one of the outstanding events of the season so far as British women in Shanghai are concerned Another popular resident who is leaving soon is Mrs Corfleld who is sailing for Home on March 3G. She will spend month in Cey lon en route Mrs Lee wife of Consul and deputy registrar of com- panics is another who will be much missed when she leaves by the Carthage on March 10 for Home Hail and Farewell Mr and Mrs Joseph Hicking- botham jnr of Piedmont California arrived here in the President Hoover and were entertained by Mr and Mrs Paui Bordwell and Mr and Mrs Steen Mrs Hicking- botham and his bride the former Miss Diana Dollar whose father heads the Dollar Steamship Company are making honeymoon trip around the world Mr and Mrs White who 'were married in Hongkong on Feb ruary 15 arrived on Thursday in the 'Empress of Japan just in time for the Paper Hunt Race They were accompanied by Mr White who accompanied his nephew south tor the races Farewell parties are just beginning for Mr and Mrs Paul Barentzen who are going on leave in April Tile first one was given last week end when Mr and Mrs Turner gave sukiyaki party for them Mrs Alice Tisdale Hobart the author of Oil For The Lamps of China Yang and Yin and numer- ous other novels is returning next month for her first visit here in two years Already Mrs Sawyer has arranged tea of welcome which will be given on March 11. Mr and Mrs Julius Wadsworth of the American Consulate-General sail ed yesterday in the ss Gneisenau for India Europe and America They spent hectic week or so before their departure trying to say bood-bye to their scores of Shanghai friends An Exotic Affair One of the loveliest parties given for them and also one of the love liest and most exotic affairs seen in Shanghai for some time was the din ner which Mr and Mrs David Mc- Lorn gave in their residence in Route Siccawei last Saturday night Their home charmingly arranged and de- in itself formed perfect setting for the dinner dancing and entertainment which carried out the gypsy motif There were half hundred guests there to enjoy their party The floor was cleared for dancing and small tables were set around the edge After the buffet dinner was served gypsy entertainers sang and danced The Ranee's Visit The Ranee of Sarawak who spent from Monday to Friday with her old friends Mr and Mrs Davidson was much entertained this week before she continued her journey to Hollywood and thence to England for the Coronation Mrs Davidson and many of Mrs Davidson's arranged parties and trips of various kinds so that the Ranee might see as much as possible of Shanghai and its life in short time Mr and Mrs Davidson who met the Ranee who is the wife of Sir Charles Vyner Brooke the world s only while Rajah in Kuala Lumpur 12 years ago gave dinner tor her on Monday night and large cocktail party on Thursday the latter occa sion being in honour of the Ranee's birthday anniversary On Tuesday Mrs Ford gave sukiyaki tiffin for the Ranee and that evening Mr and Mrs Norman Macgregor en to dinner the other guests including Mr and Mrs Davidson Brigadier and Mrs Telfer- Smollett Mr and Mrs Master Miss Winifred Mann Mr Kearton and Mr Peter Strickland The Ranee who before her marriage to the white Rajah 25 years ago was The Hon Sylvia Brett is well- known writer and it is to supervise her scenario on the life of Sir James Brooke the first white Rajah that she is going to Hollywood Also learning first hand about Shanghai's famed hospitality were Mr and Mrs Spencer Penrose of Colorado Springs who also sailed Friday in the ss Empress of Japan Their destination is Honolulu En for them during their visit here were Mr Grant Dr and Mrs Gardiner Mr and Mrs Steen and Mr and Mrs Paul Bordwell One of the month s largest cocktail parties attended by the world and his wife was given by Commander and Mme Fabre of the French Police on Tuesday night The French Police Club was the setting On Tuesday evening members of the British Returned Students Union and many of their British friends were the guests of Mr and Mrs King at very enjoyable special Club Evening given by the hosts in conjunction with the ROYAL VISITOR The Duchess of Gloucester is photographed here with the little occupants the Sixpence Day Nursery when she recently opened the Queen Mary Nursery and Social Work Settlement in Bow Road Poplar"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
## [3] "  Who's Who in China Tseng new director of the Cadastral Administration is native of Fukicn Province He is only thirty-nine years of age He was favorite student of Yen Fuh and Woe well-known Chinese scholars and Mr Tseng graduated in Nanyang College Shanghai in 1901 where he obtained the highest scholarship After graduation he was sent to Hngland as government student in the same year Upon arrival in Hngland he joined King's College where he stayed until '97- Mr 'Tseng went to Cambridge Pembroke College 111 1907 where he was popular with the students and prom inent in rowing circles His special subject of study was political economy At the end of the year he graduated After his graduation he returned to China and joined the Ministry of Foreign Affairs and remained there until 9'7- During the first revolution in '9 he was secretary for foreign intercourse to Marshal Titan Chi-jui who was then Commander-in-Chief at Hankow In the second revolution he served the same capacity under General Tuan Chih-kuei in Kittkiang In 1 9 he was appointed the Chinese C'onsiil-G eneral to Australia where he remained until China declared war against Germany in '91 7, when he returned home to offer his service to Marshal 'Tuan and to arrange for the dispatch of Chinese soldiers to Upon his return to Peking he found to his great disappointment that it impossible to dispatch Chinese troops to As he was already in China he was obliged to accept an appointment from the government as Commissioner for Foreign Affairs at Shanghai More disappointment was him for Marshal Titan's cabinet fell three days after his appointment to the new office and he could not proceed to Shanghai to assume office on account of political reasons He was consequently transferred to less important but more remunerative position He be came Salt Commissioner in Kirin and Heilttngkiang In 1918 he accompanied General Hsu Shu-cheng to Japan to attend the Japanese grand manoeuvres in the capacity of Chief Secretary Since then his name has always been mentioned the government tried to find an official to be Chinese Minister to Tokyo Mr Tseng's recent appointment as Director of the Cadastral Administration came as surprise to many Chinese politicians for ever since the death of General Tsai Ao this Administration remained closed It is how ever not clear the government intends to under take this difficult work at the present juncture or merely to keep Mr Tseng in the Capital recently returned student from America who is devoting himself to industrial and educational works in his native city is Chang Chien Junior His Excellency Chang Chien -known pioneer of Chinese industry educator and statesman is his father Bom in Chanhtcheng he received his early education in grammar school Nantungchoiv At the age of '7 he joined Tsintao University In '9'7 he to America specializing in commercial science ami graduated from Arnold Business ing the degree of He is good Chinese scholar conversant Chinese poems good tennis player en- Mr Tseng Director of the Cadastral Administration Mr Chang Chien Junior and popular Imbued with the spirit of Americanism Mr Chang has set himself an example for Chinese young mo in society and in family If you go to his residence you will find there real pleasant style family life Mr Chang has been elected member of the Special Committee of the Chamber of Commerce Nanlungchozv also chairman of Nantungchow College Faculty Union president of the Administrative Board of Nantung In dustrial Entei prises president of the Iiwei Hai Industrial Bank and Secretary-in-chief of the Grand Canal Improve ment Board Kiangsit He is also editor-in-chief of The Nantung Times one of the most popular papers in Nanlungchozv The aim of this paper is to introduce ew ideas to the com munity with view to improving the social conditions IVhile in America he was much interested in the question of foreign trade After his return he began to organize and Lace works at Nantungchozv with branch offices at Shanghai and New York City The latest report from New York City shows very good return There are two new schemes which Mr Chang has conceived in order to push the industrial and educational development of One is the organization of cotton mill and the other is the establishment of Nantung University An order for 30,000 spindles has been placed with an American firm at Shanghai 25,00 of 'which for manufacturing yarn of 10 to counts and the other 5,000 for 40's up An order for weaving machinery is also under discussion As soon as the machinery is at hand the cotton mill will start its operation Naiitiingchow has been known os an industrial city in central China Mr Chang is now work ing for its expansion At present there arc in Nantungchow several colleges the Commercial College the Textile College the Medical College and the College of Agriculture Mr Chang plan is to reorganise the present educational system-- to combine all these Colleges into single institution under the name of Nantung University with the expansion of the Nantung Normal school into Teacher's College and the establishment of other technical colleges in addition This scheme is not so hard as it seems to be because there have been colleges in existence As director-general of the Grand Canal Improve ment Board and public-minded industrial promoter and social worker II Li Chang Chien is overloaded with official duty as well as those works of public interest It is his son has been so helpful to him in his various industrial enterprises To the world Nantungchow has been known as model city in China The hope for the future of Nantungchow lies in the person of Mr Chang who is now looked upon by every body as worthy successor to his father"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
## [4] "  Men and Events The Chinese in Panama have published paper The Republic in both Chinese and Spanish special number of the Echo de Chine was pub lished for the National Holiday of France on July 14. Lin professor in the Forestry Depart ment of Nanking University Nanking is visiting in Shanghai Sailor of St John's University Shanghai departed for America on Saturday July 12, per Empress of Russia Huntly of the Shanghai College Shanghai departed for America on his vacation on Saturday July 12, per Empress of Russia Ashton and Shanahan have recently arrived in Shanghai to join the staff of the Whit ney Company tea exporters with offices at No 119 Szechuen Road Shanghai Clark of the United States Consulate departed 011 Sunday July 13, per Mishimi A4aru for Liverpool Mr Clark will visit England and will then go to the United States Thompson of the Shanghai office of the Standard Oil Company of New York is planning to depart for America early in August on leave He will be accompanied by Mrs Thompson It was announced by the Commercial Pacific Cable Company on July 11 that urgent messages will be received for transmission between China and North America at three times the ordinary rate William Lee president of the Board of Com merce of Augusta Georgia is in Shanghai on general trip throughout the Orient He is vice- president and general manager of the Southern Press Cloth Manufacturing Company of Augusta It has been announced that Bidwell has severed connections with the Shanghai branch of the Thane Co and is no longer manager ot the company Mr Bidwell has opened offices under the firm name of VV Bidwell Co According to recent daily newspaper at Dayton Ohio Toy Gun former director of air service of the Chinese army and member of special commission appointed by the government purchased number of De Havilland airplanes equipped with Liberty motors at Dayton and negotiated with another company for commercial- machines to be shipped immediately to China Miller has been appointed as manager of the Shanghai branch of the Philippine National Bank at No The Build Mr Miller was formerly manager of the Cebu branch of the Philippine National Bank Reilly former manager of the Shanghai branch will act as manager of the Park Union Banking Corporation which will open offices in various points in the East transport the Liverpool Maru chartered by the Chezcho-Slovak goverment carrying invalid soldiers arrived in Shanghai from Vladivostock on Tuesday July 15, on the way to Trieste The men 011 board are being demobilized some being over age while the remainder have tuberculosis and have been injured by shell shock Colonel Kosvosky is in command of the soldiers and they will be in Shanghai about ten days There are 22 Czech officers on board The outfitting of the ship will be completed here before proceeding to Trieste Representatives of the Pacific Mail Steamship Com pany from Manila Hongkong Singapore Yokohama and Kobe have arrived in 'Shanghai for conference of the Pacific Mail Steamship Company in session this week Johnstone is the agent for the Shanghai office Discussions of the freight and passenger rates in the tar East will be the genera topic Morton is general agent for the Orient Campbell general agent for Japan Sheppard agent at Hongkong Palmer agent at Singapore and Hunt agent at Kobe Fred Van Buskirk of Chicago representative of the following well-known American firms is in Shanghai for the purpose of making business connec tions Farley Candy Co of Chicago Selz-Schwab 5c Company manufacturers of Shoes Chicago Alfred Decker Cohn manufacturers of Society Brand clothing Chicago Rosenwald Wei manufacturers of palm beach cloth rubber sheeting for hospitals etc Chicago Mr Van Buskirk will be in Shanghai for se veral weeks and may be reached in care of the American Express Company series of nine games be played between baseball teams representing the 4th Philippine Infantry 15th Infantry of Tientsin United States Navy uul Shanghai will begin at the Race Course on Saturday July 19. The proceeds will go to the Siberian Relief Fund of the British Women's Work Association and the Shanghai American School Double headers will be played on Saturday and Sunday and single games will be played each afternoon starting at 5 o clock Season tickets for the series will be sold at the various clubs and hotels for Mex 10 Season tickets for ladies sailors and soldiers will cost Mex Among those who departed for America on Satur day July 12, per Empress of Russia were the follow ing passengers including Signor Carlo Garbasso former Italian Minister to Peking who goes to join the new Italian Cabinet William Potter formerly American Ambassador to Italy Lieut -Col Baldwin Military Attache to the American Embassy at Tokio Harold Dollar and family Mr and Mrs Jackson Wilton British Consul at Hankow Captain Marriott commander of the British gunboat Kinsha Keegan Crawford Mr and Mrs Eric Moller Hayley Morriss and Kadoorie Pinneo who is returning to Seattle after tour of the Far East in the interests of the Admiral Line also left 011 the Empress Mr Pinneo established offices throughout the Far East for his com pany The Shanghai-Hangchow-Ningpo Railway an nounce the running of night express passenger train each way between Shanghai and Hangchow on and from August 1 next The Down express will leave Shanghai North at 7.15 and Shanghai South at 7.20 reaching Hangchow at 1 1.37 and Zahkou at 12 midnight The Up train is timed to depart from Zahkou at 5.50 Hangchow at 6.15 and will arrive at Shanghai North at 10.45 P-m and Shanghai South at 10.40 Both trains will form connections with the Shanghai-Nanking Railway main line service The management is determined to improve the facilities offered to the public and we understand that the managing director is considering recommendation of the Traffic Department to purchase motor boats or steam launches so that the railway may put on an improved service to and from Mokanshan and obtain full control of the boat journey The American Company Shanghai Volunteer Corps has just issued Booklet of Information of some seventy-five pages The booklet which was compiled by 1.. Boynton and Hvkes contains historical sketch regarding the Company department entitled What the American Company Does instructions 011 how to join the Company regulations for the government of the Company mobilization manual information regarding the various trophies and most valuable Shanghai street director said to he the first ever published in this form The booklet sells for Mex to the members of the Company The American Company was organized 011 January 5, 1905, following the xed Court Riots of December 18, J905 when according to the new booklet there were 110 American warships in the harbor and the American Consulate had to be guarded by two prisoners serving sentences in the Consular fail and by Japanese sailor who had been loaned by the Japanese Consulate Numerically the American Com pany is now the strongest unit in the Corps On Friday July 11, society known as the Promotion of National Education was formed by representatives from thirteen various organizations held at the Chinese in Shanghai The purpose of the society is to help the illiterate Chinese who are employees in factories and shops Lecturers will nive free courses and hold special meetings on subjects of general knowledge sanitary methods and civics The officers elected for the year are Dr Lee presi dent Shen Hsin-ching vice-president Jui Hsian-yin secretary and Chen Chung-shen treasurer The organizations forming the Society are the following Kiangsu Educational Association the Vocational Educational Association of China the Educational Society of the Shanghai City the Shanghai Bureau for the Encouragement of Education the World Chinese Students' Federation the Western Returned Students' Union the Shanghai Students' Union the China Con Committee the National Committee the Shanghai the Shanghai and the National Committee"
## [5] "  Who's Who in China Tsao Ju-ling Minister of Communications and Concurrent Minister of Finance who has recently concluded number of important loans with Japanese financiers through Nishihara the unofficial representative of Japan who returned to Japan on the Sth instant presumably to work against the new American financial policy for China Born at Shanghai Minister sao ts now only 43 years of age and is thus in the prime of manhood Since his return from Japan where he received his education thirteen years ago he has advanced from one success to another He is considered persona grata with the Japanese Government Before the Boxer rising Mr Tsao passed com- ve examination ami reus sent by the Government to Japan to pursue higher education Upon his arrival at the Islam Empire he learned the rudiments of the Japan ese ian- after which he entered the Imperial Uni versity in Tokio For several years he studied law in that well-known institution of learning anil upen his graduation he returned to China in 'Qoj hen the Government was holding an examination of all the returned students from Europe America and Japan This examination was taken by Mr sao and successfully passed He was awarded the degree of Doctor of Law After iie boxer rising the Government 'was compel led to see the necessity of reforming its civil system and introducing modern methods and consequently created the Ministry of Commerce for the development of commerce and industry Mr Fsao joined that mis 1 ry as ti sectional chief At the same time the Peking u' eminent University was founded of which Mr sao was appointed lecturer on law In 1907 Mr Tsao was transferred from the Ministry of Commerce to the Ministry of Foreign -Iffairs Hezvas concurrently appointed by the Imperial Court member of the Bureau for the drafting of constitution for the country Since then Mr Tsao's official promotion has become more rapid When he first joined the Ministry of Foreign tr airs he occupied small position and was later giz-en the rank of councillor He participated in all Chinese- Japanese negotiations Shortly afterwards he was i ice-Minister of Foreign Affairs During the fust revolution of Mr Tsao retained this post under Shih-kai s Cabinet When the North and South composed their in in the latter part of Tang Shao-vi went to Nanking to organize the first republican cabinet -Is Mr Tsao rcas not member of the Kwomiit long or the Republican Party he was dismissed from his post However the late President Yuan Shih-kai engaged hini as his personal adviser While acting as personal adviser to the Chief Ex ecutive Mr '1 sao practised law in Peking and occupied prominent place in that profession IVhen the first republican parliament met he was elected member of the Senate representing Mongolia In Parliament he participated iti discussion of almost every subject that 'was brought up for the consideration of the 's being an eloquent speaker Then he was leading member of the Chinputang or the Progressive Party In '91S 'when the relations between China and Japan became critical President Yuan Shih-kai made use of Mr Tsao's assistance and appointed him I'ice-Miuister of Foreign Affairs Mr Tsao assisted President Yuan in the negotiations 'with Japan over the Twenty-one demands After the collapse of the monarchical movement of i uau Shih-kai Mr Tsao retired for awhile After the collapse of Chang Hsun's movement to restore the baby Emperor to the Throne in July of '9'7 Mr Tsao was appointed Minis ter of Communications and at the same time acted as Managing Director of the Bank of Communications When Wang Ko-ming Minister of Finance resigned three months ago Mr Tsao 7tu.s appointed his successor During the last three months Mr Tsao has had share in all loan transactions with Japanese bankers Mr Tsao may be appointed Minister of Foreign Affairs upon the resignation of Lu Cheng-hsiang who is now on sick leave at Peitaiho and 'who has decided 10 leave China for Switzerland as Minister If he accepts the portfolio of Foreign Affairs he will likely give up the portfolio of Finance although he will most probably retain the Ministership of Communications Tiao Ju-ling Minister of Finance and Communications"

We find a variety of articles under different labels such as “Personal Notes” from the North-China Herald, “Mainly About Chinese Personages” in The China Press, or the “Men & Events” and “Who’s Who” sections in the China Weekly Review.

What are the highest log odds words from each genre of articles?

Compute the log odds ratio using the package tidylo:

library(tidytext)
library(tidylo)

rs_genre_lo <- rs_genre_featured %>%
  unnest_tokens(word, text) %>%
  count(Genre, word) %>%
  bind_log_odds(Genre, word, n) %>%
  arrange(-log_odds_weighted)

Note: About the benefits of using a weighted log odds ratio for text analysis when the analytical question is focused on differences in frequency across sets, see Introducing tidylo

Visualize the differences between sections:

rs_genre_lo %>%
  group_by(Genre) %>%
  top_n(15) %>%
  ungroup() %>%
  mutate(word = reorder(word, log_odds_weighted)) %>%
  ggplot(aes(log_odds_weighted, word, fill = Genre)) +
  geom_col(alpha = 0.8, show.legend = FALSE) +
  facet_wrap(~Genre, scales = "free", ncol = 2) +
  labs(y = NULL)

Stop words (the, of, to) and numbers are the most frequent in the sections “Correspondence (Abroad)”, “Editorial” and “Other”. Words referring to housing and home facilities (such as bathrooms, garage, furniture, rental, heating) and business contacts (apply, phone, box, tel) are most common in advertisements (they were mostly classified advertisements, rather than commercial advertisements for consumer goods). Such words also appeared in the “Authored articles” and “People & Events” sections, which suggests that these sections might have been misclassified. The remaining genres contained a richer spectrum of words, which reflect their higher sensitiveness to special events or debates.

Except for the possibly misclassified articles we mentioned, these words make sense, but the counts are probably too low to build a good model with. Instead, let’s try using text features like the number of punctuation characters, number of pronouns, and so forth.

Text features

Extract text features usint the package textfeatures

library(textfeatures)

tf <- textfeatures(
  rs_genre_featured,
  sentiment = FALSE, word_dims = 0,
  normalize = FALSE, verbose = FALSE
)

Visualize text

tf %>%
  bind_cols(rs_genre_featured) %>%
  group_by(Genre_code, Genre) %>%
  summarise(across(starts_with("n_"), mean)) %>%
  pivot_longer(starts_with("n_"), names_to = "text_feature") %>%
  filter(value > 0.01) %>%
  mutate(text_feature = fct_reorder(text_feature, -value)) %>%
  ggplot(aes(Genre_code, value, fill = Genre)) +
  geom_col(position = "dodge", alpha = 0.8, show.legend = FALSE) +
  facet_wrap(~text_feature, scales = "free", ncol = 4) +
  labs(title = "Text features in the 'returned students' press corpus", 
       subtitle = "Distribution by genre (section)",
       x = "Section",
       y = "Mean text features per article line", 
       fill = "Genre")

The plot details the text features that characterize each section. More details about the various features here.

Let’s have a closer look of the text features extracted from our corpus:

  • Advertisements (0) contained a relatively high number of upper case words (n_caps, n_capsp), unique words (n_uq_words) compared to a low number of words (advertisements are usually very short texts with many repeated words), numbers (n_digits), punctuation (n_puncts) (not including exclamation points) and exclamation points (n_exclaims), periods and commas (n_periods, n_commas), very few “to be” words (advertisements usually avoids literary sentences and privilege nominal forms of speech), few second and third person words but a relatively high proportion of second person plural (n_secondp).
  • Authored articles (1) contrasted sharply with advertisements. They contained the highest number of characters (n_chars) and words (n_words), unique words (n_uq_words), especially lower case words (n_lowers), very few upper case words, numbers, punctuation (especially no exclamation), but a large number of prepositions, “to be” words and personal pronouns (except second person, singular).
  • Correspondence (Abroad) (2) did not stand out. They represented the average in all categories, except for the relatively high proportion of unique words, periods and person pronouns.
  • Correspondence (China) (3) presented a similar profile as its “abroad” counterpart, except for the lower number of periods and first persons, but higher number of third persons. In sum, correspondence articles in general adopted the same stylistic standards, yet with a less personal tone in China-based articles.
  • Editorial (4) represented the “lowest” of text features in almost all categories. They contained the lowest number of words and characters (shorter texts), almost no digits nor punctuation, but a relatively high number of second person plural and third person words, and lower case plurals.
  • Local & General News (5) also presented a very low profile in every category except for “to be”, second person plural and lower case plural words.
  • People & Events (6) differed by the high number of unique words, unique characters (persons and other actors’ name?), “to be” and person words (but few plural persons).
  • Readers’ Letter (7) unsurprisingly stood up by the high number of person words they contained (especially first and second). Like “Authored articles” they also contained a significant proportion of prepositions, which characterized opinion articles in general.
  • Special Pages (8) did not present any distinctive feature.
  • Other (9) articles presented a low profile in every category, except for upper case words, digits and punctuations.

Note: We shall beware of not overstating or understating these results, especially regarding punctuation, since many errors may occur during the ocerizing and segmenting process. It is not clear, for instance, whether punctuation was systematically extracted from the original text. Conversely, the full text of some articles contains punctuation signs that did not appear in the original article (for instance, we noticed that many advertisements start with quotation marks that did not appear in the original article). The two last categories contained a variety of articles, which makes it uneasy to identify any stylistic coherence.

Build the model

We can start by loading the tidymodels metapackage, and splitting our data into training and testing sets.

library(tidymodels)

set.seed(123)
rs_genre_tf_split <- initial_split(rs_genre_featured, strata = Genre_code)
rs_genre_tf_train <- training(rs_genre_tf_split)
rs_genre_tf_test <- testing(rs_genre_tf_split)

Next, we create cross-validation resamples of the training data, to evaluate our models:

set.seed(234)
rs_genre_tf_folds <- vfold_cv(rs_genre_tf_train, strata = Genre_code)
rs_genre_tf_folds

Preprocessing

Next, we preprocess our data to get it ready for modeling:

library(textrecipes)
library(themis)

rs_genre_tf_rec <- recipe(Genre_code ~ text, data = rs_genre_tf_train) %>%
  step_downsample(Genre_code) %>%
  step_textfeature(text) %>%
  step_zv(all_predictors()) %>%
  step_normalize(all_predictors())

rs_genre_tf_prep <- prep(rs_genre_tf_rec)
rs_genre_tf_prep
## Data Recipe
## 
## Inputs:
## 
##       role #variables
##    outcome          1
##  predictor          1
## 
## Training data contained 828 data points and no missing data.
## 
## Operations:
## 
## Down-sampling based on Genre_code [trained]
## Text feature extraction for text [trained]
## Zero variance filter removed 16 items [trained]
## Centering and scaling for 11 items [trained]
juice(rs_genre_tf_prep)

Let’s walk through the steps in this recipe.

  1. First, we tell the recipe() what our model is going to be (using a formula here) and what data we are using.
  2. Next, we downsample for our predictor, since the classes are imbalanced (there are many more articles that belong the “Correspondence (China)” and “People & Events” sections, as we saw earlier)
  3. We create the text features using a step from the textrecipes package.
  4. Then we remove zero-variance variables, which includes variables like the text features about URLs and hashtags in this case.
  5. Finally, we center and scale the predictors because of the specific kind of model we want to try out.

We’re mostly going to use this recipe in a workflow() so we don’t need to stress too much about whether to prep() or not. Since we are going to compute variable importance, we will need to come back to juice(rs_genre_tf_prep).

Let’s compare two different models, a random forest model and a support vector machine model. We start by creating the model specifications.

Random forest

rf_spec <- rand_forest(trees = 1000) %>%
  set_engine("ranger") %>%
  set_mode("classification")

rf_spec
## Random Forest Model Specification (classification)
## 
## Main Arguments:
##   trees = 1000
## 
## Computational engine: ranger

Support vector machine (SVM)

svm_spec <- svm_rbf(cost = 0.5) %>%
  set_engine("kernlab") %>%
  set_mode("classification")

svm_spec
## Radial Basis Function Support Vector Machine Specification (classification)
## 
## Main Arguments:
##   cost = 0.5
## 
## Computational engine: kernlab

Workflow

Next we build a tidymodels workflow(), a helper object to help manage modeling pipelines with pieces that fit together like Lego blocks. Notice that there is no model yet (Model: None).

rs_genre_tf_wf <- workflow() %>%
  add_recipe(rs_genre_tf_rec)

rs_genre_tf_wf
## ══ Workflow ════════════════════════════════════════════════════════════════════
## Preprocessor: Recipe
## Model: None
## 
## ── Preprocessor ────────────────────────────────────────────────────────────────
## 4 Recipe Steps
## 
## ● step_downsample()
## ● step_textfeature()
## ● step_zv()
## ● step_normalize()

Now we can add a model, and the fit to each of the resamples.

Fit model

First, we can fit the random forest model:

(The model failed)

Second, we can fit the support vector machine model.

set.seed(2345)

svm_rs <- rs_genre_tf_wf %>%
  add_model(svm_spec) %>%
  fit_resamples(
    resamples = rs_genre_tf_folds,
    metrics = metric_set(roc_auc, accuracy, sens, spec),
    control = control_grid(save_pred = TRUE)
  )

We have fit each of our candidate models to our resampled training set!

Evaluate model

collect_metrics(svm_rs)
conf_mat_resampled(svm_rs)

Confusion matrix for folding 1

svm_rs %>%
  collect_predictions() %>%
  filter(id == "Fold01") %>%
  conf_mat(Genre_code, .pred_class) %>%
  autoplot(type = "heatmap") +
  scale_y_discrete(labels = function(x) str_wrap(x, 20)) +
  scale_x_discrete(labels = function(x) str_wrap(x, 20))

In the next tutorial, we will develop an alternative method using the package stylo.