Chapter 19

ultimate-calcs.R
########################################################################
# Project X:
# "The Ultimate Question of Life, the Universe, and Everything"
# Project Collaborators: Mr. X, Mrs. Y
#
# R Script "ultimate-calcs"
# by: F Heiss
# Date of this version: February 08, 2016
########################################################################

# The main calculation using the method "square root"
# (http://mathworld.wolfram.com/SquareRoot.html)
sqrt(1764)
projecty-master.R
########################################################################
# Bachelor Thesis Mr. Z
# "Best Practice in Using R Scripts"
#
# R Script "master"
# Date of this version: 2016-02-08
########################################################################

# Some preparations:
setwd(~/bscthesis/r)
rm(list = ls())

# Call R scripts
source("data.R"        ,echo=TRUE,max=1000) # Data import and cleaning
source("descriptives.R",echo=TRUE,max=1000) # Descriptive statistics
source("estimation.R"  ,echo=TRUE,max=1000) # Estimation of model 
source("results.R"     ,echo=TRUE,max=1000) # Tables and Figures
LaTeXwithR.R
library(stargazer);library(xtable)
data(gpa1, package='wooldridge')


# Number of obs.
sink("numb-n.txt"); cat(nrow(gpa1)); sink()
# generate frequency table in file "tab-gender.txt"
gender <- factor(gpa1$male,labels=c("female","male"))
sink("tab-gender.txt")
xtable( table(gender) )
sink()

# calculate OLS results
res1 <- lm(colGPA ~ hsGPA      , data=gpa1)
res2 <- lm(colGPA ~         ACT, data=gpa1)
res3 <- lm(colGPA ~ hsGPA + ACT, data=gpa1)

# write regression table to file "tab-regr.txt"
sink("tab-regr.txt")
stargazer(res1,res2,res3, keep.stat=c("n","rsq"),
              type="latex",title="Regression Results",label="t:reg")
sink()

# b1 hat
sink("numb-b1.txt"); cat(round(coef(res1)[2],3)); sink()

# Generate graph as PDF file
pdf(file = "regr-graph.pdf", width = 3, height = 2)
par(mar=c(2,2,1,1))
plot(gpa1$hsGPA, gpa1$colGPA)
abline(res1)
dev.off()
ultimate-calcs.py
########################################################################
# Project X:
# "The Ultimate Question of Life, the Universe, and Everything"
# Project Collaborators: Mr. X, Mrs. Y
#
# Python Script "ultimate-calcs"
# by: F Heiss
# Date of this version: February 18, 2019
########################################################################
# external modules:
import numpy as np
import datetime as dt

# create a time stamp:
ts = dt.datetime.now()

# print to logfile.txt ('w' resets the logfile before writing output)
# in the provided path (make sure that the folder structure
# you may provide already exists):
print(f'This is a log file from: \n{ts}\n',
      file=open('Pyout/19/logfile.txt', 'w'))

# the first calculation using the function "square root" from numpy:
result1 = np.sqrt(1764)

# print to logfile.txt but with keeping the previous results ('a'):
print(f'result1: {result1}\n',
      file=open('Pyout/19/logfile.txt', 'a'))

# the second calculation reverses the first one:
result2 = result1 ** 2

# print to logfile.txt but with keeping the previous results ('a'):
print(f'result2: {result2}',
      file=open('Pyout/19/logfile.txt', 'a'))
ultimate-calcs2.py
# external modules:
import numpy as np
import datetime as dt
import sys

# make sure that the folder structure you may provide already exists:
sys.stdout = open('Pyout/19/logfile2.txt', 'w')

# create a time stamp:
ts = dt.datetime.now()

# print to logfile2.txt:
print(f'This is a log file from: \n{ts}\n')

# the first calculation using the function "square root" from numpy:
result1 = np.sqrt(1764)

# print to logfile2.txt:
print(f'result1: {result1}\n')

# the second calculation reverses the first one:
result2 = result1 ** 2

# print to logfile2.txt:
print(f'result2: {result2}')
ultimate-calcs.jl
########################################################################
# Project X:
# "The Ultimate Question of Life, the Universe, and Everything"
# Project Collaborators: Mr. H, Mr. B
#
# Julia Script "ultimate-calcs"
# by: F Heiss
# Date of this version: December 1, 2022
########################################################################
# load packages:
using Dates

# create a time stamp:
ts = now()

# print to logfile.txt (write=true resets the logfile before writing output)
# in the provided path (make sure that the folder structure
# you may provide already exists):
open("Jlout/19/logfile.txt", write=true) do io
    println(io, "This is a log file from: \n $ts\n")
end

# the first calculation using the square root function:
result1 = sqrt(1764)
# print to logfile.txt but with keeping the previous results (append=true):
open("Jlout/19/logfile.txt", append=true) do io
    println(io, "result1: $result1\n")
end

# the second calculation reverses the first one:
result2 = result1^2
# print to logfile.txt but with keeping the previous results (append=true):
open("Jlout/19/logfile.txt", append=true) do io
    println(io, "result2: $result2")
end
ultimate-calcs2.jl
# load packages:
using Dates

# create a time stamp:
ts = now()

# print to logfile2.txt (write=true resets the logfile before writing output)
# in the provided path (make sure that the folder structure
# you may provide already exists):
open("Jlout/19/logfile2.txt", write=true) do io
    println(io, "This is a log file from: \n $ts\n")

    # the first calculation using the square root function:
    result1 = sqrt(1764)
    # print to logfile2.txt:
    println(io, "result1: $result1\n")

    # the second calculation reverses the first one:
    result2 = result1^2
    # print to logfile2.txt:
    println(io, "result2: $result2")
end