Most (memory and time) efficient way to add element to vector in loop R?

Question: I can think of a few ways to add an element to a vector in a loop. For large loops time and memory become important. Which is most memory/time efficient?
This method may be bad because it copies the vector on each iteration?
vec <- c()
for (i in 1:10) {
  vec <- c(vec, i)
}

This may be optimal?
vec <- c()
for (i in 1:10) {
  vec <- append(vec, i)
}

This could get fiddly for complicated loops
vec <- c()
for (i in 1:10) {
  vec[i] <- i
}

Other methods?
Comment: Why not preallocate? All 3 of your methods are quadratic under the hood.
Comment: I think you are looking for this https://stackoverflow.com/a/2970284/5784831
Answer: When using a for loop when you know the number of iterations (hence the size of the resulting vector) ahead of time, it makes more sense to preallocate:
vec <- numeric(10)
for(i in 1:10){
  vec[i] <- i
}

That way you are not creating new vectors in each pass through the loop.
To get a feel for the time-complexity, you can experiment with the following code (which uses the microbenchmark package):
f1 <- function(n) {
  vec <- c()
  for (i in 1:n) {
    vec <- c(vec, i)
  }
  vec
}

f2 <- function(n) {
  vec <- c()
  for (i in 1:n) {
    vec <- append(vec, i)
  }
  vec
}

f3 <- function(n) {
  vec <- c()
  for (i in 1:n) {
    vec[i] <- i
  }
  vec
}

f4 <- function(n) {
  vec <- numeric(n)
  for(i in 1:n) {
    vec[i] <- i
  }
  vec
}

test <- function(n) {
  print(microbenchmark::microbenchmark(f1(n),f2(n),f3(n),f4(n)))
}

For example:
> test(10)
Unit: nanoseconds
  expr  min     lq     mean median      uq     max neval
 f1(n) 1411 1763.5 20001.06 1764.0  2117.0 1765199   100
 f2(n) 8816 8992.0 33756.65 9521.5 11108.5 2183403   100
 f3(n) 2116 2469.0 29077.37 2469.0  3879.5 2565287   100
 f4(n)  705 1058.0 20699.22 1059.0  1764.0 1906246   100
> test(100)
Unit: microseconds
  expr    min      lq      mean   median      uq     max neval
 f1(n) 21.158 22.5680  23.11462  22.9205  23.450  28.562   100
 f2(n) 95.913 98.3805 100.45053 100.1440 101.554 112.838   100
 f3(n) 19.747 20.8050  21.96511  21.1580  22.568  38.435   100
 f4(n)  5.290  5.6420   5.91758   5.6430   5.995   9.521   100
> test(1000)
Unit: microseconds
  expr      min        lq       mean    median        uq      max neval
 f1(n)  842.402  856.8590 2526.95373  875.3715 1060.4950 8562.944   100
 f2(n) 1622.390 1642.4890 2772.92502 1669.4640 1710.5440 9472.342   100
 f3(n)  185.125  192.5295  267.38981  194.9980  199.0525 7266.019   100
 f4(n)   49.367   51.1300   54.43051   52.8930   55.3610  128.000   100

For n = 10 the differences are minor, but by the time you get to n = 1000, the differences are dramatic.


Declare an object in C++ w/o creating it?

Question: Is this possible? For example if i write
Car myCar;

Then the constructor taking no arguments of Car is called. It results in an error if there is only a constructor taking arguments.
In Java I can easily declare an object and create it later using the exact same statement as above.
Comment: Would a declaration such as "extern Car myCar;" do? Later you could define "Car myCar" e.g.

struct car{
   car(){}
};

extern car c;

car c;

int main(){

}
Comment: possible duplicate of [C++: Creating an uninitialized placeholder variable rather than a default object.](http://stackoverflow.com/questions/3291644/c-creating-an-uninitialized-placeholder-variable-rather-than-a-default-object)
Answer: Well you confusing, in Java everything is a reference( or even you can think of like pointers) to objects and not the objects themself.
So you probably what to do like this:
Car* car = NULL;

and then later explicitly call a c'tor by:
car = new Car( params...);

and don't forget to call delete after you finish using car object.
delete car;

Answer: To do what you did in Java, you declare a pointer in C++:
Car* myCar;

Comment: No you don't. You could later do Car otherCar; myCar = &otherCar (also if you want to compare to java, objects live on the heap there too)
Comment: @Nils, you have no other choice. Writing in C++ isn't same as in Java. If you want C'tor not to be called you have to instantiate object on heap.
Comment: Ok.. but then it requires an empty constructor to create otherCar.. maybe the easiest thing is to just add one..
Comment: @Nils, no you later mention you wanted it to point to myVector[i].
So just do Car *myCar = &myVector[i]; or use a reference if you can:
Car &myCar = myVector[i];. You do not have to instantiate it on the heap as @Artem Barger says, a pointer to an element on the stack is fine too.
Comment: So I can do... Car *myCar; for(int i=0..) if(myVector[i].label == 3) { myCar = myVector[i];} and then I can use myCar just as an ordinary object created on the stack?
Comment: I mean I don't have to delete it, do I?
Comment: @Nils: No you don't have to delete it in this case, but probably not for the reason you think. You have to be careful to separate value and reference/pointer semantics. What you are used to in Java is reference semantics. When using Stack based variables you (usually) have value semantics. In this case here you take a reference to a stack based object (well the vector is stack based, the content may live on the heap, but this doesn't matter in this case) so you need not (even may not) delete it, but you need to be sure that the reference doesn't outlive the vector.
Answer: No, this is not possible. You could come up with some dirty hacks based on placement new which may get you close, but I doubt you are interested in them.
Why do you want to do that? Perhaps there is some clean way how to achieve that in a C++ style.
If you only want to create a variable which will point to some object later, this is what pointers are used for in C++.
auto_ptr<Car> car;

car = new Car(xxx);

Or "old way":
Car *car = NULL;

car = new Car(xxx);

delete car;

To find an item in a vector a code like this is commonly used:
std::vector <Car> cars;
Car *find = NULL;
for(std::vector<Car>::iterator car = cars.begin(); car != cars.end(); ++car )
for (int i=0; i<cars.size(); i++)
  if (Match(*car,xxx)
  {
    find=car;
    break;
  }

In many situations you would probably also prefer not to have a vector of cars, but of pointers to cars as well.
Comment: @Nils You probably want to use a vector iterator instead of indexing into the vector: `for(std::vector::iterator iter = myVector.begin(); iter != myVector.end(); ++iter)`, which lets you use `*iter` to refer to the current object. Either way, you can make a reference in the loop that corresponds to the current element, either `Car& cur = myVector[i];` or `Car& cur = *iter;`
Comment: is there any reason why you wrote ++iter instead of iter++?
Comment: Just a convention to indicate you do not need the result (and to make compiler job easier). You can find multiple topics about the difference, in this particular case it does not really matter.
Comment: Well I have to find an object from a vector of objects and so I don't have to use myVector[i] all the time, I would like to create a reference first and the loop over the list and attach the object to the reference created before the loop.
Comment: So you can use car = &carsVector[i];
Comment: @Michael: Agreed, this is a more "canonical" way, so that other containers may be used easily. Updated the code.
Comment: @Nils It avoids needing to create a temporary copy of the iterator
Answer: What you're used to in java isn't declaring the object and creating it later, but declaring the reference and creating the object to which the reference refers later.  In C++, what you're looking for is:
Car *MyCar;
MyCar = new Mycar;

Answer: You could also use pointers, eg
Car *myCar;

Later you'd write:
myCar = new Car();

Answer: As discussed, the literal way to represent java-objects in C++ is by using pointers to class objects.
Using C++ pointers with the new statement has the disadvantage, that the slower heap has to be accessed, and that deleting the object has to be done manually.
Depending on the scope, there are more C/C++ like solutions.
At global or namespace-scope, you can use the extern-specifier to declare an object, defined elsewhere:
extern C c;
<more code>
c(1,2,3);

(The extern keyword tends to be used rarely within C++).
As usual, boost offers an elegant and general solution with the library boost::optional.
This can be used everwhere as follows:
optional<int> oa; // declaration
<more code>
*oa=3; // construction


USB Serial communication between Android and computer

Question: I need to create an App that receives and transmit text data from a computer to a Android App. I found this : https://github.com/mik3y/usb-serial-for-android.
I used the example to see if I starting like this I could see a comunication between then, when I used in my mobile it appears  1 device found Vendor 1519 Product 0020 No Driver. It's like not happened the communication and not found the computer.
Here is the code:
SerialConsoleActivity.java--
package com.hoho.android.usbserial.examples;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.util.HexDump;
import com.hoho.android.usbserial.util.SerialInputOutputManager;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Monitors a single {@link UsbSerialDriver} instance, showing all data
 * received.
 *
 * @author mike wakerly (firstname.lastname@example.com)
 */
public class SerialConsoleActivity extends Activity {

    private final String TAG = SerialConsoleActivity.class.getSimpleName();

    /**
     * Driver instance, passed in statically via
     * {@link #show(Context, UsbSerialDriver)}.
     *
     *

     * This is a devious hack; it'd be cleaner to re-create the driver using
     * arguments passed in with the {@link #startActivity(Intent)} intent. We
     * can get away with it because both activities will run in the same
     * process, and this is a simple demo.
     */
    private static UsbSerialDriver sDriver = null;

    private TextView mTitleTextView;
    private TextView mDumpTextView;
    private ScrollView mScrollView;

    private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();

    private SerialInputOutputManager mSerialIoManager;

    private final SerialInputOutputManager.Listener mListener =
            new SerialInputOutputManager.Listener() {

        @Override
        public void onRunError(Exception e) {
            Log.d(TAG, "Runner stopped.");
        }

        @Override
        public void onNewData(final byte[] data) {
            SerialConsoleActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    SerialConsoleActivity.this.updateReceivedData(data);
                }
            });
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.serial_console);
        mTitleTextView = (TextView) findViewById(R.id.demoTitle);
        mDumpTextView = (TextView) findViewById(R.id.consoleText);
        mScrollView = (ScrollView) findViewById(R.id.demoScroller);
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopIoManager();
        if (sDriver != null) {
            try {
                sDriver.close();
            } catch (IOException e) {
                // Ignore.
            }
            sDriver = null;
        }
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "Resumed, sDriver=" + sDriver);
        if (sDriver == null) {
            mTitleTextView.setText("No serial device.");
        } else {
            try {
                sDriver.open();
                sDriver.setParameters(115200, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
            } catch (IOException e) {
                Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
                mTitleTextView.setText("Error opening device: " + e.getMessage());
                try {
                    sDriver.close();
                } catch (IOException e2) {
                    // Ignore.
                }
                sDriver = null;
                return;
            }
            mTitleTextView.setText("Serial device: " + sDriver.getClass().getSimpleName());
        }
        onDeviceStateChange();
    }

    private void stopIoManager() {
        if (mSerialIoManager != null) {
            Log.i(TAG, "Stopping io manager ..");
            mSerialIoManager.stop();
            mSerialIoManager = null;
        }
    }

    private void startIoManager() {
        if (sDriver != null) {
            Log.i(TAG, "Starting io manager ..");
            mSerialIoManager = new SerialInputOutputManager(sDriver, mListener);
            mExecutor.submit(mSerialIoManager);
        }
    }

    private void onDeviceStateChange() {
        stopIoManager();
        startIoManager();
    }

    private void updateReceivedData(byte[] data) {
        final String message = "Read " + data.length + " bytes: \n"
                + HexDump.dumpHexString(data) + "\n\n";
        mDumpTextView.append(message);
        mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
    }

    /**
     * Starts the activity, using the supplied driver instance.
     *
     * @param context
     * @param driver
     */
    static void show(Context context, UsbSerialDriver driver) {
        sDriver = driver;
        final Intent intent = new Intent(context, SerialConsoleActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
        context.startActivity(intent);
    }

}

DeviceListActivity--
  import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TwoLineListItem;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import com.hoho.android.usbserial.util.HexDump;

import java.util.ArrayList;
import java.util.List;

/**
 * Shows a {@link ListView} of available USB devices.
 *
 * @author mike wakerly (firstname.lastname@example.com)
 */
public class DeviceListActivity extends Activity {

    private final String TAG = DeviceListActivity.class.getSimpleName();

private UsbManager mUsbManager;
private ListView mListView;
private TextView mProgressBarTitle;
private ProgressBar mProgressBar;

private static final int MESSAGE_REFRESH = 101;
private static final long REFRESH_TIMEOUT_MILLIS = 5000;

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MESSAGE_REFRESH:
                refreshDeviceList();
                mHandler.sendEmptyMessageDelayed(MESSAGE_REFRESH, REFRESH_TIMEOUT_MILLIS);
                break;
            default:
                super.handleMessage(msg);
                break;
        }
    }

};

/** Simple container for a UsbDevice and its driver. */
private static class DeviceEntry {
    public UsbDevice device;
    public UsbSerialDriver driver;

    DeviceEntry(UsbDevice device, UsbSerialDriver driver) {
        this.device = device;
        this.driver = driver;
    }
}

private List<DeviceEntry> mEntries = new ArrayList<DeviceEntry>();
private ArrayAdapter<DeviceEntry> mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mListView = (ListView) findViewById(R.id.deviceList);
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mProgressBarTitle = (TextView) findViewById(R.id.progressBarTitle);

    mAdapter = new ArrayAdapter<DeviceEntry>(this, android.R.layout.simple_expandable_list_item_2, mEntries) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final TwoLineListItem row;
            if (convertView == null){
                final LayoutInflater inflater =
                        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = (TwoLineListItem) inflater.inflate(android.R.layout.simple_list_item_2, null);
            } else {
                row = (TwoLineListItem) convertView;
            }

            final DeviceEntry entry = mEntries.get(position);
            final String title = String.format("Vendor %s Product %s",
                    HexDump.toHexString((short) entry.device.getVendorId()),
                    HexDump.toHexString((short) entry.device.getProductId()));
            row.getText1().setText(title);

            final String subtitle = entry.driver != null ?
                    entry.driver.getClass().getSimpleName() : "No Driver";
            row.getText2().setText(subtitle);

            return row;
        }

    };
    mListView.setAdapter(mAdapter);

    mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "Pressed item " + position);
            if (position >= mEntries.size()) {
                Log.w(TAG, "Illegal position.");
                return;
            }

            final DeviceEntry entry = mEntries.get(position);
            final UsbSerialDriver driver = entry.driver;
            if (driver == null) {
                Log.d(TAG, "No driver.");
                return;
            }

            showConsoleActivity(driver);
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    mHandler.sendEmptyMessage(MESSAGE_REFRESH);
}

@Override
protected void onPause() {
    super.onPause();
    mHandler.removeMessages(MESSAGE_REFRESH);
}

private void refreshDeviceList() {
    showProgressBar();

    new AsyncTask<Void, Void, List<DeviceEntry>>() {
        @Override
        protected List<DeviceEntry> doInBackground(Void... params) {
            Log.d(TAG, "Refreshing device list ...");
            SystemClock.sleep(1000);
            final List<DeviceEntry> result = new ArrayList<DeviceEntry>();
            for (final UsbDevice device : mUsbManager.getDeviceList().values()) {
                final List<UsbSerialDriver> drivers =
                        UsbSerialProber.probeSingleDevice(mUsbManager, device);
                Log.d(TAG, "Found usb device: " + device);
                if (drivers.isEmpty()) {
                    Log.d(TAG, "  - No UsbSerialDriver available.");
                    result.add(new DeviceEntry(device, null));
                } else {
                    for (UsbSerialDriver driver : drivers) {
                        Log.d(TAG, "  + " + driver);
                        result.add(new DeviceEntry(device, driver));
                    }
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(List<DeviceEntry> result) {
            mEntries.clear();
            mEntries.addAll(result);
            mAdapter.notifyDataSetChanged();
            mProgressBarTitle.setText(
                    String.format("%s device(s) found",Integer.valueOf(mEntries.size())));
            hideProgressBar();
            Log.d(TAG, "Done refreshing, " + mEntries.size() + " entries found.");
        }

    }.execute((Void) null);
}

private void showProgressBar() {
    mProgressBar.setVisibility(View.VISIBLE);
    mProgressBarTitle.setText(R.string.refreshing);
}

private void hideProgressBar() {
    mProgressBar.setVisibility(View.INVISIBLE);
}

private void showConsoleActivity(UsbSerialDriver driver) {
    SerialConsoleActivity.show(this, driver);
}

}

Comment: Have you had some luck with this library ? I am trying to communicate wid my PC. ADB commands are working jst fine. Also, I am not able to find vendor id and product id for my PC, to use in the device_filter.xml file. Any ideas ?
Answer: Have you installed the driver your phone migh require to work? For example any Samsung smartphone will require the Key-software to be installed in order for the drivers to function properly. I am asuming you want to try USB-debugging, correct?
Comment: Thanks for the response.
I am trying now USB-debugging with mine Samsung s3, it need's another driver to work? Do you know where i can get it?
Comment: [Samsung Keys](http://www.samsung.com/uk/support/usefulsoftware/KIES/JSP) is needed to for any Samsung device, as it contains the drivers. I know the S3 requires Keys in order for the Android SDK to work properly. Download, install, reboot.
Comment: I have installed Kies in my computer, it needs to be in the phone too?
Comment: No. Just make sure that you phone is detected by your computer.
Answer: It seems that you don't have the right driver installed for your device.Try firstly to use google official tools for android development like adb and friends. If they don't work than you don't have the driver. Visit the site of the vendor of your mobile phone and download it.
Comment: I have a samsung s3, and the driver i found in samsung website it's the usb driver that i already have instaled in my computer. Do you think i need other driver to put in my mobile?
Comment: No. As I understood your phone is properly detected, try firstly something simple like completing this tutorial [link](http://developer.android.com/training/basics/firstapp/index.html).Buy the way what is the output of `adb devices`?
Comment: Yes, it is detected. I already did a few apps. And my adb is okay is okay too, it apears my phone when i do adb devices. I think the problem is the connection via USB Serial from the computer to the mobile. I checked the permissions and the app can find the device, but when it's gonna open it returns null..
Answer: It seems that you lack of USB driver.The driver should be download at http://www.ftdichip.com/Drivers/VCP.htm
according to you system version. The same problem confused me, and that driver solves this problem. Hope it works for you.


Computational Complexity - 1^n + n^4 + 4n + 4

Question: Could someone please explain what the correct answer should be:
Question = What is the complexity of 1^n + n^4 + 4n + 4?
Options = Exponential, Polynomial, Linear, Constant
My interpretation:
Answer should be exponential, because law of addition for O() is that the answer is the dominant term, which imo would be 1^n aka c^n (c = constant) aka exponential. But the answer is wrong and I don't get it :(
Answer: Well 1^n is always 1. Therefore, 1^n + n^4 + 4n + 4 can be simplified to 1 + n^4 + 4n + 4. Consequently, O(n^4).
So the correct answer is Polynomial.


Como repetir esse comandos em Python

Question: Gostaria de saber como eu faço um comando para o programa ficar repetindo esse código em uma determinada quantidade de vezes. Quero que ele repita de um em um minuto este código:
    driver.find_element_by_class_name("Ypffh").click()
    comment_input_box = driver.find_element_by_class_name("Ypffh")

    time.sleep(random.randint(2, 5))
    self.type_like_a_person(random.choice(comments), comment_input_box)

    time.sleep(random.randint(3, 5))
    driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

    time.sleep(random.randint(3, 5))
    time.sleep(3)

Já tentei while, for e outros. Quero que ele fique fazendo um loop sempre nesses comandos acima.
Comment: Cara, a única forma de repetir esse código é com os laços de repetição `while` ou `for`. Qual exatamente foi o seu problema utilizando eles ?
Comment: Eu sei que é um bot. Mas a minha pergunta é o seu script vai apenas executar esse código repetidamente a cada minuto ou ele vai realizando outra atividade enquanto a cada minuto ele repete esse código?
Answer: tudo certo. Eu já passei por uma situação semelhante a sua, porém não é exatamente igual.
O que eu poderia propor seria algo assim:
import time

init_time = time.time()

while True:
    end_time = time.time()

    if end_time - init_time < 60:
        driver.find_element_by_class_name("Ypffh").click()
        comment_input_box = driver.find_element_by_class_name("Ypffh")

        time.sleep(random.randint(2, 5))
        self.type_like_a_person(random.choice(comments), comment_input_box)

        time.sleep(random.randint(3, 5))
        driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

        time.sleep(random.randint(3, 5))
        time.sleep(3)

Porém, devo avisar que o time.sleep() pode interferir nessa contagem. Além disso, isso vai travar o fluxo da sua aplicação, no caso o Python não é assíncrono, como Node.js.
Answer: Boa noite, se a sua ideia é que ele se repita de tempo em tempo, eu acredito que melhor opção seja usar o scheduler, que é um bib python para agendar tarefas, ou uma forma que eu não acho muito interessante seria um while True e seu flag da parada poderia ser um contador, para quando ele se repetisse n vezes parasse. Mas acredito que ainda assim o melhor é o scheduler.
conte = 0

while True:

    """código aqui dentro"""
    conte += 1
    sleep(60) # repete o código a cada 60 segundos
    if conte == n:
        break

n = número de vezes

Espero que tenha ajuda em algo.
Answer: Você pode tentar criar uma função para isso da seguinte forma:

Apenas repassando: vezes representa o quanto de vezes que seu código vai rodar e intervalo o período de tempo entre cada execução.

def Repetir_meu_código(vezes, intervalo):

    for v in range(vezes):
        driver.find_element_by_class_name("Ypffh").click()
        comment_input_box = driver.find_element_by_class_name("Ypffh")

        time.sleep(random.randint(2, 5))
        self.type_like_a_person(random.choice(comments), comment_input_box)

        time.sleep(random.randint(3, 5))
        driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

        time.sleep(intervalo)

Repetir_meu_código(10, 60) # Repetir 10 vezes a cada 60 segundos

Ou simplesmente usar o loop for fora da função bem assim:

O v dentro do for representa quantas vezes o loop já rodou, sua contagem começa a partir do número 0 e vai até o valor definido em range() menos 1.
Por exemplo se o range for 10 o último valor de v será 9 pois no python a contagem começa a partir do 0. O nome v pode ser trocado por qualquer outra coisa como i por exemplo ou qualquer outro nome.

for v in range(10):
    driver.find_element_by_class_name("Ypffh").click()
    comment_input_box = driver.find_element_by_class_name("Ypffh")

    time.sleep(random.randint(2, 5))
    self.type_like_a_person(random.choice(comments), comment_input_box)

    time.sleep(random.randint(3, 5))
    driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()

    time.sleep(60)

Fazendo este processo em modelo de função seu você tem mais controle sobre seu código e pode simplesmente chamar a função novamente para ser executada em outro momento ou até mesmo importar seu script para utilizar a função em outro código.

Deste modo você importa apenas a função escolhida:
from nome_do_script import função_escolhida

Importar todas:
from nome_do_script import *

Ou simplesmente importa todas também necessitando de especificação dentro do código:
import nome_do_script

Aqui porém em todas as funções ou classes que você estiver importando precisar especificar para seu código atual a fonte da função ou classe

nome_do_script_importado.função_importada()

Note que não é preciso incluir as terminações (.py, .pyw e etc.) no import.

Como repetir esse comandos em Python


Why is 16/9 used for the minor seventh instead of 7/4?

Question: Does the 16/9 sound less dissonant than 7/4 (less uncomfortable) due to consisting of only the 3rd undertone (squared), or the 7/4 would sound "better" (I know, it's quite subjective what sounds good or better to someone), but it's a tradeoff as the 16/9 is closer to 2^(10/12)?
Comment: Might be able to get some insight from http://music.stackexchange.com/questions/16294/should-the-dominant-seventh-chord-in-just-intonation-use-95-for-its-seventh-or
Comment: Yes, I was looking at it and 9/5 wins for OP. I was thinking of 7/4 because in this case we have a prime in the numerator and a 2-power in the denominator, making it a prime order overtone instead of a compound one like either 16/9 or 9/5. Also, OP writes the harmonic seventh 7/4 undermines the leading melody. Why is it a problem? It's no more the leading tone yearning to resolve to the tonic, it's about to resolve to the major sixth.
Answer: This blog post compares the three forms of the minor seventh (with sound samples). His conclusion is that the harmonic seventh (7/4) is used as a blue note, but that it is too stable for use in a dominant seventh chord. The extra dissonance of 16/9 gives it the need to resolve that is lacking in 7/4.


Why I do not get the interaction output using aov function in R

Question: I hope you are well.
I am analyzing some data related to some bacterial phylum.
Here is the data:

I am writing the aov function as the following but I do not get the interaction value.
acioaov <- aov(Acidobacteria ~ Treatment * Zone, data = phyla)
Why it doesn't work?
The out put of that is like the following:
Df   Sum Sq   Mean Sq F value Pr(>F)
Treatment    1 0.000106 0.0001063   0.473 0.4986
Zone         6 0.003153 0.0005255   2.338 0.0656 .
Residuals   23 0.005170 0.0002248

Thank you!
Comment: Hi Armin. Just to clarify. I see that in zone u added a subscript nt or t. For example maize nt 21 and maize t 21 are these the same strain?
Comment: If i get you correct, labelling for example NT_Z1 and T_Z1 as maize_Z1 is correct. They are two different strains / experiments / observations. In your model, you want to account for the effect of zone and treatment on acidobacteria. so the model in my solution is correct
Comment: Hi @StupidWolf Thanks for your comment. I added the data.
Comment: Hi @StupidWolf. NT means Not Treated and T means Treated. That is not 21. That is Z1 and "Z" indicates zone. If it's not clear let me know, please.
Comment: I see, hey the reason your anova did not work, is because it does not recognize maize_nt_21 and maize_t_21 as the same zone category so as to say. So what you need to do is to change the way it's named and redo the anova
Comment: @StupidWolf, Thank you very much for your help and your time. Yes it worked.
Just one more question:
For example, NT_Z1 and T_Z1 are belong to the same zone but there are for two different strain. Can I follow based on your solution? I mean as you said, I removed NT and T from all the Zone column but wouldn't make a problem?  On the other hand, I don't have NT_Z1 and T_Z1 for one strain; it is for two different strain.
Answer: If the categories of one dependent variable is associated with the categories of the other dependent variable, you'll not get the interaction.. For example, if a particular type of treatment is available in only one zone, there is no interaction between the zone and the treatment.
Since you have not provided the information about the categories, I couldn't find any other answer for your question.
Answer: I cannot really type your data in, but let's suppose your data.frame phyla is something like this:
# you don't need to run
phyla = data.frame(Treatment=rep(c("NT","T"),each=4),
Zone=rep(c("maize_NT_Z1","maize_NT_Z2","maize_T_Z1","maize_T_Z2"),each=2)
)
phyla$Acidobacteria = rnorm(nrow(phyla))

summary(aov(Acidobacteria ~ Treatment * Zone, data = phyla))
            Df Sum Sq Mean Sq F value Pr(>F)
Treatment    1  0.082  0.0824   0.319 0.6022
Zone         2  3.391  1.6953   6.572 0.0544 .
Residuals    4  1.032  0.2580

And you don't see the interaction term. That's because there are two categories under each zone, i.e "maize_NT_Z1" and "maize_T_Z1" are treated as a same category, but in fact, they are from the same zone Z1, so you should have maize_Z1 for both of them. Let's rectify this:
phyla$Zone = gsub("_[N]*T","",phyla$Zone)
summary(aov(Acidobacteria ~ Treatment * Zone, data = phyla))

               Df Sum Sq Mean Sq F value Pr(>F)
Treatment       1 0.0824  0.0824   0.319 0.6022
Zone            1 0.9381  0.9381   3.636 0.1292
Treatment:Zone  1 2.4525  2.4525   9.507 0.0368 *
Residuals       4 1.0318  0.2580

Answer: Do you have more than one observation for each combination of Treatment and Zone? (Unless you do, you cannot estimated the interaction terms in the aabsence of additional assumptions.) Is your data balanced?
Comment: Thank you for your time and answer.

To answer to your questions:

1- Yes, I do have more than one observation
2- The data is balanced.


prove the following statement $|a-b| \le 2\sqrt{a}c + d$ implies $a \le \varepsilon a/(1+\varepsilon) + (1+\varepsilon)c^2/\varepsilon + (b + d)$

Question: I encountered a statement in a paper and I am not sure why this is the case.
It says: let $a$, $b$, $c$, $d$ be positive real numbers and $\varepsilon \in (0, 1]$, then $|a-b| \le 2\sqrt{a}c + d$ implies $a \le \varepsilon a/(1+\varepsilon) + (1+\varepsilon)c^2/\varepsilon + (b + d)$.
I tried to search online but couldn't find anything. Any reference, hint, proof etc. will be much appreciated. Thanks.
Comment: What is $\epsilon$ ?
Comment: $\varepsilon \in (0, 1]$
Answer: Let $e=a-b-d.$ By hypothesis, $e\le2c\sqrt a,$ and we want to prove that
$$e\le\varepsilon a/(1+\varepsilon) + (1+\varepsilon)c^2/\varepsilon.$$
It is sufficient to prove that
$$2c\sqrt a\le\varepsilon a/(1+\varepsilon) + (1+\varepsilon)c^2/\varepsilon.$$
For this, simply notice that
$$\varepsilon a/(1+\varepsilon)-2c\sqrt a+ (1+\varepsilon)c^2/\varepsilon=\left(\sqrt\frac{a\varepsilon}{1+\varepsilon}-c\sqrt\frac{1+\varepsilon}\varepsilon\right)^2.$$
Comment: Thank you for your quick answer! Just a quick question: how can we remove the absolute value around $a-b$?
Comment: Same as in the other answer: $a-b\le|a-b|.$ (Even simpler: no need for triangular inequality, just apply the definition of the absolute value.)
Answer: First note Young's inequality,
$$ \alpha\beta \leq \delta \alpha^2 + \frac{\beta^2}{4\delta}$$
For all $\alpha,\beta,\delta >0$. Then, by the triangle inequality and the above applied to $\alpha = \sqrt{a}$, $\beta = 2c$, and $\delta = \frac{\epsilon}{1+\epsilon}$,
$$a\leq |a-b|+b \leq 2c\sqrt{a}+d+b \leq \frac{\epsilon a}{1+\epsilon}+\frac{(1+\epsilon)c^2}{\epsilon}+(b+d) $$
Comment: Thanks for your answer! I really appreciate they way you apply triangular inequality and Young's inequality for products to get the result. I think it is more intuitive this way (at least for me).


Vale a pena usar Bootstrap com Angular 8?

Question: Boa noite,
Recentemente comecei a estudar Angular 8. Meu contato com Angular até então era praticamente 0. Hoje me deparei com uma dúvida.
Na maioria das vezes que li sobre o uso de jQuery com Angular, as pessoas desaconselharam o uso de ambos em conjunto.
Hoje, criando algo para conhecer um pouco melhor Angular Router, pensei em usar o Bootstrap pra deixar o conteúdo um pouco melhor. Até que me lembrei: Bootstrap depende de jQuery.
Minha dúvida é:
Vale a pena usar os dois em conjunto? Existe alguma alternativa melhor?
Tenho a impressão de que usando os dois vou estar apenas tornando o site mais pesado.
Obrigado.
Comment: Por que não usa a versão Angular do Bootstrap? A maioria das bibliotecas escritas com jQuery possuem versões reescritas em Angular/Vue/React.
Comment: Obrigado pela dica, vou pesquisar sobre a versão pra Angular.
Answer: não necessariamente precisará de jquery, alem do mais, o jquery é uma "biblioteca" gigantesca, o correto é separar apenas o que você vai usar.
apesar de chamarem o jquery de "biblioteca", a ideia inicial para ele, era ser um framework, assim como o bootstrap, que deveria ser um framework, ao longo do tempo fizeram um desmonte nele, então decretou o status de biblioteca de vez, ele possui algumas coisas que podem entrar em conflito com o angular, pois são parecidos em alguns pontos, nada que não possa ser contornado.
no mais, você não precisará do jquery, porque dá para fazer tudo o que ele faz com o angularjs, tudo dependerá do que pretendes desenvolver, o bootstrap pode sim ser usado, mesmo sem o jquery.
Lembrando que o jquery apenas simplifica o código, não é outra linguagem, assim como o ajax, continua sendo javascript da mesma forma.


Why am i getting this Drush error after updating a site?

Question: I use Drush 8.1.17 to update my Drupal 7 sites, and as I have been updating my sites today after the latest security update I have noticed that after I update a site if I try to run any update related command (ex: drush up -n | grep available) I get this error:
<code>The external command could not be executed due to an application error.                                      [error]
The command could not be executed successfully (returned: PHP Fatal error:  Uncaught                         [error]
TYPO3\PharStreamWrapper\Exception: Unexpected file extension in "phar:///usr/local/bin/drush/includes/.." in
/srv/www/htdocs/fac-dev/misc/typo3/drupal-security/PharExtensionInterceptor.php:39
Stack trace:
#0 /srv/www/htdocs/fac-dev/misc/typo3/phar-stream-wrapper/src/Behavior.php(72):
Drupal\Core\Security\PharExtensionInterceptor->assert('phar:///usr/loc...', 'url_stat')
#1 /srv/www/htdocs/fac-dev/misc/typo3/phar-stream-wrapper/src/Manager.php(83):
TYPO3\PharStreamWrapper\Behavior->assert('phar:///usr/loc...', 'url_stat')
#2 /srv/www/htdocs/fac-dev/misc/typo3/phar-stream-wrapper/src/PharStreamWrapper.php(412):
TYPO3\PharStreamWrapper\Manager->assert('phar:///usr/loc...', 'url_stat')
#3 /srv/www/htdocs/fac-dev/misc/typo3/phar-stream-wrapper/src/PharStreamWrapper.php(401):
TYPO3\PharStreamWrapper\PharStreamWrapper->assert('phar:///usr/loc...', 'url_stat')
#4 [internal function]: TYPO3\PharStreamWrapper\PharStreamWrapper->url_stat('phar:///usr/loc...', 2)
#5 phar:///usr/local/bin/drush/includes/filesyste in
/srv/www/htdocs/fac-dev/misc/typo3/drupal-security/PharExtensionInterceptor.php on line 39
, code: 255)
pm-updatestatus failed.
</code>
Does anyone know why this error only appeared after I updated the site and how I can fix it?
Comment: I'm voting to close this question as off-topic because it's a bug report for the core project, and covered by the latest core releases
Comment: I only read `typo3`???
Comment: What do you mean?
Comment: Wow, there really is TYPO3 in Drupal: https://cgit.drupalcode.org/drupal/tree/misc/typo3/phar-stream-wrapper/README.md?id=7f1f3b8
Answer: It looks like they just released Drupal 7.63 and in the release notes they say, "This is a hotfix release for a regression affecting some Drush installations that was introduced by the fix for SA-CORE-2019-002. No other fixes are included."
Comment: Ah yeah, see [Drush fatal error after upgrading to 8.6.6, 8.5.9, or 7.62: PHP Fatal error: Uncaught TYPO3\PharStreamWrapper\Exception](https://www.drupal.org/project/drupal/issues/3026386).


if A = {a,b,c} . Prove OR disprove that P(A) "power set of A" is a group under the union operation

Question: So my thinking is that
$$P(A) = \{\{\emptyset\},\{a\},\{b\},\{c\},\{a,b\},\{a,c\},\{b,c\},\{a,b,c\}\} $$
although it's clearly closed under union , assiociatve and the identity exists which is the empty set but still it's not a group since there are no inverses exists for any of the elements except for that of the empty set itself.
But is that valid to say , I mean how can I do it more rigorously?
Answer: The empty set is the identity but $B\cup C$ contains $B$ so if $B$ is non empty $B\cup C$ is non empty.  Hence a nonempty element is not invertible.
Answer: Assume that inverses exist. Then:
$\{a\}\cup\{b,c\}=\{a,b,c\}=\{a,b\}\cup\{b,c\}\Rightarrow\{a\}=\{a,b\}$, a contradiction.
Answer: Your proof is valid as it stands.
It would be more compact to say that
$$\forall x \in P(\left\{a,b,c\right\} : a \in x\cup a
$$ so the empty set cannot be the identity element, and then show that
any other element can't be the identity either.
Answer: may be worth noting in this context that the power set of $X$ is a group under the operation of disjoint union, with the empty set as identity. if the complement of $A \subset X$ is $A'$ then the disjoint union of $A$ and $B$ is:
$$
A \circ B = (A \cap B') \cup (A' \cap B)
$$
proving associativity is a nice exercise, though it follows much more simply if the disjoint union is viewed as ordinary addition (mod 2) in $\mathbb{F}_2^X$
Comment: That's not [the usual definition of "disjoint union"](https://en.wikipedia.org/wiki/Disjoint_union), and indeed I've never heard that called the "disjoint union" before; instead, [**symmetric difference**](https://en.wikipedia.org/wiki/Symmetric_difference) is the standard term. Do you have a source for your usage? (It is, confusingly, rarely called the "*disjunctive union*", but that's not the same as "*disjoint* union.")
Comment: you are right, i should have used "symmetric difference". just a senior moment,  thanks for pointing it out.
Answer: Let $~ X= \{1,2,3\}~$ and $~P(X)~$ be the power set of $~X~$. Examine if $~P(X)~$ is a group under the composition $~*~$ defined by $$~A*B= A\cup B ,~~~~~~~ \forall A,B \in P(X)~.$$
Comment: Use LaTeX please: https://math.meta.stackexchange.com/questions/5020


Multiple select in Thymeleaf doesn't store chosen options

Question: I'm trying to learn Spring and other related technologies during summer break by developing a simple web application, however Thymeleaf hinders my progress.
The program is based on two entity classes:
Invoice.java:
@Entity
public class Invoice {
    @Id
    private String invoiceId;

    @NotNull
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate issueDate;

    //getters and setters
}

TrasportOrder.java:
@Entity
public class TransportOrder {

    @Id
    private int number;

    @NotNull
    private BigDecimal value;

    @ManyToOne
    private Invoice invoice;

    //getters and setters
}

I'm getting a form for adding invoices using a method from InvoiceController:
@GetMapping(path = "/add")
public String getForm(Model model) {
     model.addAttribute("unusedOrders", service.getInvoiceOrders(null));
     model.addAttribute("orders", new ArrayList<TransportOrder>());
     model.addAttribute("invoice", new Invoice());

     return "addInvoice";
}

unusedOrders is a list of orders that a user can choose from,
orders is a list that is meant to contain orders chosen by user
invoice is just an invoice that's being created in the form.
My form contains text and data inputs regarding the invoice, and then comes a multiple select for the orders:

<form action="/invoices/add" method="post">

                    Invoice ID:

                    <input type="text" th:value="${invoice.invoiceId}" name="invoiceId"/>

                    Orders:

                    <select id="orders" th:value="${orders}" multiple="multiple">
                        <option th:each="unusedOrder: ${unusedOrders}"
                                th:value="${unusedOrder.number}"
                                th:text="${unusedOrder}">Unused orders to choose from</option>
                    </select>

        <button type="submit">Next</button>
    </form>

I've read Thymeleaf docs and forums, as well as several SO questions, but they still leave me confused about how does th:object, th:field, th:value and others work with forms, and especially with multiple select tag.
On submit, the form sends a POST request to a method in the same controller:
@PostMapping(path = "/add")
public String addInvoice(@ModelAttribute Invoice invoice,
                         BindingResult result,
                         @ModelAttribute("orders") ArrayList<TransportOrder> orders,
                         Model model) {

    //invoice and orders saving logic, etc.

    return "viewInvoices";
}

My problem is that invoice is being properly retrieved from the from and persisted in the database, but orders list stays empty. I expect it to be populated with orders chosen in the form. I don't know, if it's because of @ModelAttribute annotation (I also tried @RequestAttribute without success), Thymeleaf tags, or anything else.
Answer: Okay, so I decided to fight for the answer once more, and fortunately I stumbled upon an answer.
Based on this tutorial, I created an wrapper class ChosenOrdersDTO:
public class ChosenOrdersDTO {
    private List<TransportOrder> chosenOrders;

    //constructor, getters, setters...
}

I added it to the first model (changed the getForm() method as following):
model.addAttribute("chosenOrders", new ChosenOrdersDTO(new ArrayList<>()));

In the form I used the th:field tag, similarly to previous fields:
<select id="orders" th:field="${chosenOrders.chosenOrders}" multiple="multiple">

And in the second controller I was able to retrieve the list wrapped in the ChosenOrdersDTO class as a @ModelAttribute:
@PostMapping(path = "/add")
public String addInvoice(@ModelAttribute Invoice invoice,
                          BindingResult
                         @ModelAttribute ChosenOrdersDTO chosenOrders,
                          Model model)


Fingerings for 2+ octave major/minor arpeggio inversions

Question: Here is a traditional fingering for a Em arpeggio across 2 octaves
RH 123 1235
LH 5421 42
What is fingering best practice one wished to practice 1st and 2nd inversions of Em arpeggio across 2 octaves?
Would one use the fingering above, but starting from the respective inversion's first note?
RH 2312351
LH 421425
Or does each inversion get its own fingering?
Answer: The reason certain fingering is recommended (or suggested) is that it sort of works generally.
When you consider certain notes played in sequence, place your hand over those notes, at least the first three. Bear in mind that the next one in the series will probably need thumb again, in order to reach the topmost note in the second octave, usually with the little finger. This is all for r.h. of course!
So with your example of Em, r.h.1st inversion, the notes are GBEGBEG. It makes sense to start with thumb on G, and expect to move that onto the following G. So you can work out which of your fingers are most comfortable and efficient on the other white keys. Thwe do the same procedure for l.h.
That's part of the practice routine for a piano player - sorting out which fingering is best. Don't rely on complete strangers telling what is best - I could say what's best for me, but why should that automatically be best for you?


Como substituir dados da tag

Question: Estou tentando substituir um determinado valor dentro de um HTML parseado, usando a seguinte função eu consigo substituir valores que estão dentro de tags inputs, css e etc:
$('input[value="/services/CouponTemplate.mvc/GetCoupon"]').attr('value', function(_, href){
    return "https://meusite.com?url=https://siteremoto.com" + href;
});

Só que agora preciso substituir um endereço que está dentro da tag

Eu preciso substituir o /services/InPlayApp.mvc/, eu consigo por PHP com str_replace, mas gostaria de saber se tem como fazer o mesmo utilizando jQuery.
Tentei repetir o procedimento trocando o input por script mas não funcionou, sou iniciante no mundo Javascript.
Existe algo semelhante ao str_replace do PHP em jQuery?
Comment: Eu acho que isso deve mesmo ser feito no lado do servidor. Se tens de mudar no lado do cliente é opção simplesmente dar um novo valor à variável?: `inPlayPreferences.InPlayAppServiceName = "/services/novoNome.mvc/";`
Comment: Não entendi o motivo disso. Não poderia apenas atribuir um novo valor ao array? inPlayPreferences["InPlayAppServiceName"] = "novo valor aqui..";
Comment: Os dados de `InPlayAppServiceName` são gerados automaticamente pelo site remoto, se eu atribuísse um novo valor a ele, simplesmente o script pararia de funcionar, pois o código é grande e contém instruções em base_64 que é gerado em cada página de forma diferente, só resumi o código para facilitar a explicação.
Já a questão do Sérgio, eu estou fazendo isso mesmo no lado do servidor, mas queria saber se poderia ser feito no lado cliente, apenas por curiosidade mesmo.
Answer: Você pode atribuir um identificador para esta tag script, assim fica mais fácil encontrá-la, ex:

Com jQuery:

$(function(){

  var script = $('#theScript'); // alvo
  var content = 'var bar = 10;'; // conteúdo que será inserido dentro da tag

  $('#changeScriptContent').on('click', function(){
    alert('Antes de alterar:\n' + script.get(0).outerHTML);
    script.html(content); // :)
    alert('Depois de alterar:\n' + script.get(0).outerHTML);
  });
});

<button id='changeScriptContent'>Alterar</button>

Somente com Javascript:

var button = document.getElementById('changeScriptContent'), // botão que altera o conteúdo
    script = document.getElementById('theScript'), // alvo
    content = 'var bar = 10;'; // conteúdo que será inserido

button.addEventListener('click', function(){
    alert("Antes de alterar:\n" + script.outerHTML);
    script.innerHTML = content; // :)
    alert("Depois de alterar:\n" + script.outerHTML);
});

<button id='changeScriptContent'>Alterar</button>
Comment: Obrigado! excelente.

Como substituir dados da tag


Repeated measures regression or time series analysis?

Question: I have managed to confuse myself again, so I hope you could give me some tips or point into the right direction.
I have pesticide residue data in different plant matrices (e.g. flowers, leaves) from different experiments. Samples were taken on 5 subsequent days (DAA). Each day five repetitions were collected (from the same plants- repeated measures). I also have additional information like temperature or the applied dose rate which could affect the residues.
    A tibble: 6 x 9
# Groups:   DAA, App [6]
  Repetition   DAA Doserate AI     App          Site  Temperature Flowers Leaves
       <dbl> <dbl>    <dbl> <chr>  <chr>        <chr>       <dbl>   <dbl>  <dbl>
1          1     0     0.5  Fludio Field_Fludio Field        16.9   22.3    80.6
2          1     0     0.75 Cyp    Field_Cypro  Field        16.9   31.0    52.7
3          1     0     0.75 Cyp    Hand         GH           20.5   89.6   306.
4          1     1     0.5  Fludio Field_Fludio Field        16.8    3.24   66.7
5          1     1     0.75 Cyp    Field_Cypro  Field        16.8    7.36   56.3
6          1     1     0.75 Cyp    Track        GH           20.5    5.42  280.

I would like to run a regression and see whether I could predict residues in flowers from residues I found in leaves. From graphs I looked at I assume the sampling day (DAA) and temperature would affect the residues as well and could be included into the model. I thought I could do something like this:
fit1 <- lmer(Pollen ~ Flowers*DAA+Temperature+(1|Repetition), data=apptest)
So far (in other analyses with lmer) I have used the sampling days (DAA) as a factor variable. But now I have read that I cant use a factor in regression. So either I would have to use DAA as numeric variable or instead I would have to introduce dummy variables. I think I am confused about whether DAA is a factor or a numeric variable. From this plot it looks like a factor:

It would be ok to have 4 dummy variables for this data set, but I have another dataset with 12 sampling days. I also came across time series analysis to make predictions over time, but I dont think that would make any sense with only 5 days?
So my questions:
Is my variable day(DAA) a factor variable or a numeric one? How would I include it into my regression model?
And in which circumstances/for which kind of analysis is it ok to have a factor in a lmer model?
Many thanks for your help in advance!
Comment: If the residues you found in leaves are equally affected by time as those you found in the flowers, regressing on residues could be enough to take the time effect into account. Else, you would need to model the time as well. You could check graphically if you have some remaining time trend by plotting the residuals against DAA. If you are not directly interested in the effect of time, that is. DAA should be seen as a time index, not a factor.
Comment: Thank you very much for your advice, some things are already much clearer. Leaves and flowers were equally affected by time in this data set, but it might be useful for other data sets. Furthermore, I have plotted the residuals against DAA, and there is some pattern visible, so I assume DAA could have an effect. What would be the best approach to include DAA into the regression? Is it sufficient to use as.ts(DAA) and just include it? I have found a lot of information about time series online, but mainly of huge data sets with years of data!
Answer:
But now I have read that I cant use a factor in regression.

I don't know where you have read this but there must be some misunderstanding. You can, of course, use a factor variable in regression models.

So either I would have to use DAA as numeric variable or instead I would have to introduce dummy variables.

When you use a factor variable in a regression model, the software creates dummy variables behind the scenes.
Based on the information given, I don't see anything wrong with your model:
lmer(Pollen ~ Flowers*DAA+Temperature+(1|Repetition), data=apptest)

Since DDA is measuring time, it probably makes more sense to code is as numeric, especially when you have 12 time points, otherwise it will make the interpretation quite cumbersome. You may of course, also want to investigate nonlinearity.
Comment: Thanks! I have used ```r.squaredGLMM(x) ``` from library(MuMIn) for the calculation, just to get an idea of what is going on. I am sure I can find information there on how it is calculated and do some research myself and hope I wont confuse myself again :)
Comment: Thanks a lot! But to be honest, now I am even more confused... It would make my life much easier if I could just include factors (and tbh I didnt quite understand why I shouldnt), but I have been reading a lot (online) that the variables should be numeric or be converted into dummies...
Comment: You can include factors, I didn't say you can't. I just said it would make the interpretation easier, if you code it numeric. I don't know why you think it would make your life easier ?
Comment: Yes I understood your comment-sorry if i havent been quite clear. I just became so confused because I have read in several posts that I couldnt include factors in a regression to predict a variable, but that was what I had done so far (I will try to find these posts again and include the links). You have simply taken away all my confusion and confirmed that I have done everything correctly so far- thanks for that! Another question if you dont mind: Why does my Rsqrt decrease when I use DAA as numeric instead of as factor (from 0.85 to 0.74)?
Comment: You're welcome. Again: you CAN include a categorical (factor) variable in a regression model. As for the question about $R^2$, this is a mixed effects model and $R^2$ is not well defined for mixed models, as it would be for a model without random effects. There are many proposals for ways to calculate a $R^2$-like quantity but none of these have the same properties as $R^2$ for a model without random effects. So the answer to your question will depend on how it is calculated. It might be related to degrees of freedom (the factor variable will use more). You can ask a new question about that :)


Pearl et al. (2016) definition of cause: circular?; how to improve?

Question: Pearl et al. "Causal Inference in Statistics: A Primer" (2016) p. 26 provides the following definitions of direct cause and cause:

A variable $X$ is a direct cause of a variable $Y$ if $X$ appears in
  the function that assigns $Y$'s value. $X$ is a cause of $Y$ if it
  is a direct cause of $Y$, or of any cause of $Y$.

It seems to me the definition of cause is circular (and thus essentially invalid) as it refers back to itself within the definition. Am I misunderstanding it? If not, how could the definition be improved?
Answer: It's not circular, it's just recursive. It makes the functional programmer in me smile.
Note that this is actually two definitions - that of a direct cause, and then the more generic definition of (any kind of) cause, using the former as the boundary condition.
It's quite similar to how the set-theoretic definition of natural numbers, if you're familiar with that - there's the base case (empty set for 0 in numbers, direct cause for general causality), and then the recursive part (e.g. 1 is a set of empty sets, and a level-1 generalized cause is the direct cause of a direct cause of Y).
Comment: I think I got it. I took a walk (before seeing your last comment), and it fell into places. Thank you for the answer!
Comment: I was suspecting something like that, but it is still hard to wrap my head around it... How do I verify whether $X$ is a cause of $Y$ in cases when it is not a direct cause? In such cases, I have to verify that $X$ is a direct cause of a cause of $Y$. But the definition of a cause is not explicit (it is recursive). Could you help me further?
Comment: Y as direct cause of Z: Z = f(Y, u1). X as direct cause of Y: Y = g(X, u2). X as cause of Z: Z = f(g(X, u2), u1), where the 'u's are sets of various other causal factors we're not interested in. If you can only observe X and Z, you may or may not be able to identify causal effects. If you do have Y, it's Bayes and chain rule time. This is a very TL;DR version, you really do need a proper book to cover everything.


If my servers get overloaded, how hard is it to get my site back up?

Question: If my servers get many visitors, and it can't handle the traffic, what would happen to the site, and how do I get it back up?
Answer: Getting your site back up depends on exactly what happened:

If your provider allows for 10 Mbps bandwidth, each connection will have to share that  bandwidth, so if you have 10,000 people all downloading at the same time, each download will be slow.  When the downloads finish, everything will return to normal again.
If your provider allows for 10GB of data transfer a month and you use that up, they'll likely turn off your site until you pay them for more data.
If your server only allows 1000 connections at once, the 1001st person who tries to connect will have to wait until there's an opening, and either timeout or just get slow page loads.  Once there are openings, everything returns to normal again.
If the server runs out of RAM then an important process may crash or be killed.  In this case, an admin may have to restart that process.
While it's unlikely with well-built systems in well-cooled facilities, if the server heats up under load, then high load may cause heat damage (failed drives, damaged processors, etc) that might require replacing the system.
Answer: If you expect to get a lot of visits from Launch, then it is probably worth doing some performance testing and scale up as much as you can afford.
As far as what happens, if you get way more traffic then you can handle your sites will be down for all practical purposes too all users. To limit the damage you could put a rate limiter in front of your servers, so at least a certain amount of people can use the site. If these are web servers, then you might have a look at HAProxy rate limiting.
Always consider caching as much as possible, this can allow you to handle a lot more load if your application is such that caching can be used.
Answer: Nothing more than visitors will have to wait or even can't see your site content wouldn't happen. You have to optimize your web stack or upgrade your hardware/balance traffic
Answer: Some web frameworks/setups are subject to positive load feedback effects, these can require manual intervention to get the site back up even if traffic has lowered again (lingering requests, for example to backend databases, still retrying, left from the load bursts, cause new requests to have far less of a budget, resulting in a smaller number of requests needed to drive the application over the edge again...)
If load balancers are involved: Nodes going offline from overload will leave even less nodes to deal with the onslaught...
Cache stampeding effecs somewhere in the stack can also cause very unstable behaviour.


Keep the focus to the current row while adding new rows dynamically to the jqgrid

Question: I have jqgrid-treegrid in a system to display all records in the same page with scrolls. But Im loading the records dynamically after loading set of records initially(kind of backgroud process).
One of the grid column is editable, so I keep update that particular cell in each row as the rest of the rows are being added to the grid.
The problem is as Im using
<code>afterInsertRow: function(id)
{
  jQuery('#myGrid').jqGrid('editRow',id,true);
}
</code>
to set the row editable, Im losing the focus(cursor) from the current row to the newly added row.
But I really need to keep the cursor to the current row without interrupting my updating process.
Help me on this pls...
Answer: Try this API
<code>$(grid).setSelection(selectedGuidRow, true);
</code>
For more detail JqGris.Selection
Comment: Thank you for answering, But my problem is not with the row selection. The selection remains with the current row but the cursor only moves to the input field of the newly added row. Hope you got this issue.
Answer: Finally I found a way...
I commented this line in jquery.jqGrid.src.js (line no 9035)
<code>//$("td:eq("+focus+") input",ind).focus();
</code>


Angular.js - error with providerInjector

Question: I have a problem with providers in angular, I get this error:

Row 2696: Error: Unknown provider: a

Using unminified angular v 1.06, Row 2696:
providerInjector = createInternalInjector(providerCache, function() {
    throw Error("Unknown provider: " + path.join(' <- '));
}),

This is the code:
var myApp = angular.module('myApp', [], function ($interpolateProvider) {
    $interpolateProvider.startSymbol('{[{');
    $interpolateProvider.endSymbol('}]}');
});

myApp.directive('buttonsRadio', function() {
[...]
});

myApp.controller('MainController', function MainController ($scope) {
[...]
})

Any ideas?
Edit: Added error message:
Error: Unknown provider: aProvider <- a
createInjector/providerInjector<@/libs/angular.js:2696
getService@/libs/angular.js:2824
createInjector/instanceCache.$injector<@/libs/angular.js:2701
getService@/libs/angular.js:2824
invoke@/libs/angular.js:2842
instantiate@/libs/angular.js:2874
@/libs/angular.js:4759
applyDirectivesToNode/nodeLinkFn/<@/libs/angular.js:4338
forEach@/libs/angular.js:138
nodeLinkFn@/libs/angular.js:4323
compositeLinkFn@/libs/angular.js:3969
compositeLinkFn@/libs/angular.js:3972
nodeLinkFn@/libs/angular.js:4354
compositeLinkFn@/libs/angular.js:3969
publicLinkFn@/libs/angular.js:3874
bootstrap/resumeBootstrapInternal/</<@/libs/angular.js:963
Scope.prototype.$eval@/libs/angular.js:8011
Scope.prototype.$apply@/libs/angular.js:8091
bootstrap/resumeBootstrapInternal/<@/libs/angular.js:961
invoke@/libs/angular.js:2857
bootstrap/resumeBootstrapInternal@/libs/angular.js:960
bootstrap@/libs/angular.js:973
angularInit@/libs/angular.js:934
@/libs/angular.js:14756
f.Callbacks/n@http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2
f.Callbacks/o.fireWith@http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2
.ready@http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2
B@http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2

/libs/angular.js
Line 5704

Answer: Before running uglify or any other ofuscator/compression algorithm, you should run the grunt task ng-annotate which adds the dependencies as strings for you as an injectable array, this makes your code a lot cleaner as the injectable annotaion can be added automatically as a build step rather than having to manually code it.
myApp.controller('MainController', function MainController ($scope) {
[...]
})

Becomes:
myApp.controller('MainController', ['$scope', function MainController ($scope) {
[...]
}])

Take a look at: https://github.com/olov/ng-annotate
Update:
ng-min   AngularJS Pre-minifier deprecated –> use ng-annotate
Answer: I assume you're using some kind of javascript obufuscator (Clousure, SquishIt, UglifyJS etc).
In this case you need to specify dependencies in such way:
var myApp = angular.module('myApp', [], ['$interpolateProvider',function ($interpolateProvider) {
    $interpolateProvider.startSymbol('{[{');
    $interpolateProvider.endSymbol('}]}');
}]);

myApp.directive('buttonsRadio', function() {
[...]
});

myApp.controller('MainController',['$scope', function MainController ($scope) {
[...]
}])

Note to specify dependenices for dependency injection - instead of passing function, you need to specify array with list of strings with names of objects to inject into parameters and the function itself.
Comment: I am actually using the source as is without obfuscation. I think I tried that solution (will try again), could it be something else?
Comment: Other option could be that you're passing somewhere function with parameter named 'a' where AngularJS expect function with injectable parameters (e.g. controller function even in directive definition object). Try to post part of your code into Plunker to reproduce error. In this case you can easier find a place + we could take a look and give you advice.
Comment: Seems I got rid of one error. Do I need to inject the $interpolateProvider into the directive and controller as well perhaps??
Comment: if you're not using that provider there, you can not pass. The good think in DI that you need to inject only what you're using. Only advice you is to split you definition of module into definition itself and configuring. like ```var myApp = angular.module('myApp', []); myApp.config(['$interpolateProvider',function (...)]);``` in this case it would be little bit more readable.
Comment: Thanks. Does not seem to be an "a" param. Added error message to the question, will provide Plunker if I can reproduce it there. Is there a better way to debug angular??


Is there any data on how successful runners are at stealing third base with right-handed vs. left-handed batters?

Question: According to an article, between 1998 and 2002, base runners successfully stole second base 69% of the time, and they successfully stole third base 72% of the time.
It might seem that it should be harder to steal third base than second base, given that the catcher has a shorter distance to throw to third base (90 feet) than second base (127 feet and 3 3/8 inches). However, given that the majority of batters are right-handed, they may obstruct the catcher from making a good throw to third base.
Is there any data that breaks down how often third base was stolen successfully with right-handed batters vs. left-handed batters?
Comment: Batters are required to get out of the way of throws, so I don't know if there is likely to be such a difference... I'll think about whether it's something I could easily test using BBRef data.
Answer: Clarification/Context
I don't quite have enough reputation to leave a comment but, @Joe batters are not obliged to get out of the way. As stated on UmpireBible Rule 6.03(a)(3), a batter is considered out if

He interferes with the catcher's fielding or throwing by stepping out of the batter's box or making any other movement that hinders the catcher's play at home base.

As stated by UmpireBible (again) in a write up, there is somewhat of a gray area here in the rule book about batter interference. While the rules state the Catcher has the "right of way" and should have a clear path to make whatever play he feel necessary, you can't expect the batter inside of the batter's box to "just disappear" from the play either. Another quote from the UmpireBible

The emphasis is important because "any other movement" covers a lot of ground. A lot.

Point is, the batter is not required to get out of the way and from my interpretation, SHOULD NOT get out of way. From my own encounters (played baseball for 15 years) I personally never left the batter's box when I knew a throw down to third was happening while I was batting. In fact, most of the time I saw it happen, I believed the batter to inch forward towards the pitcher if anything to not get domed in the helmet by the catcher throwing down to third.(Somewhat disproving your theory that the batter's handedness influences the steal percentage of third.)
Answer
As far as the answer to your question(sorry for the small rule lecture), there are multiple factors influencing the steal rate for third base being a few percentage points higher.
InexperienceWhile the throw down to third base is shorter for the catcher, catchers have less experience making the throw down to third base thus it is more uncomfortable for them.(Catcher SB Attempts Against) Pitchers have a harder time "holding" the runner on second base for similar reasons. Pitchers do not make throws to second base very often and do not practice holding runners on second base very often as opposed to throwing over to first base. (Pitcher Pickoff Attempts)Catcher Pop Times Runners are less expected to steal third base which might make it more of a surprise, in turn the runner throws the catcher off resulting in varying pop times (The time it takes for the catcher to catch a pitched ball and throw it down to the base, second or third) that are apparent in the Baseball Savant link in the previous paragraph(Catcher Pop Times). While all of this is true, the pop times are still significantly slower to second base as opposed to third base (about .5 second or 25% slower) so my previous statements don't fully explain the reasoning behind third base having a whole 3% higher success rate.

Taking a Lead
Runners are not able to take as big of a lead off of first base than they are second. While having a left handed pitcher on the mound presents the obvious challenge a baserunner on first is presented with, (the pitcher is looking right at them) taking a lead off of first base is harder because the first baseman is almost always "holding" the runner on by standing on first base itself. Whoever is tasked with holding the runner on second base, either the shortstop or second basemen, NEVER stands directly on the base and rarely comes over to "hold" the runner on the bag. In turn, the baserunner on second base is normally able to take a bigger lead (I struggled to find direct statistics on this).

Other Factors
There are a few other factors that might lead to this as well. Since third base is perceived as "harder to steal", only the fastest of runners are even attempting to steal third base. Thus they bring the average up and result in a higher percentage. Another factor is just the small sample size. While your statistic is shown over the course of 4 seasons (1998-2002), third base steal attempt data is only 10% that of second base steal attempt data. Lastly, somewhat building upon what I said earlier, pithers make more errors picking off to second base thus don't do as good of a job as they at first base. There have been 12 pick off errors this year(2022), and almost half of them (5 out of 12/ Pitcher Pickoff Errors) have been to second base even though pitchers pick off to second base significantly less.
TL;DR
Sadly I could not find exact data representing the information you are seeking. There are a lot of factors that are influencing the third base stolen base success percentage being a little higher than first base stolen base success percentage, but I do not believe the elevated success rate could be tied to the batter being a hinderance.


Wrong result from GlobalMemoryStatusEx() on Windows

Question: I'm trying to get total system memory using GlobalMemoryStatusEx():
    MEMORYSTATUSEX memory;
    GlobalMemoryStatusEx(&memory);
#define PRINT(v) {printf("%s ~%.3fGB\n", (#v), ((double)v)/(1024.*1024.*1024.));}
    PRINT(memory.ullAvailPhys);
    PRINT(memory.ullTotalPhys);
    PRINT(memory.ullTotalVirtual);
    PRINT(memory.ullAvailPageFile);
    PRINT(memory.ullTotalPageFile);
#undef PRINT
    fflush(stdout);

But the result is very weired and not understandable.
memory.ullAvailPhys ~1.002GB
memory.ullTotalPhys ~1.002GB
memory.ullTotalVirtual ~0.154GB
memory.ullAvailPageFile ~0.002GB
memory.ullTotalPageFile ~1.002GB

My total physical memory is 8GB but non of result is close it. All values are much smaller.
Also, the 'total' values keep changing whenever I execute. For instance, another result is here:
memory.ullAvailPhys ~0.979GB
memory.ullTotalPhys ~0.979GB
memory.ullTotalVirtual ~0.154GB
memory.ullAvailPageFile ~0.002GB
memory.ullTotalPageFile ~0.979GB

What am I doing wrong?
Comment: You print values to output? Make sure you treat that as 64-bit integers, without clipping upper 32 bits.
Comment: @RomanR. Okay, I edited post. Now I'm trying to cast all value to double but the values are still extremely small.
Answer: This is the part you are missing:
MEMORYSTATUSEX memory = { sizeof memory };

MSDN:

dwLength
  The size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx.

If you checked value returned by GlobalMemoryStatusEx, you could see the problem by getting error indication there.


How to calculate a composite score from correlations in a meta-analysis

Question: I am conducting a meta-analysis on safety culture (predictor) and safety performance (outcome). In some studies that I am coding, they presented the correlations between the subscales of safety culture and safety performance, without providing the correlation between the overall score of safety culture and safety performance.
For example:

What is the best way to calculate the overall safety culture score so that I can figure out its correlation with safety performance?
If it helps, I am calculating the effect size using Hunter and Schmidt's method.
Thank you!
Answer: I'm posting this as an answer since I do not have enough reputation points to post a comment. Your answer is in the book of Hunter and Schmidt (2014). Several examples are discussed in the book on how to deal with the situation you describe in your question. I do not have the book with me at the moment, so I cannot provide you a page number. However, searching the index of the book will definitely bring you to the right page.
Schmidt, F. L., & Hunter, J. E. (2014). Methods of meta-analysis: Correcting error and bias in research findings.
Comment: Thanks for the answer! I had a look at the book, and it is suggested that the "correlations should be averaged and the average could be contributed as the one value representing the study" (p. 430 in the 2nd edition). However, I consulted other sources which argue that, correlations should not be averaged without transforming to Fisher's z first, then back-transforming to Pearson r.

The problem however, is that Hunter and Schmidt also discourage the transformation of r to z.
Comment: Yes, there is indeed no consensus about whether you should transform your correlations to Fisher's z and then back-transforming to Pearson r. Ralf Schulze compared in his book different meta-analysis methods that use correlation coefficients (also with transforming to Fisher's z) [(link to amazon)](https://www.amazon.com/Meta-Analysis-Comparision-Approaches-Ralf-Schulze-ebook/dp/B00CDM4AH2/ref=sr_1_2?s=books&ie=UTF8&qid=1469082464&sr=1-2&keywords=meta-analysis+ralf+schulze#navbar). Maybe this is useful for you.


PHP Form Validation on Empty Fields and Error Messages

Question: I'm not sure what I'm doing wrong. I'm trying to create a simple form which will not upload to the MySQL DB if the fields are empty. I also have some basic validation in the form. However, when I click 'save' the blank form is uploaded to the DB and I also do not get the error messages per field as I have highlighted them.
I would appreciate suggestion on where I err.
Here's my PHP Code:
<?php

$firstNameError = $lastNameError = $idNumberError = $mobileNumberError = $emailError = $birthDateError = $languageTypeError = $interestError = "";
$firstName = $lastName = $idNumber = $mobileNumber = $email = $birthDate = $languageType = $interest = "";

if(isset($_POST['submit'])) {

    include 'dbconnect.php';

    try {

        $sql = "INSERT INTO members SET
                firstName = :firstName,
                lastName = :lastName,
                idNumber = :idNumber,
                mobileNumber = :mobileNumber,
                email = :email,
                birthDate = :birthDate,
                languageType = :languageType,
                interest = :interest,
                created = :created";

        $stmt = $conn->prepare($sql);

        if (empty($_POST["firstName"])) {
            $firstNameError = "First Name is Required Please";
        } else {
            $firstName = clean_data($_POST["firstName"]);
                if (!preg_match("/^[a-zA-Z ]*$/",$firstName)) {
                    $firstNameError = "Only letters and white space allowed";
                }
        }
        if (empty($_POST["lastName"])) {
            $lastNameError = "Last Name is Required Please";
        } else {
            $lastName = clean_data($_POST["lastName"]);
                if (!preg_match("/^[a-zA-Z ]*$/",$lastName)) {
                    $lastNameError = "Only letters and white space allowed";
                }
        }
        if (empty($_POST["idNumber"])) {
            $idNumberError = "ID Number is Required Please";
        } else {
            $idNumber = clean_data($_POST["idNumber"]);
            if (!preg_match("/^[0-9]{13}$/",$idNumber)) {
                    $idNumberError = "ID must be exactly 13 digits and no white spaces or other characters";
                }
        }
        if (empty($_POST["mobileNumber"])) {
            $mobileNumberError = "Mobile Nr is Required Please";
        } else {
            $mobileNumber = clean_data($_POST["mobileNumber"]);
            if (!preg_match("/^[0-9]{10}$/",$mobileNumber)) {
                    $mobileNumberError = "Your phone nr must be exactly 10 digits and no white spaces or other characters";
                }
        }
        if (empty($_POST["email"])) {
            $emailError = "Email is Required Please";
        } else {
            $email = clean_data($_POST["email"]);
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $emailError = "Please enter a valid email address";
            }
        }
        if (empty($_POST["birthDate"])) {
            $birthDateError = "Birth Date is Required Please";
        } else {
            $birthDate = clean_data($_POST["birthDate"]);
            list($dd,$mm,$yyyy) = explode('-',$birthDate);
            if (!checkdate($yyyy,$mm,$dd)) {
                $birthDateError = "Please use the format YYYY-MM-DD";
            }
        }
        if (empty($_POST["languageType"])) {
            $languageTypeError = "Languge Type is Required Please";
        } else {
            $languageType = clean_data($_POST["languageType"]);
        }
        if (empty($_POST["interest"])) {
            $interestError = "Interest is Required Please";
        } else {
            $interest = clean_data($_POST["interest"]);
        }

        $stmt->bindParam(':firstName', $firstName);
        $stmt->bindParam(':lastName', $lastName);
        $stmt->bindParam(':idNumber', $idNumber);
        $stmt->bindParam(':mobileNumber', $mobileNumber);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':birthDate', $birthDate);
        $stmt->bindParam(':languageType', $languageType);
        $stmt->bindParam(':interest', $interest);

        $created = date('Y-m-d H:i:s');
        $stmt->bindParam(':created', $created);

        if($stmt->execute()){
            echo "
Member was saved.
";
        } else {
            echo "
Unable to save this member.
";
        }
    }

    catch(PDOException $exception){
        die('ERROR: ' . $exception->getMessage());
    }

}

function clean_data($data) {

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;

}

?>
And here is my HTML Code:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

            First Name: *
            <input type='text' name='firstName' class='form-control' />

            Last Name: *
            <input type='text' name='lastName' class='form-control' />

            ID Number: *
            <input type='text' name='idNumber' class='form-control' />

            Mobile Number: *
            <input type='text' name='mobileNumber' class='form-control' />

            Email: *
            <input type='text' name='email' class='form-control' />

            Birth Date
            <input type='text' name='birthDate' class='form-control' />

            Language

                <select class="form-control" name="languageType">

                  <option>Select One...</option>
                  <option>Afrikaans</option>
                  <option>English</option>
                  <option>Zulu</option>
                  <option>Xhosa</option>
                  <option>Venda</option>
                  <option>French</option>

              </select>

            Interest

                <select class="form-control" name="interest">

                  <option>Select One...</option>
                  <option>Golf</option>
                  <option>Rugby</option>
                  <option>Tennis</option>
                  <option>Cricket</option>
                  <option>Swimming</option>
                  <option>Hiking</option>
                  <option>Surfing</option>
                  <option>Movies</option>
                  <option>Swords</option>

                </select>

                <input type='submit' name='submit' value='Save' class='btn btn-primary' />
                Back to view members

</form>

Comment: Thanks Difster, not sure why the thread posted like that
Comment: You know, if you want to include jQuery on the front end, they have an excellent validation class built in.
Comment: You do your validation as in, if empty foo, fooError = 'foo can't be empty', but then do the insert anyway.  Mark a boolean such as $is_valid as false at that point, and then check that before the insert.
Comment: Yes Difster, I need to look more into JQuery. Thanks for the advice
Answer: Unless I'm mistaken (and I frequently am), it doesn't look like you're testing to see if there are error messages before trying to write to the database. You should do all of your data validation first and THEN put the sql statment in the try/catch but only if all the errors evaluate to false. If you think you can rearrange that on your own, go for it. If you need help, let me know.
Another hint, set another variable called $error = false; at the beginning and then if any of the error messages get triggered, set it to to true. Then, for your database write, you only have to check to see if($error == true) ...
Comment: I must admit that I made a schoolboy error in not echo'ing my error messages. I fixed that now
Answer: You are assigning error string for every unsuccessful field name.
But, not using it anywhere.
Modify the code to:
if ($firstNameError != '' OR  $lastNameError != '' OR  $idNumberError != '' OR  $mobileNumberError != '' OR  $emailError != '' OR  $birthDateError != '' OR  $languageTypeError != '' OR  $interestError != '') {
 echo "
Unable to save this member.
";
 // Die or redirect to error page.
}
else {
 // Code for inserting into database.
}

Comment: @Progrock, thanks for comment. I am looking into the link you have sent and will update my answer accordingly once I read it out. Thanks for sharing such a knowledgeable thing. This is the way SO users get benefited.
Comment: @Progrock, updated the answer and removed the `action` related thing. Thanks :)
Answer: So I added 'throw new exception' in the php code which blocks the data from being submitted to the db. Below I highlighted the change.
if (empty($_POST["firstName"])) {
                        *throw new Exception ("First Name is a Required Field");*
                    } else {
                        $firstName = clean_data($_POST["firstName"]);
                            if (!preg_match("/^[a-zA-Z ]*$/",$firstName)) {
                                *throw new Exception ("Only letters and whitespace allowed in First Name");*
                            }
                    }
                    if (empty($_POST["lastName"])) {
                        *throw new Exception ("Last Name is a Required Field");*
                    } else {
                        $lastName = clean_data($_POST["lastName"]);
                            if (!preg_match("/^[a-zA-Z ]*$/",$lastName)) {
                                *throw new Exception ("Only letters and whitespace allowed in Last Name");*
                            }
                        }
                    if (empty($_POST["idNumber"])) {
                        *throw new Exception ("ID Number is a Required Field");*
                    } else {
                        $idNumber = clean_data($_POST["idNumber"]);
                        if (!preg_match("/^[0-9]{13}$/",$idNumber)) {
                                *throw new Exception ("ID Number may only contain numbers and may only be 13 digits long");*
                            }
                    }
                    if (empty($_POST["mobileNumber"])) {
                        *throw new Exception ("Mobile Number is a Required Field");*
                    } else {
                        $mobileNumber = clean_data($_POST["mobileNumber"]);
                        if (!preg_match("/^[0-9]{10}$/",$mobileNumber)) {
                                *throw new Exception ("Mobile Number may only contain numbers and may only be 10 digits long");*
                            }
                    }
                    if (empty($_POST["email"])) {
                        *throw new Exception ("Email is a Required Field");*
                    } else {
                        $email = clean_data($_POST["email"]);
                        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                            *throw new Exception ("Please make sure you entered a valid email");*
                        }
                    }
                    if (empty($_POST["birthDate"])) {
                        *throw new Exception ("D.O.B. is a Required Field");*
                    } else {
                        $birthDate = clean_data($_POST["birthDate"]);
                    }
                    if (empty($_POST["languageType"])) {
                        *throw new Exception ("Please select a language");*
                    } else {
                        $languageType = clean_data($_POST["languageType"]);
                    }
                    if (empty($_POST["interest"])) {
                        *throw new Exception ("Please select a interest");*
                    } else {
                        $interest = clean_data($_POST["interest"]);
                    }

                    $stmt->bindParam(':firstName', $firstName);
                    $stmt->bindParam(':lastName', $lastName);
                    $stmt->bindParam(':idNumber', $idNumber);
                    $stmt->bindParam(':mobileNumber', $mobileNumber);
                    $stmt->bindParam(':email', $email);
                    $stmt->bindParam(':birthDate', $birthDate);
                    $stmt->bindParam(':languageType', $languageType);
                    $stmt->bindParam(':interest', $interest);

                    $created = date('Y-m-d H:i:s');
                    $stmt->bindParam(':created', $created);

                    if($stmt->execute()){
                        echo "
Member was saved.
";
                    } else {
                        echo "
Unable to save this member.
";
                    }
                }

                catch(Exception $exception){
                    *echo '
' . $exception->getMessage() . '
';*
                }

In html I just added the required attribute as shown below:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

                    First Name: *
                    <input type='text' name='firstName' class='form-control' required/>

                    Last Name: *
                    <input type='text' name='lastName' class='form-control' required/>

                    ID Number: *
                    <input type='text' name='idNumber' class='form-control' required/>

                    Mobile Number: *
                    <input type='text' name='mobileNumber' class='form-control' required/>

                    Email: *
                    <input type='text' name='email' class='form-control' required/>

                    Birth Date: *
                    <input type='date' name='birthDate' class='form-control' required/>

                    Language: *

                        <select class="form-control" name="languageType" required>

                          <option>Select One...</option>
                          <option>Afrikaans</option>
                          <option>English</option>
                          <option>Zulu</option>
                          <option>Xhosa</option>
                          <option>Venda</option>
                          <option>French</option>

                        </select>

                    Interest: *

                        <select class="form-control" name="interest" required>

                          <option>Select One...</option>
                          <option>Golf</option>
                          <option>Rugby</option>
                          <option>Tennis</option>
                          <option>Cricket</option>
                          <option>Swimming</option>
                          <option>Hiking</option>
                          <option>Surfing</option>
                          <option>Movies</option>
                          <option>Swords</option>

                        </select>

                        <input type='submit' name='submit' value='Save' class='btn btn-primary' />
                        Back to view members

        </form>

Comment: What if your database insert throws an exception?
Comment: I'm not sure how a user is expected to correct any errors?  Using exceptions, only one input error presents a warning at a time.  With the user potentially having to go back a page and try again until all fields are correct.  Could you present all input errors at once to the user?.
Comment: I'd constrain the users choices for language and interest with validation also.  Perhaps a non-issue but the user could submit values not in those select lists.
Comment: Your clean data function uses htmlspecialchars, why?
Comment: Hi Prgrock, as far as I know htmlspecialchars help to prevent users entering malicious code. Just putting it in there in case someone tries to hack me. Won't do harm would it?
Comment: It's knowing where to use it.
Comment: What if my name is Jim O'Reilly?
Answer:

     <form action="" method="post">

                            First Name: *
                            <input type='text' name='firstName' class='form-control' required/>

                            Last Name: *
                            <input type='text' name='lastName' class='form-control' required />

                            ID Number: *
                            <input type='text' name='idNumber' class='form-control' required/>

                            Mobile Number: *
                            <input type='text' name='mobileNumber' class='form-control' required/>

                            Email: *
                            <input type='text' name='email' class='form-control' required />

                            Birth Date
                            <input type='text' name='birthDate' class='form-control' required/>

                            Language

                                <select class="form-control required" name="languageType" required>

                                  <option>Select One...</option>
                                  <option>Afrikaans</option>
                                  <option>English</option>
                                  <option>Zulu</option>
                                  <option>Xhosa</option>
                                  <option>Venda</option>
                                  <option>French</option>

                              </select>

                            Interest

                                <select class="form-control" name="interest" required>

                                  <option>Select One...</option>
                                  <option>Golf</option>
                                  <option>Rugby</option>
                                  <option>Tennis</option>
                                  <option>Cricket</option>
                                  <option>Swimming</option>
                                  <option>Hiking</option>
                                  <option>Surfing</option>
                                  <option>Movies</option>
                                  <option>Swords</option>

                                </select>

                                <input type='submit' name='submit' value='Save' class='btn btn-primary' />
                                Back to view members

                    </form>
Comment: Please add an explanation to your answer.
Comment: Please add the explanation to your answer (edit it), otherwise it's just a wall of code.
Comment: The required keyword is use to fire non empty field error meassge
Answer: It will be much simpler to do this validation prior to POST:
<input type='text' name='firstName' class='form-control' required placeholder="Enter a valid First Name :)"/>

Place "required" property to each mandatory input field. This validation is done prior to POST so you won't need another validation from backend side.
Comment: It's good to have both server side and client side validation/checks.
Comment: I agree with Progrock that validation should be done on both ends. I however, did not do any validation on the front-end. So thanks Alexandru - the required attribute is a quick front-end fix
Answer: You're missing returns. In your codes, you have
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $emailError = "Please enter a valid email address";
}

This will only assign your message to variable but will still proceed in saving. try something like this
$sErrors = '';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $sErrors  .= "Please enter a valid email address \n";
    }
if (empty($_POST["mobileNumber"])) {
            $sErrors  .= "Mobile Nr is Required Please\n";
        }

if (strlen($sErrors) > 0) {
    return $sErrors;
}

of course you'll have to do it in all your if validator
Comment: Where would this return to exactly?
Comment: Have you tried it first? I'm assuming your page will go to the value of $_SERVER["PHP_SELF"] and will display the message in your page.
Comment: @perseusi the point being that currently the validations are in if control structures, not in a function.  And a return will just short circuit the script, and error messages will be lost.  The web client could do with appropriate feedback and hand holding.  This is half an answer.