Pages

Wednesday, 25 February 2015

Count Occurrences of Factor in R

events <- data.frame(type = factor(c('A', 'A', 'B'), c('A','B','C')), quantity = c(1, 2, 1))

# Method 1
table(events$type)

# Method 2
xtabs(quantity~type, events)

# Method 3
aggregate(quantity~type, events, FUN=sum)

# Method 4
library(plyr)
ddply(events, .(type), summarise, quantity = sum(quantity), .drop=FALSE)


Thursday, 19 February 2015

Adjacency Matrix is an amazing Tool for Analysing Networks

I work a lot with adjacency matrix, a way of representing a relationship network. Adjacency matrix transfers a network graph into a well developed mathematical form. This opens the door to analysing networks using rigorous mathematical tools. The more I use it, the more useful I find this invention is.

1. In addition to indicating which nodes link to which nodes, with or without direction -- the most fundamental function, the matrix can conveniently reflect how important the links are (adding weight values to the corresponding elements).

2. Doing various elementary transformations of the adjacency matrix can achieve a variety of changes of the network, such as changing the order of the nodes, merging the relationships of two nodes.

3. One can assign attributes to nodes by multiplying the matrix (by row or column) with a vector storing the attributes.

Tuesday, 3 February 2015

Dropping Factor Levels in R

  • drop.levels {gdata} # Drop unused factor levels
  • droplevels {base} # The function droplevels is used to drop unused levels from a factor or, more commonly, from factors in a data frame.
  • When creating data frame or importing data into data frame (loading data with read.table, read.csv or the like), character vectors are converted to factors by default. To avoid this, set option (stringsAsFactors = FALSE)

Wednesday, 14 January 2015

Asking for Comments via Emails

Here are a few phrases you can use to ask colleagues for suggestions, or feedback, on a document you’ve prepared.

If you want to make sure that other members of your team are informed about the work you’ve been doing, you can use sentences like these to express interest in their opinions:

 I’d be grateful for any comments or suggestions you might have.
 I’d like to get some feedback on the report before we send it out.
 Please let me know if you’d like to propose any changes.

If you want to make a stronger request for feedback, making it clear that you expect a response from your readers, you can use sentences like these:

 Please take a look and let me know what you think of this draft.
 I’d like to receive detailed feedback on this version of the document.
 I’d appreciate it if each of you can take a few minutes to review and comment on the report.


Write an email to a group of colleagues, announcing that you have completed a business document for your company and that you would like them to read and review it before you submit it to your supervisor. Be sure to include the following points:

 The type of document you are sending
 A way to find the document (for example, as a file attached to your email)
 The type of comments you are asking for
 A deadline for submitting comments

NB: You can invent details such as the name of the company or product if you want, but it is not necessary to complete the task

  • Subject: Document for review

Dear Emily,

I should be glad if you could review the attached document. Can you please let me

know if you have any comments?

I would like to receive your feedback by 5:00pm next Wednesday, so that I can

make any changes you feel are necessary.

Thank you very much for your attention. I look forward to hearing from you.

Regards,

Sandra

  • Subject: Draft of quarterly sales report
Dear Colleagues,

I’ve just completed a draft version of our department’s sales report for the most recent quarter, which you will find attached as a Word document.

I’ve been asked to submit the final version of this report to our head office no later than 9:00am on Friday, and I’d very much like to hear your feedback on the draft before I send it out. If you have any comments or suggestions, I’ll need to receive them by 3:00pm Thursday so I can refer to them when I prepare the final version on Thursday afternoon.

I look forward to hearing from all of you.

Thanks and best regards,

Simon

Wednesday, 12 November 2014

Convert Matrix into Symmetric in R

  • require(Matrix):
forceSymmetric(dat, uplo="U"))
  • Assign the lower triangular part  to the upper triangular part:
dat[upper.tri(dat)] <- t(dat)[lower.tri(dat)]
  • micEcon package:
symMatrix(dat[upper.tri(dat, TRUE)], nrow=nrow(dat), byrow=TRUE)

Monday, 23 June 2014

Transforming NetLogo Simulated Networks into UCINET for Analysis

There's a program called NetLogo. It's an open-source agent-based modeling program. And it's awesome, although you have to spend some time with it. There are some example models that are automatically downloaded into a library when you download the program, and several of them are network simulators (they'll simulate a virus in a network, diffusion of a directed network, the formation of giant components, preferential attachment, etc).

There's a program called UCINET too. It's a social network analysis program which works with a freeware program called NETWORK which allows you to visualize networks. UCINET can only be run on Windows, and you can get a free trial you 60 days. With it, you can take a network generated in NetLogo and run all sorts of network diagnostics and alter your network image in whatever ways you like (I have yet to figure out if there is a way to keep track of the time variable somehow- perhaps as a link or turtle attribute, which could then be input as a node attribute in NETWORK).

They should probably be able to talk to each other, right? And then you could create rules for behavior in your NetLogo, simulate a network on those rules, analyze that network or a set of random simulations, and see if your rules are creating the types of networks you see in reality. That would be pretty cool, not? In the absence of knowing much about programming, here's a semi-labor intensive way to make this happen (doubtless R or some other program can do a lot of this already):

(1) In NetLogo:
File > Models Library > Networks > Preferential Attachment > Open
Select the Procedures tab
At the very top of Procedures, above the setup procedures, add "turtles-own [ my-list-partners; ]"
In the Main Procedures section, in the "to go" command, add "ask turtles [ set my-list-partners ([who] of link-neighbors) ]" just above the "tick" command
Select the Interface tab (and pray you don't see an error message)
Setup
Go until you have a network you like the size of
File > Export > World... > Save it somewhere
(there may be a more efficient way to create an output with just the variables of interest, but the below commands will work as well)

(2) In Excel:
Open the network's csv file you just created
Delete columns B:M
Delete rows 1:13
Delete rows below your initial ego/alter list (everything including and below "PATCHES")
Edit > Replace... > type in "[" into the "Find what:" space, leave "Replace with:" blank > Replace All > OK
type in "]" into the "Find what:" space, leave "Replace with:" blank > Replace All > OK > Close
Select column B (the commands below might depend on your version of Word)
Data > Text to Columns... > Delimited > Next > select "Space" as the delimiter and deselect "Tab" > Next > Finish
Hand select the frame with the data
Copy

(3) In UCINET 6
Data > Date editors > DL Editor
In the DL Editor, select the data tab
Select the first cell
Paste
Under "Data format:", select "Nodelist (1-mode)"
File > Save UCINET dataset > name it and save it somewhere
Close the DL editor
Visualize > NetDraw

(4) In NetDraw (which is automatically downloaded as part of UCINET)
File > Open > Ucinet dataset > Network
Select your file, which will end with ##h
Click OK

You can play with it from there. There's also an ability to upload attributes into NetDraw (like turtle color or breed), or resize nodes according to their degree, or any of a number of other options. If you're network in NetLogo has several components, UCINET can handle that too. I imagine there's some way to do a lot of the data reformatting/exporting/
importing automatically. You could also probably apply a similar alteration in code to other network models within NetLogo and use the same procedures to get it into UCINET.

Best of luck! Let me know if anything interesting comes of this/you find a more efficient way to accomplish the same thing.


Source: http://sexandstats.blogspot.ie/2011/09/network-analysis-transforming-netlogo.html

Thursday, 12 June 2014

Common Words in Spoken Dialogues

There are a number of words that are common in spoken dialogues that do not occur in written forms. This section discusses how such words should be transcribed. Where possible, we use the spelling from Quirk et al. (1985).

Filled Pauses Filled pauses are very common in natural dialogue. There seem to be two types, ones that sound like "uh" and ones that sound like "um". The endings of these words are often prolonged, thus tempting transcribers to label it is "ummm". Rather, these words should be classified as either "um" or "uh", and transcribed as such. We also include "er", which is more common in British accents. Note that the filled-pauses should never be transcribed as partial words.
umFilled pause.
uhFilled pause.
erFilled pause. More common in British English.

Acknowledgments

The following is a list of commonly occurring acknowledgments, and how they should be spelt.
okayAgreement. Speakers will often produce variants of this, such as "kay", "mkay", "umkay". All of these variants should be spelt as "okay".
uh-huhAgreement.
uh-hmAgreement.
mm-hmAgreement.
uh-uhDisagreement.
mmAgreement, stalling for time
huhRequest for clarification. Puzzlement.
hmStalling for time.
nahInformal version of "no".
nopeInformal version of "no".
a-haInterjection denoting surprise, as in "aha< I found it", rather than "uh-huh" as an acknowledgement ha Interjection, similar to "aha".
ohSurprise. ooh As in "ooh, that's gross."
yeahInformal version of "yes"
yepInformal version of "yes"

Contractions

Contractions, that are common, should be written as one word. The following is a list of common contraction endings. Note that there can often be an ambiguity as to whether the speaker was saying the words as one or as two individual words, especially since words are often blurred together. If in doubt, annotate the word pair as two separate words, spelling out the second in full.
'llfor "will"
'vefor "have"
n'tfor "not"
'refor "are"
'sfor "is"
All other contractions are left to the transcriber's discretion as to whether they should be transcribed as one word or two.
Word Pairs There are some word pairs that are so altered (in pronounciation) that they seem to be one lexical item. Such pairs can be transcribed as single words. Below, we give some common word pairs.
lemmefor "let me"
wannafor "want to"
gonnafor "going to"
gottafor "going to"



Extracted from PostScript by R. Paul McCarty 2001/07/30

Friday, 6 June 2014

On Paper Reading

Paper is not something you should read a large number of, but something you should read a number of times.

Rereading papers can be more useful than starting new papers, conditional on careful selection of papers to read.

A paper is good because it clearly tell you where it comes from and where it goes to, and can inspire readers with various interests.

Monday, 19 May 2014

Start to Change

I feel the situation starts to change from this week on. I will become more efficient at work gradually. Here I list the tasks to do for this week (20140519-20140525):

  • Submit the abstract by Monday;
  • Interview (QQ msgs) Li and Dad for setting up the ABM; 
  • Finalize the ABM of peer effects among smallholders;
  • Complete the statistical analysis on the simulation data;
  • Complete a first draft paper for peer effects (including all sections);
  • Start to do the daily recaps (new words, paper(s) read, being honesty, etc.);
  • Figure out how to import the map to NetLogo;
  • Email to a prof.

Saturday, 17 May 2014

Living beside the Sea

My current living place is 2 minutes walk to the sea. Every morning I cycle along the seaside to my work, and every evening I cycle back along the seaside. Every weekend morning I practise Tai Chi facing the sea.


I once had a strong expectation to the sea. I grew up in a very inland place. To see the sea was one of my dreams when I was a child. The dream had not been realised until I was 25 years old. Now I am so close to the sea. I can see it when I lay on the bed. I can hear and smell it on my way to work and on my way home.

Sea is often compared to the board minds or deep thoughts, which I am short of. A Chinese saying goes: "the wise delight in water, the benevolent delight in mountains". Water is flexible and profound. 

Sea is also linked to boldness and tenacity. People obtain such qualities in the fight with sea. This seems true. People living in the coastal places appear more adventurous than those dwelling in inland places. For example, the southerners in China are more likely to do business than the northerners.