Showing posts with label education. Show all posts
Showing posts with label education. Show all posts

2023-02-07

Using chatGPT in the teaching of R

I am currently focalising an introductory course in programming with R within my organisation. We use datacamp.com as a resource and meet online once a week to share.

This week my input was an interaction with chatGPT, see below. My impression is that AI can be quite helpful in learning a programming language. Especially if you can ask good questions. In the future, as a course organiser, the focus will be less on the content and more on being together as a learning group and sharing.

Also worth reading on the topic of chatGPT is a discussion thread on datamethods.org.


Here is the AI interaction:

I want to extract the first five characters of a string using R. How would the code look like?

You can extract the first five characters of a string in R using the substring function. Here is an > example:

string <- "Hello World"
result <- substring(string, 1, 5)
print(result)

This code will return the following output:

[1] "Hello"

Explain how vectorisation works in R!

In R, vectorization is the process of performing operations on entire arrays of data, rather than iterating over the elements one by one. This is important because vectorized operations are typically much faster than non-vectorized operations, due to the optimized underlying code.

The key to vectorization in R is that all basic data structures in R are arrays, or vectors. This means that operations performed on them are automatically vectorized. For example, if you add two vectors of the same length, the result is a new vector where each element is the sum of the corresponding elements from the input vectors.

Here is an example to illustrate this:

a <- c(1, 2, 3, 4, 5)
b <- c(5, 4, 3, 2, 1)
c <- a + b
print(c)

This code will return the following output:

[1] 6 6 6 6 6

In this example, the + operator is applied to the entire vectors a and b, producing the new vector c that contains the sums of corresponding elements.

It’s worth noting that not all operations in R are vectorized, and some operations can only be vectorized under certain conditions. For example, some operations require that all elements in a vector have the same length and structure, and some operations may return unexpected results if applied to vectors with different lengths.

Give me another example!

Sure, here’s another example that demonstrates vectorization in R:

x <- c(1, 2, 3, 4, 5)
y <- x * 2
print(y)

In this code, we create a vector x with 5 elements, and then use the * operator to multiply each element in x by 2. The result is a new vector y that contains the doubled values of the elements in x.

[1]  2  4  6  8 10

This demonstrates how vectorization allows us to perform operations on entire arrays of data, without having to loop over the elements one by one. This is a more efficient and concise way to work with data in R.

What are the most important string functions in base R?

Here are some of the most commonly used string functions in base R:

  1. nchar: returns the number of characters in a string
  2. substring: extracts a substring from a string
  3. paste: concatenates strings into a single string
  4. toupper: converts all characters in a string to uppercase
  5. tolower: converts all characters in a string to lowercase
  6. gsub: replaces all occurrences of a pattern in a string with a replacement string
  7. grep: searches for a pattern in a string and returns the matching elements
  8. grepl: similar to grep, but returns a logical vector indicating which elements match the pattern
  9. strsplit: splits a string into substrings based on a specified separator
  10. trimws: removes leading and trailing whitespace from a string.

These functions form the basis for many common string manipulation tasks in R, and are commonly used in data cleaning and preparation. It’s worth noting that there are many more string functions available in R, and many specialized string manipulation packages available in the R ecosystem.

Translate excel string functions to their corresponding R functions!

Here are some common Excel string functions and their equivalent R functions:

Excel function R function
LEFT substring
RIGHT substring
MID substring
LEN nchar
UPPER toupper
LOWER tolower
CONCATENATE paste
REPLACE gsub
SEARCH grep or grepl

Note that the R functions are not always a direct match to their Excel counterparts, but they can be used to achieve the same results. For example, substring can be used to implement the functionality of the LEFT, RIGHT, and MID functions by specifying different arguments for the start and end positions of the desired substring.

2014-03-14

2048

Last night and some hours yesterday I spent my time playing the browser game 2048. It was great fun and hard to stop. But finally I reached the goal of the game,

Screenshot of my last game 2048

and can leave it for good.

There is also a long discussion on this game on hackernews.

2013-09-03

On learning (math, in particular)

Here are some bon mots I collected from an online course on math didactics.

When you had the answer wrong, your brain grew…

… when you got the answer right, nothing happened to your brain. This aims to build a work ethic. Dave Panesku tried different starting messages at Khan Academy Videos. Messages that tried to build work ethic (“the more you work, the more you learn”) made students solve more problems, while encouraging messages (“this is hard, try again if you fail first time”) had no effect compared to the control group.

It is also about appreciating mistakes.

Convince yourself, convince a friend, convince a skeptic

Most math students and their parents have difficulties to name the topic or learning goal that is currently covered in class. Discussing different ways of seeing, different paths and strategies to tackle a problem, however, is what learning (math, in particular) is about.

Uri Treisman and his colleagues showed in their minority studies [1] that students who discussed the problems outperformed those who did not.

Where are you, where do you need to be, how to close the gap

Feedback is important. Regular peer and self assessments outperforms control groups, especially low achievers improve [2].

Grading does not provide useful feedback. Diagnostic feedback encourages the students and outperforms grades – as well as grades together with feedback [3].

Pseudo-context problems need redesign

Math problems can be fun if they are presented in an open style that allows multiple entry points.

It is more interesting to construct two rectangles given a perimeter than to find the perimeter of a given rectangle.

“Doing and undoing”, i.e. being able to reason both forwards and backwards with operations, is the central practice in algebraic thinking. For instance, first discuss several methods to solve a problem, then present expressions for new methods and discuss what the method behind the expression might be [4].

There is an web effort on makeover of dull math problems.

Can you do any number between 1 and 20 by using only 4 4s? For example, 20 = (4/4 + 4)*4.

  1. Fullilove, R. E., & Treisman, P. U. (1990). Mathematics achievement among African American undergraduates at the University of California, Berkeley: An evaluation of the Mathematics Workshop Program. Journal of Negro Education, 59 (30), 463-478.
  2. White, B., & Frederiksen, J. (1998). Inquiry, modeling and metacognition: making science accessible to all students. Cognition and Instruction, 16(1), 3-118.
  3. Butler, R. (1988). Enhancing and Undermining Intrinsic Motivation: The Effects of Task-Involving and Ego-Involving Evaluation on Interest and Performance. British Journal of Educational Psychology, 58, 1-14.
  4. Driscoll, M. (1999). Fostering Algebraic Thinking: A Guide for Teachers, Grades 6-10. Heinemann, 361 Hanover Street, Portsmouth, NH 03801-3912.

2013-02-16

Some of Excel's Finance Functions in R

Last year I took a free online class on finance by Gautam Kaul. I recommend it, although there are other classes I can not compare it to. The instructor took great efforts in motivating the concepts, structuring the material, and enable critical thinking / intuition. I believe this is an advantage of video lectures over books. Textbooks often cover a broader area and are more subtle when it comes to recommendations.
One fun excercise to me was porting the classic excel functions FV, PV, NPV, PMT and IRR to R. Partly I used the PHP class by Enrique Garcia M. You can find the R code at pastebin. By looking at the source code, you will understand how sensitive IRR to its start value is:
> source("http://pastebin.com/raw.php?i=q7tyiEmM")
> irr(c(-100, 230, -132), start=0.14)
[1] 0.09999995
> irr(c(-100, 230, -132), start=0.16)
[1] 0.1999999
I still do not understand the sign of the return values. This I have to figure out every time I use the function. If you have a memory hook for this, please leave a comment.
The class did of course not only cover the time value of money, it was also a non-rigorous introduction to bonds and perpetuities (which I found interesting, too), as well as to CAPM and portfolio theory.

2013-02-14

Reflections on a Free Online Course on Quantitative Methods in Political Sciences

Last year I watched some videos of Gary King's lectures on Advanced Quantitative Research Methodology (GOV 2001). The course teaches ongoing political scientists how to develop new approaches to research methods, data analysis, and statistical theory. The course material (videos and slides) seems to be still online, a subsequent course apparently has started end of January 2013.

I only watched some videos and did not work through the assignments. Nevertheless I learned a lot, and I am writing this post to reduce my pile of loose leafs (new year's resolution) and summarize the take-aways.

Theoretical concepts

In one of the first lessons, the goals of empirical research are stepwise partitioned until the concept of counterfactual inference appears, a new term for me. It denotes “using facts you know to learn facts you cannot know, because they do not (yet) exist” and can further differentiated into prediction, what-if analysis, and causal inference. I liked the stepwise approximation) to the concept: summarize vs. inference, descriptive inference vs. counterfactual inference.

In the course was presented a likelihood theory of inference. New to me was the likelihood axiom which states that a likelihood function L(t',y) must be proportional to the probability of the data given the hypothetical parameter and the “model world”. Proportional means here a constant that only depends on the data y, i.e. L(t',y) = k(y) P(y|t'). Likelihood is a relative measure of uncertainty, relative to the data set y. Comparisons of values of the likelihood function across data sets is meaningless. The data affects inferences only through the likelihood function.

In contrast to likelihood inference, Bayesian inference models a posterior distribution P(t'|y) which incorporates prior information P(t'), i.e. P(t'|y) = P(t') P(y|t')/P(y). To me, it seems the likelihood theory of inference is more straightforward as it is not necessary to treat prior information P(t'). I have heard that there discussions between “frequentists” and “Bayesians”, but it was new to me to hear from a third group “Likelihoodists”.

Modeling

At the beginning of the course, some model specifications with “systematic” and “stochastic” components were introduced. I like this notation, it makes very clear what goes on and where the uncertainty is.

An motivation was given of the negative binomial distribution as a compounding distribution of the Poisson and the Gamma distribution (aka Gamma mixture). The negative binomial distribution can be viewed as a Poisson distribution where the Poisson parameter is itself a random variable, distributed according to a Gamma distribution. With g(y|\lambda) as density of the Poisson distribution and h(\lambda|\phi, \sigma^2) as density of the Gamma distribution, the negative binomial distribution f arises after collapsing their joint distribution: f(y|\phi, \sigma^2) = \int_0^+\infty g(y|\lambda) h(\lambda|\phi, \sigma^2) d\lambda

There were many other modeling topics, including missing value imputation and matching as a technique for causal inference. I did not look into it, maybe later/someday.

The assignments did move very fast to simulation techniques. I did not work through them, but got interested in the subject and will work some chapters of Ripley's “Stochastic Simulation” book, when time permits.

Didactical notes

I was really impressed by the efforts Mr. King and his teaching assistants took to teach their material. Students taking the (non-free) full course prepare replication papers. The assignments involve programming. In the lectures quizzes are shown, the students vote using facebook and the result is presented two minutes later. The professor interrupted his talk once per lecture and said “Here is the question; discuss this with your neighbour for five minutes”. Very good idea.