Skip to content

Instantly share code, notes, and snippets.

View MartinMacharia's full-sized avatar

MartinMacharia

View GitHub Profile
@MartinMacharia
MartinMacharia / Mixed Models and MCMC
Last active August 16, 2017 07:26
Mixed models and lme4
Model selection codes
Model 1
Initial model specification
library(lme4)
Model1=lmer(yield.per.ha~n.per.ha*rain*soilclass+manure+seed+n.squared+(1|year)+(1|hhid)+(1|aezsmall)+(1|aez),data=isfm)
Model selection using fitLMER.fnc
library(LMERConvenienceFunctions)
optimum1=fitLMER.fnc(Model1,backfit.on="F",item=F,alpha=0.05,if.warn.not.add=TRUE,llrt=T,prune.ranefs=TRUE,p.value="upper",t.threshold=2,set.REML.FALSE=TRUE,reset.REML.TRUE=TRUE)
optimum1
Model criticism plots
Guide to Generating field book and field layouts in R
January 2014
What is R?
R is a programming language for statistical computing and graphics. It is available as free software. As a thriving open-source project, R is supported by a large community of users and developers worldwide. Whether you're using R for data management, analyzing genomic sequences, or designing field books and layouts for agronomy work, experts in every domain have made resources, applications and code available for free, online.
Why we need R in OFRA
R presents a quick way to randomize treatments and generate field books and field layouts. Beyond this R is the leading tool for statistics, data analysis, and machine learning. It is more than a statistical package; it’s a programming language, so you can create your own objects, functions, and packages.
What is a package?
Peer reviewed methods in statistics and statistical modeling from lead researchers in statistics that have been packaged into applications and can be downloaded f
@MartinMacharia
MartinMacharia / Implementing constrOptim
Last active December 14, 2021 08:40
Implementation of constrOptim in maximization of an objective function
#Appendix 2: Optimization algorithm
#Specify the function to be maximized. By default constrOptim performs minimization but the problem can be maximized by setting the fnscale to -1
LP_sol=function(X){
Y_R=5625-2897*(0.966^X[1])-2728 #Rice N response function
Y_M=3159-1191*(0.976^X[2])-1968 #Maize N response function
Y_B=1415-715*(0.950^X[3])-700 #Bean N response function
Y_FM=2100-923*(0.944^X[4])-1177 #Finger millet N response
Y_R1=5665-828*(0.871^X[5])-4837 #Rice P response function
Y_M1=4474-770*(0.898^X[6])-3704 #Maize P response function
Y_B1=1138-263*(0.848^X[7])-875 #Bean P response function
@MartinMacharia
MartinMacharia / Returns LP publication
Created September 14, 2016 11:23
Returns LP publication
# 1,000,000
Y_R=5625-2897*(0.966^120)-2728 #Rice N response function
Y_M=3159-1191*(0.976^120)-1968 #Maize N response function
Y_B=1415-715*(0.950^68)-700 #Bean N response function
Y_FM=2100-923*(0.944^61)-1177 #Finger millet N response function
Y_R1=5665-828*(0.871^30)-4837 #Rice P response function
Y_M1=4474-770*(0.898^36)-3704 #Maize P response function
Y_B1=1138-263*(0.848^22)-875 #Bean P response function
Y_FM1=2101-537*(0.798^19)-1564 #Finger millet P response function
Y_PP=2538-487*(0.758^19)-487 #Pigeon Pea P response function
@MartinMacharia
MartinMacharia / Random Forest
Last active April 17, 2017 09:03
Random Forest
# soils
# set this to the xlsx file on your computer
setwd("c:/Users/machariam/Desktop/Soil_R_project") # Change working directory
# load libraries
lapply(c("data.table", "DescTools", "stringr", "ggplot2", "readxl", "ranger", "RWeka", "Boruta", "DMwR"), require, character.only = T)
# load data
myfiles = dir(pattern = "xlsx") #These functions produce a character vector of the names of files or directories in the named directory.
myfiles #See no value in this unless it can be linked with the read_excel below
df = read_excel(myfiles, na = "") # Load the excel, still need a way to sort out the data structure and missing values
df
@MartinMacharia
MartinMacharia / Nonlinear & Inequality
Last active July 20, 2016 15:07
Linear programming
#Specify the function to be maximized. By default constrOptim performs minimization but
#the problem can be maximized by setting the fnscale to -1
LP_sol=function(X){
Y_R=5625-2897*(0.966^X[1])-2728 #Rice N response function
Y_M=3159-1191*(0.976^X[2])-1968 #Maize N response function
Y_B=1415-715*(0.950^X[3])-700 #Bean N response function
Y_FM=2100-923*(0.944^X[4])-1177 #Finger millet N response function
Y_R1=5665-828*(0.871^X[5])-4837 #Rice P response function
Y_M1=4474-770*(0.898^X[6])-3704 #Maize P response function
Y_B1=1138-263*(0.848^X[7])-875 #Bean P response function
@MartinMacharia
MartinMacharia / constrOptim
Last active October 31, 2017 05:00
constrOptim, One dimension problem
#One dimension problem
fun=function(X){
Y=5151-1735*(0.945^X[1])-3416
Py=100
R=Y*Py
Px=200
C=X[1]*Px
P=Y*Py-X[1]*Px
return(P)
#plot(X,P,type="l")
@MartinMacharia
MartinMacharia / Spillman Vs Linear response with Plateau
Last active May 3, 2016 11:46
Spillman, Linear response with Plateau, and Qadraticwith Plateau
# Spillman/ Assymptotic
#Niger, 2015
#Dosso
#Bengou
prate=c(0,7.5,15,22.5)
yield=c(826.5,1127.4, 1203.7, 1250)
#prate<- c(0,7.5,15,22.5, 30, 32.5)
#yield <- c(826.5,1035.4,1203.7,1250, 1250, 1250)
dat <- as.data.frame(cbind(prate,yield))
dat
@MartinMacharia
MartinMacharia / Regression kriging
Last active October 26, 2020 13:09
Regression kriging
#Regression kriging for maize 50 kg N
library(rgdal)
library (raster)
library (maptools)
library(rgeos)
library(dismo)
library(sp)
#load tiffs of standardized variables and PCA
@MartinMacharia
MartinMacharia / Mozambique training, getting started in R
Last active April 11, 2016 07:53
Mozambique OFRA Workshop, quick mention about R
yield=read.csv(file.choose(),header=T)
yield
#Another way of importing the data
#SmallYieldData <- read.csv("C:/Users/Administrator/Desktop/SmallYieldData.csv")
#View(SmallYieldData)
#You can also type the data in R as follows
yield=data.frame(Nrates=c(0,10,20,30,40,50,60),yield=c(2748,3162,3307,3571,3576,3544,3600))
yield
#You can view in which directory you are in R and also change the working direcory
#as follows